Skip to content

Commit 9c39d9b

Browse files
committed
fix: unnecessary_cast suggests extra brackets when in macro
1 parent 8eed350 commit 9c39d9b

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::numeric_literal::NumericLiteral;
33
use clippy_utils::source::{SpanRangeExt, snippet_opt};
4+
use clippy_utils::sugg::Sugg;
45
use clippy_utils::visitors::{Visitable, for_each_expr_without_closures};
56
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local};
67
use rustc_ast::{LitFloatType, LitIntType, LitKind};
@@ -157,21 +158,20 @@ pub(super) fn check<'tcx>(
157158
// `*x.foo()`, which changes what the `*` applies on).
158159
// The same is true if the expression encompassing the cast expression is a unary
159160
// expression or an addressof expression.
160-
let needs_block = matches!(cast_expr.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..))
161-
|| get_parent_expr(cx, expr).is_some_and(|e| matches!(e.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..)));
162-
163-
span_lint_and_sugg(
161+
span_lint_and_then(
164162
cx,
165163
UNNECESSARY_CAST,
166164
expr.span,
167165
format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
168-
"try",
169-
if needs_block {
170-
format!("{{ {cast_str} }}")
171-
} else {
172-
cast_str
166+
|diag| {
167+
let mut applicability = Applicability::MachineApplicable;
168+
let sugg = Sugg::hir_with_applicability(cx, cast_expr, "..", &mut applicability);
169+
if applicability != Applicability::MachineApplicable {
170+
return;
171+
}
172+
173+
diag.span_suggestion(expr.span, "try", sugg.maybe_paren().to_string(), applicability);
173174
},
174-
Applicability::MachineApplicable,
175175
);
176176
return true;
177177
}

tests/ui/unnecessary_cast.fixed

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ mod fixable {
224224
let _ = &1 as &I32Alias;
225225

226226
let x = 1i32;
227-
let _ = &{ x };
227+
let _ = &x;
228228
//~^ unnecessary_cast
229229
}
230230

@@ -266,7 +266,18 @@ mod fixable {
266266
// Issue #11968: The suggestion for this lint removes the parentheses and leave the code as
267267
// `*x.pow(2)` which tries to dereference the return value rather than `x`.
268268
fn issue_11968(x: &usize) -> usize {
269-
{ *x }.pow(2)
269+
(*x).pow(2)
270+
//~^ unnecessary_cast
271+
}
272+
273+
#[allow(clippy::cast_lossless)]
274+
fn issue_14640() {
275+
let x = 5usize;
276+
let vec: Vec<u64> = vec![1, 2, 3, 4, 5];
277+
assert_eq!(vec.len(), x);
278+
//~^ unnecessary_cast
279+
280+
let _ = (5i32 as i64).abs();
270281
//~^ unnecessary_cast
271282
}
272283
}

tests/ui/unnecessary_cast.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,15 @@ mod fixable {
269269
(*x as usize).pow(2)
270270
//~^ unnecessary_cast
271271
}
272+
273+
#[allow(clippy::cast_lossless)]
274+
fn issue_14640() {
275+
let x = 5usize;
276+
let vec: Vec<u64> = vec![1, 2, 3, 4, 5];
277+
assert_eq!(vec.len(), x as usize);
278+
//~^ unnecessary_cast
279+
280+
let _ = (5i32 as i64 as i64).abs();
281+
//~^ unnecessary_cast
282+
}
272283
}

tests/ui/unnecessary_cast.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ error: casting to the same type is unnecessary (`i32` -> `i32`)
209209
--> tests/ui/unnecessary_cast.rs:227:18
210210
|
211211
LL | let _ = &(x as i32);
212-
| ^^^^^^^^^^ help: try: `{ x }`
212+
| ^^^^^^^^^^ help: try: `x`
213213

214214
error: casting integer literal to `i32` is unnecessary
215215
--> tests/ui/unnecessary_cast.rs:234:22
@@ -245,7 +245,19 @@ error: casting to the same type is unnecessary (`usize` -> `usize`)
245245
--> tests/ui/unnecessary_cast.rs:269:9
246246
|
247247
LL | (*x as usize).pow(2)
248-
| ^^^^^^^^^^^^^ help: try: `{ *x }`
248+
| ^^^^^^^^^^^^^ help: try: `(*x)`
249249

250-
error: aborting due to 41 previous errors
250+
error: casting to the same type is unnecessary (`usize` -> `usize`)
251+
--> tests/ui/unnecessary_cast.rs:277:31
252+
|
253+
LL | assert_eq!(vec.len(), x as usize);
254+
| ^^^^^^^^^^ help: try: `x`
255+
256+
error: casting to the same type is unnecessary (`i64` -> `i64`)
257+
--> tests/ui/unnecessary_cast.rs:280:17
258+
|
259+
LL | let _ = (5i32 as i64 as i64).abs();
260+
| ^^^^^^^^^^^^^^^^^^^^ help: try: `(5i32 as i64)`
261+
262+
error: aborting due to 43 previous errors
251263

0 commit comments

Comments
 (0)