diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs index cc513d46bf4ec..08341ff32f358 100644 --- a/clippy_lints/src/casts/unnecessary_cast.rs +++ b/clippy_lints/src/casts/unnecessary_cast.rs @@ -151,13 +151,24 @@ pub(super) fn check<'tcx>( return false; } + // If the whole cast expression is a unary expression (`(*x as T)`) or an addressof + // expression (`(&x as T)`), then not surrounding the suggestion into a block risks us + // changing the precedence of operators if the cast expression is followed by an operation + // with higher precedence than the unary operator (`(*x as T).foo()` would become + // `*x.foo()`, which changes what the `*` applies on). + // The same is true if the expression encompassing the cast expression is a unary + // expression or an addressof expression. + let needs_block = matches!(cast_expr.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..)) + || get_parent_expr(cx, expr) + .map_or(false, |e| matches!(e.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..))); + span_lint_and_sugg( cx, UNNECESSARY_CAST, expr.span, &format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"), "try", - if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::AddrOf(..))) { + if needs_block { format!("{{ {cast_str} }}") } else { cast_str diff --git a/tests/ui/unnecessary_cast.fixed b/tests/ui/unnecessary_cast.fixed index f52d325033987..288541362cddb 100644 --- a/tests/ui/unnecessary_cast.fixed +++ b/tests/ui/unnecessary_cast.fixed @@ -221,4 +221,10 @@ mod fixable { fn issue_9603() { let _: f32 = -0x400 as f32; } + + // 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) + } } diff --git a/tests/ui/unnecessary_cast.rs b/tests/ui/unnecessary_cast.rs index dfd8b454e6cca..eef3a42e35149 100644 --- a/tests/ui/unnecessary_cast.rs +++ b/tests/ui/unnecessary_cast.rs @@ -221,4 +221,10 @@ mod fixable { fn issue_9603() { let _: f32 = -0x400 as f32; } + + // 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 as usize).pow(2) + } } diff --git a/tests/ui/unnecessary_cast.stderr b/tests/ui/unnecessary_cast.stderr index 935bb71da32bd..80fd5c13d8185 100644 --- a/tests/ui/unnecessary_cast.stderr +++ b/tests/ui/unnecessary_cast.stderr @@ -241,5 +241,11 @@ error: casting to the same type is unnecessary (`f32` -> `f32`) LL | let _num = foo() as f32; | ^^^^^^^^^^^^ help: try: `foo()` -error: aborting due to 40 previous errors +error: casting to the same type is unnecessary (`usize` -> `usize`) + --> tests/ui/unnecessary_cast.rs:228:9 + | +LL | (*x as usize).pow(2) + | ^^^^^^^^^^^^^ help: try: `{ *x }` + +error: aborting due to 41 previous errors