Skip to content

Commit 2cd6511

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

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ pub(super) fn check<'tcx>(
157157
// `*x.foo()`, which changes what the `*` applies on).
158158
// The same is true if the expression encompassing the cast expression is a unary
159159
// 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(..)));
160+
let needs_block = (matches!(cast_expr.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..))
161+
|| get_parent_expr(cx, expr).is_some_and(|e| {
162+
matches!(e.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..)) && !e.span.from_expansion()
163+
}));
162164

163165
span_lint_and_sugg(
164166
cx,

tests/ui/unnecessary_cast.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,11 @@ mod fixable {
269269
{ *x }.pow(2)
270270
//~^ unnecessary_cast
271271
}
272+
273+
fn issue_14640() {
274+
let x = 5usize;
275+
let vec: Vec<u64> = vec![1, 2, 3, 4, 5];
276+
assert_eq!(vec.len(), x);
277+
//~^ unnecessary_cast
278+
}
272279
}

tests/ui/unnecessary_cast.rs

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

tests/ui/unnecessary_cast.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,11 @@ error: casting to the same type is unnecessary (`usize` -> `usize`)
247247
LL | (*x as usize).pow(2)
248248
| ^^^^^^^^^^^^^ 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:276:31
252+
|
253+
LL | assert_eq!(vec.len(), x as usize);
254+
| ^^^^^^^^^^ help: try: `x`
255+
256+
error: aborting due to 42 previous errors
251257

0 commit comments

Comments
 (0)