fix: unnecessary_cast suggests extra brackets when in macro (#14643)

Closes rust-lang/rust-clippy#14640

changelog: [`unnecessary_cast`] fix extra brackets in suggestions when
in macro
This commit is contained in:
Jason Newcomb 2025-04-26 11:55:15 +00:00 committed by GitHub
commit ff307bac14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 91 additions and 16 deletions

View file

@ -266,7 +266,21 @@ mod fixable {
// Issue #11968: The suggestion for this lint removes the parentheses and leave the code as
// `*x.pow(2)` which tries to dereference the return value rather than `x`.
fn issue_11968(x: &usize) -> usize {
{ *x }.pow(2)
(*x).pow(2)
//~^ unnecessary_cast
}
#[allow(clippy::cast_lossless)]
fn issue_14640() {
let x = 5usize;
let vec: Vec<u64> = vec![1, 2, 3, 4, 5];
assert_eq!(vec.len(), x);
//~^ unnecessary_cast
let _ = (5i32 as i64).abs();
//~^ unnecessary_cast
let _ = 5i32 as i64;
//~^ unnecessary_cast
}
}

View file

@ -269,4 +269,18 @@ mod fixable {
(*x as usize).pow(2)
//~^ unnecessary_cast
}
#[allow(clippy::cast_lossless)]
fn issue_14640() {
let x = 5usize;
let vec: Vec<u64> = vec![1, 2, 3, 4, 5];
assert_eq!(vec.len(), x as usize);
//~^ unnecessary_cast
let _ = (5i32 as i64 as i64).abs();
//~^ unnecessary_cast
let _ = 5i32 as i64 as i64;
//~^ unnecessary_cast
}
}

View file

@ -245,7 +245,25 @@ error: casting to the same type is unnecessary (`usize` -> `usize`)
--> tests/ui/unnecessary_cast.rs:269:9
|
LL | (*x as usize).pow(2)
| ^^^^^^^^^^^^^ help: try: `{ *x }`
| ^^^^^^^^^^^^^ help: try: `(*x)`
error: aborting due to 41 previous errors
error: casting to the same type is unnecessary (`usize` -> `usize`)
--> tests/ui/unnecessary_cast.rs:277:31
|
LL | assert_eq!(vec.len(), x as usize);
| ^^^^^^^^^^ help: try: `x`
error: casting to the same type is unnecessary (`i64` -> `i64`)
--> tests/ui/unnecessary_cast.rs:280:17
|
LL | let _ = (5i32 as i64 as i64).abs();
| ^^^^^^^^^^^^^^^^^^^^ help: try: `(5i32 as i64)`
error: casting to the same type is unnecessary (`i64` -> `i64`)
--> tests/ui/unnecessary_cast.rs:283:17
|
LL | let _ = 5i32 as i64 as i64;
| ^^^^^^^^^^^^^^^^^^ help: try: `5i32 as i64`
error: aborting due to 44 previous errors