Auto merge of #10821 - Centri3:unnecessary_operation_cast_underscore, r=dswij

Emit `unnecessary_cast` on raw pointers as well

Supersedes(?) #10782, since this and #10567 will cover the original issue.
Does not lint on type aliases or inferred types.

changelog: [`unnecessary_cast`]: Also emit on casts between raw pointers with the same type and constness
This commit is contained in:
bors 2023-06-02 17:23:56 +00:00
commit c85ceeae3f
12 changed files with 189 additions and 57 deletions

View File

@ -174,8 +174,8 @@ declare_clippy_lint! {
declare_clippy_lint! {
/// ### What it does
/// Checks for casts to the same type, casts of int literals to integer types
/// and casts of float literals to float types.
/// Checks for casts to the same type, casts of int literals to integer types, casts of float
/// literals to float types and casts between raw pointers without changing type or constness.
///
/// ### Why is this bad?
/// It's just unnecessary.

View File

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::numeric_literal::NumericLiteral;
use clippy_utils::source::snippet_opt;
use clippy_utils::{get_parent_expr, path_to_local};
use clippy_utils::{get_parent_expr, is_ty_alias, path_to_local};
use if_chain::if_chain;
use rustc_ast::{LitFloatType, LitIntType, LitKind};
use rustc_errors::Applicability;
@ -20,6 +20,38 @@ pub(super) fn check<'tcx>(
cast_from: Ty<'tcx>,
cast_to: Ty<'tcx>,
) -> bool {
let cast_str = snippet_opt(cx, cast_expr.span).unwrap_or_default();
if_chain! {
if let ty::RawPtr(..) = cast_from.kind();
// check both mutability and type are the same
if cast_from.kind() == cast_to.kind();
if let ExprKind::Cast(_, cast_to_hir) = expr.kind;
then {
if_chain! {
if let TyKind::Path(qpath) = cast_to_hir.kind;
if is_ty_alias(&qpath);
then {
return false;
}
}
if let TyKind::Infer = cast_to_hir.kind {
return false;
}
span_lint_and_sugg(
cx,
UNNECESSARY_CAST,
expr.span,
&format!("casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"),
"try",
cast_str.clone(),
Applicability::MachineApplicable,
);
}
}
// skip non-primitive type cast
if_chain! {
if let ExprKind::Cast(_, cast_to) = expr.kind;
@ -27,12 +59,10 @@ pub(super) fn check<'tcx>(
if let Res::PrimTy(_) = path.res;
then {}
else {
return false
return false;
}
}
let cast_str = snippet_opt(cx, cast_expr.span).unwrap_or_default();
if let Some(lit) = get_numeric_literal(cast_expr) {
let literal_str = &cast_str;

View File

@ -1,6 +1,6 @@
#![allow(unused)]
#![warn(clippy::as_ptr_cast_mut)]
#![allow(clippy::wrong_self_convention)]
#![allow(clippy::wrong_self_convention, clippy::unnecessary_cast)]
struct MutPtrWrapper(Vec<u8>);
impl MutPtrWrapper {

View File

@ -1,4 +1,4 @@
#![allow(clippy::let_unit_value)]
#![allow(clippy::let_unit_value, clippy::unnecessary_cast)]
fn main() {
let x: [i32; 3] = [1_i32, 2, 3];

View File

@ -1,4 +1,5 @@
#![allow(dead_code, unused_variables)]
#![allow(clippy::unnecessary_cast)]
/// Should not trigger an ICE in `SpanlessEq` / `consts::constant`
///

View File

@ -1,4 +1,5 @@
#![warn(clippy::from_raw_with_void_ptr)]
#![allow(clippy::unnecessary_cast)]
use std::ffi::c_void;
use std::rc::Rc;

View File

@ -1,60 +1,60 @@
error: creating a `Box` from a void raw pointer
--> $DIR/from_raw_with_void_ptr.rs:10:22
--> $DIR/from_raw_with_void_ptr.rs:11:22
|
LL | let _ = unsafe { Box::from_raw(ptr) };
| ^^^^^^^^^^^^^^^^^^
|
help: cast this to a pointer of the appropriate type
--> $DIR/from_raw_with_void_ptr.rs:10:36
--> $DIR/from_raw_with_void_ptr.rs:11:36
|
LL | let _ = unsafe { Box::from_raw(ptr) };
| ^^^
= note: `-D clippy::from-raw-with-void-ptr` implied by `-D warnings`
error: creating a `Rc` from a void raw pointer
--> $DIR/from_raw_with_void_ptr.rs:21:22
--> $DIR/from_raw_with_void_ptr.rs:22:22
|
LL | let _ = unsafe { Rc::from_raw(ptr) };
| ^^^^^^^^^^^^^^^^^
|
help: cast this to a pointer of the appropriate type
--> $DIR/from_raw_with_void_ptr.rs:21:35
--> $DIR/from_raw_with_void_ptr.rs:22:35
|
LL | let _ = unsafe { Rc::from_raw(ptr) };
| ^^^
error: creating a `Arc` from a void raw pointer
--> $DIR/from_raw_with_void_ptr.rs:25:22
--> $DIR/from_raw_with_void_ptr.rs:26:22
|
LL | let _ = unsafe { Arc::from_raw(ptr) };
| ^^^^^^^^^^^^^^^^^^
|
help: cast this to a pointer of the appropriate type
--> $DIR/from_raw_with_void_ptr.rs:25:36
--> $DIR/from_raw_with_void_ptr.rs:26:36
|
LL | let _ = unsafe { Arc::from_raw(ptr) };
| ^^^
error: creating a `Weak` from a void raw pointer
--> $DIR/from_raw_with_void_ptr.rs:29:22
--> $DIR/from_raw_with_void_ptr.rs:30:22
|
LL | let _ = unsafe { std::rc::Weak::from_raw(ptr) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: cast this to a pointer of the appropriate type
--> $DIR/from_raw_with_void_ptr.rs:29:46
--> $DIR/from_raw_with_void_ptr.rs:30:46
|
LL | let _ = unsafe { std::rc::Weak::from_raw(ptr) };
| ^^^
error: creating a `Weak` from a void raw pointer
--> $DIR/from_raw_with_void_ptr.rs:33:22
--> $DIR/from_raw_with_void_ptr.rs:34:22
|
LL | let _ = unsafe { std::sync::Weak::from_raw(ptr) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: cast this to a pointer of the appropriate type
--> $DIR/from_raw_with_void_ptr.rs:33:48
--> $DIR/from_raw_with_void_ptr.rs:34:48
|
LL | let _ = unsafe { std::sync::Weak::from_raw(ptr) };
| ^^^

View File

@ -1,7 +1,7 @@
//@run-rustfix
#![warn(clippy::transmute_ptr_to_ref)]
#![allow(clippy::match_single_binding)]
#![allow(clippy::match_single_binding, clippy::unnecessary_cast)]
unsafe fn _ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
let _: &T = &*p;

View File

@ -1,7 +1,7 @@
//@run-rustfix
#![warn(clippy::transmute_ptr_to_ref)]
#![allow(clippy::match_single_binding)]
#![allow(clippy::match_single_binding, clippy::unnecessary_cast)]
unsafe fn _ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
let _: &T = std::mem::transmute(p);

View File

@ -1,13 +1,24 @@
//@run-rustfix
#![warn(clippy::unnecessary_cast)]
#![allow(
unused_must_use,
unused,
clippy::borrow_as_ptr,
clippy::no_effect,
clippy::nonstandard_macro_braces,
clippy::unnecessary_operation
)]
type PtrConstU8 = *const u8;
type PtrMutU8 = *mut u8;
fn owo<T>(ptr: *const T) -> *const T {
ptr
}
fn uwu<T, U>(ptr: *const T) -> *const U {
ptr as *const U
}
#[rustfmt::skip]
fn main() {
// Test cast_unnecessary
@ -22,6 +33,24 @@ fn main() {
1_i32;
1_f32;
let _: *mut u8 = [1u8, 2].as_ptr() as *mut u8;
[1u8, 2].as_ptr();
[1u8, 2].as_ptr() as *mut u8;
[1u8, 2].as_mut_ptr();
[1u8, 2].as_mut_ptr() as *const u8;
[1u8, 2].as_ptr() as PtrConstU8;
[1u8, 2].as_ptr() as PtrMutU8;
[1u8, 2].as_mut_ptr() as PtrMutU8;
[1u8, 2].as_mut_ptr() as PtrConstU8;
let _: *const u8 = [1u8, 2].as_ptr() as _;
let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
owo::<u32>([1u32].as_ptr());
uwu::<u32, u8>([1u32].as_ptr());
// this will not lint in the function body even though they have the same type, instead here
uwu::<u32, u32>([1u32].as_ptr());
// macro version
macro_rules! foo {
($a:ident, $b:ident) => {

View File

@ -1,13 +1,24 @@
//@run-rustfix
#![warn(clippy::unnecessary_cast)]
#![allow(
unused_must_use,
unused,
clippy::borrow_as_ptr,
clippy::no_effect,
clippy::nonstandard_macro_braces,
clippy::unnecessary_operation
)]
type PtrConstU8 = *const u8;
type PtrMutU8 = *mut u8;
fn owo<T>(ptr: *const T) -> *const T {
ptr as *const T
}
fn uwu<T, U>(ptr: *const T) -> *const U {
ptr as *const U
}
#[rustfmt::skip]
fn main() {
// Test cast_unnecessary
@ -22,6 +33,24 @@ fn main() {
1_i32 as i32;
1_f32 as f32;
let _: *mut u8 = [1u8, 2].as_ptr() as *const u8 as *mut u8;
[1u8, 2].as_ptr() as *const u8;
[1u8, 2].as_ptr() as *mut u8;
[1u8, 2].as_mut_ptr() as *mut u8;
[1u8, 2].as_mut_ptr() as *const u8;
[1u8, 2].as_ptr() as PtrConstU8;
[1u8, 2].as_ptr() as PtrMutU8;
[1u8, 2].as_mut_ptr() as PtrMutU8;
[1u8, 2].as_mut_ptr() as PtrConstU8;
let _: *const u8 = [1u8, 2].as_ptr() as _;
let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
owo::<u32>([1u32].as_ptr()) as *const u32;
uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
// this will not lint in the function body even though they have the same type, instead here
uwu::<u32, u32>([1u32].as_ptr()) as *const u32;
// macro version
macro_rules! foo {
($a:ident, $b:ident) => {

View File

@ -1,190 +1,232 @@
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:14:5
error: casting raw pointers to the same type and constness is unnecessary (`*const T` -> `*const T`)
--> $DIR/unnecessary_cast.rs:15:5
|
LL | 1i32 as i32;
| ^^^^^^^^^^^ help: try: `1_i32`
LL | ptr as *const T
| ^^^^^^^^^^^^^^^ help: try: `ptr`
|
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:25:5
|
LL | 1i32 as i32;
| ^^^^^^^^^^^ help: try: `1_i32`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:15:5
--> $DIR/unnecessary_cast.rs:26:5
|
LL | 1f32 as f32;
| ^^^^^^^^^^^ help: try: `1_f32`
error: casting to the same type is unnecessary (`bool` -> `bool`)
--> $DIR/unnecessary_cast.rs:16:5
--> $DIR/unnecessary_cast.rs:27:5
|
LL | false as bool;
| ^^^^^^^^^^^^^ help: try: `false`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:19:5
--> $DIR/unnecessary_cast.rs:30:5
|
LL | -1_i32 as i32;
| ^^^^^^^^^^^^^ help: try: `-1_i32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:20:5
--> $DIR/unnecessary_cast.rs:31:5
|
LL | - 1_i32 as i32;
| ^^^^^^^^^^^^^^ help: try: `- 1_i32`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:21:5
--> $DIR/unnecessary_cast.rs:32:5
|
LL | -1f32 as f32;
| ^^^^^^^^^^^^ help: try: `-1_f32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:22:5
--> $DIR/unnecessary_cast.rs:33:5
|
LL | 1_i32 as i32;
| ^^^^^^^^^^^^ help: try: `1_i32`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:23:5
--> $DIR/unnecessary_cast.rs:34:5
|
LL | 1_f32 as f32;
| ^^^^^^^^^^^^ help: try: `1_f32`
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
--> $DIR/unnecessary_cast.rs:36:22
|
LL | let _: *mut u8 = [1u8, 2].as_ptr() as *const u8 as *mut u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_ptr()`
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
--> $DIR/unnecessary_cast.rs:38:5
|
LL | [1u8, 2].as_ptr() as *const u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_ptr()`
error: casting raw pointers to the same type and constness is unnecessary (`*mut u8` -> `*mut u8`)
--> $DIR/unnecessary_cast.rs:40:5
|
LL | [1u8, 2].as_mut_ptr() as *mut u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_mut_ptr()`
error: casting raw pointers to the same type and constness is unnecessary (`*const u32` -> `*const u32`)
--> $DIR/unnecessary_cast.rs:49:5
|
LL | owo::<u32>([1u32].as_ptr()) as *const u32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `owo::<u32>([1u32].as_ptr())`
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
--> $DIR/unnecessary_cast.rs:50:5
|
LL | uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u8>([1u32].as_ptr())`
error: casting raw pointers to the same type and constness is unnecessary (`*const u32` -> `*const u32`)
--> $DIR/unnecessary_cast.rs:52:5
|
LL | uwu::<u32, u32>([1u32].as_ptr()) as *const u32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u32>([1u32].as_ptr())`
error: casting integer literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:64:9
--> $DIR/unnecessary_cast.rs:93:9
|
LL | 100 as f32;
| ^^^^^^^^^^ help: try: `100_f32`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:65:9
--> $DIR/unnecessary_cast.rs:94:9
|
LL | 100 as f64;
| ^^^^^^^^^^ help: try: `100_f64`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:66:9
--> $DIR/unnecessary_cast.rs:95:9
|
LL | 100_i32 as f64;
| ^^^^^^^^^^^^^^ help: try: `100_f64`
error: casting integer literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:67:17
--> $DIR/unnecessary_cast.rs:96:17
|
LL | let _ = -100 as f32;
| ^^^^^^^^^^^ help: try: `-100_f32`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:68:17
--> $DIR/unnecessary_cast.rs:97:17
|
LL | let _ = -100 as f64;
| ^^^^^^^^^^^ help: try: `-100_f64`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:69:17
--> $DIR/unnecessary_cast.rs:98:17
|
LL | let _ = -100_i32 as f64;
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:70:9
--> $DIR/unnecessary_cast.rs:99:9
|
LL | 100. as f32;
| ^^^^^^^^^^^ help: try: `100_f32`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:71:9
--> $DIR/unnecessary_cast.rs:100:9
|
LL | 100. as f64;
| ^^^^^^^^^^^ help: try: `100_f64`
error: casting integer literal to `u32` is unnecessary
--> $DIR/unnecessary_cast.rs:83:9
--> $DIR/unnecessary_cast.rs:112:9
|
LL | 1 as u32;
| ^^^^^^^^ help: try: `1_u32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:84:9
--> $DIR/unnecessary_cast.rs:113:9
|
LL | 0x10 as i32;
| ^^^^^^^^^^^ help: try: `0x10_i32`
error: casting integer literal to `usize` is unnecessary
--> $DIR/unnecessary_cast.rs:85:9
--> $DIR/unnecessary_cast.rs:114:9
|
LL | 0b10 as usize;
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
error: casting integer literal to `u16` is unnecessary
--> $DIR/unnecessary_cast.rs:86:9
--> $DIR/unnecessary_cast.rs:115:9
|
LL | 0o73 as u16;
| ^^^^^^^^^^^ help: try: `0o73_u16`
error: casting integer literal to `u32` is unnecessary
--> $DIR/unnecessary_cast.rs:87:9
--> $DIR/unnecessary_cast.rs:116:9
|
LL | 1_000_000_000 as u32;
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:89:9
--> $DIR/unnecessary_cast.rs:118:9
|
LL | 1.0 as f64;
| ^^^^^^^^^^ help: try: `1.0_f64`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:90:9
--> $DIR/unnecessary_cast.rs:119:9
|
LL | 0.5 as f32;
| ^^^^^^^^^^ help: try: `0.5_f32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:94:17
--> $DIR/unnecessary_cast.rs:123:17
|
LL | let _ = -1 as i32;
| ^^^^^^^^^ help: try: `-1_i32`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:95:17
--> $DIR/unnecessary_cast.rs:124:17
|
LL | let _ = -1.0 as f32;
| ^^^^^^^^^^^ help: try: `-1.0_f32`
error: casting to the same type is unnecessary (`i32` -> `i32`)
--> $DIR/unnecessary_cast.rs:101:18
--> $DIR/unnecessary_cast.rs:130:18
|
LL | let _ = &(x as i32);
| ^^^^^^^^^^ help: try: `{ x }`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:107:22
--> $DIR/unnecessary_cast.rs:136:22
|
LL | let _: i32 = -(1) as i32;
| ^^^^^^^^^^^ help: try: `-1_i32`
error: casting integer literal to `i64` is unnecessary
--> $DIR/unnecessary_cast.rs:109:22
--> $DIR/unnecessary_cast.rs:138:22
|
LL | let _: i64 = -(1) as i64;
| ^^^^^^^^^^^ help: try: `-1_i64`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:116:22
--> $DIR/unnecessary_cast.rs:145:22
|
LL | let _: f64 = (-8.0 as f64).exp();
| ^^^^^^^^^^^^^ help: try: `(-8.0_f64)`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:118:23
--> $DIR/unnecessary_cast.rs:147:23
|
LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
| ^^^^^^^^^^^^ help: try: `8.0_f64`
error: casting to the same type is unnecessary (`f32` -> `f32`)
--> $DIR/unnecessary_cast.rs:126:20
--> $DIR/unnecessary_cast.rs:155:20
|
LL | let _num = foo() as f32;
| ^^^^^^^^^^^^ help: try: `foo()`
error: aborting due to 31 previous errors
error: aborting due to 38 previous errors