]> git.lizzy.rs Git - rust.git/commitdiff
Fix `unnecessary_cast` suggestion when taking a reference
authorJason Newcomb <jsnewcomb@pm.me>
Wed, 30 Nov 2022 15:48:53 +0000 (10:48 -0500)
committerJason Newcomb <jsnewcomb@pm.me>
Wed, 30 Nov 2022 15:55:48 +0000 (10:55 -0500)
clippy_lints/src/casts/unnecessary_cast.rs
tests/ui/unnecessary_cast.fixed
tests/ui/unnecessary_cast.rs
tests/ui/unnecessary_cast.stderr

index ecc8a8de97b930cd25c365c2dbddc20e8b8d82c3..7e23318076cf2e8b9aa6ec5247b39d2268c3ca38 100644 (file)
@@ -90,7 +90,11 @@ pub(super) fn check<'tcx>(
             expr.span,
             &format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
             "try",
-            cast_str,
+            if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::AddrOf(..))) {
+                format!("{{ {cast_str} }}")
+            } else {
+                cast_str
+            },
             Applicability::MachineApplicable,
         );
         return true;
index f234d4473c3e9b12bdd9239dea28ede9ef6acd17..2f7e2997e739d50ec990455b60cbf898820ad2bf 100644 (file)
@@ -96,6 +96,9 @@ mod fixable {
 
         let _ = 1 as I32Alias;
         let _ = &1 as &I32Alias;
+
+        let x = 1i32;
+        let _ = &{ x };
     }
 
     type I32Alias = i32;
index 855a4efa03411e033bb972e2e2ddba2ae126483c..54dd46ba59f104a1172d14c9363b09b08ad79627 100644 (file)
@@ -96,6 +96,9 @@ fn main() {
 
         let _ = 1 as I32Alias;
         let _ = &1 as &I32Alias;
+
+        let x = 1i32;
+        let _ = &(x as i32);
     }
 
     type I32Alias = i32;
index 934db0e86e662539b4e158c55d61696ae8ba4935..fcee4ee2a65cc9fa267c65354382292421c3ede8 100644 (file)
@@ -150,35 +150,41 @@ error: casting float literal to `f32` is unnecessary
 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
+   |
+LL |         let _ = &(x as i32);
+   |                  ^^^^^^^^^^ help: try: `{ x }`
+
 error: casting integer literal to `i32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:104:22
+  --> $DIR/unnecessary_cast.rs:107:22
    |
 LL |         let _: i32 = -(1) as i32;
    |                      ^^^^^^^^^^^ help: try: `-1_i32`
 
 error: casting integer literal to `i64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:106:22
+  --> $DIR/unnecessary_cast.rs:109:22
    |
 LL |         let _: i64 = -(1) as i64;
    |                      ^^^^^^^^^^^ help: try: `-1_i64`
 
 error: casting float literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:113:22
+  --> $DIR/unnecessary_cast.rs:116: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:115:23
+  --> $DIR/unnecessary_cast.rs:118: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:123:20
+  --> $DIR/unnecessary_cast.rs:126:20
    |
 LL |         let _num = foo() as f32;
    |                    ^^^^^^^^^^^^ help: try: `foo()`
 
-error: aborting due to 30 previous errors
+error: aborting due to 31 previous errors