about summary refs log tree commit diff
diff options
context:
space:
mode:
authoralex-semenyuk <alexsemenyuk88@gmail.com>2022-07-30 17:21:37 +0300
committeralex-semenyuk <alexsemenyuk88@gmail.com>2022-08-04 21:30:50 +0300
commit0e1d65850afd9ebf894526c75ddf7d6dcabf4097 (patch)
tree6be5dcae9b8ce08e8ecd250a3c90e54f0e7a9c5b
parent580f642cfac7d7035c563a060265b3dbef092ada (diff)
downloadrust-0e1d65850afd9ebf894526c75ddf7d6dcabf4097.tar.gz
rust-0e1d65850afd9ebf894526c75ddf7d6dcabf4097.zip
Fix cast_abs_to_unsigned generates non-compiling code when original code is in parens
-rw-r--r--clippy_lints/src/casts/cast_abs_to_unsigned.rs2
-rw-r--r--tests/ui/cast_abs_to_unsigned.fixed2
-rw-r--r--tests/ui/cast_abs_to_unsigned.rs2
-rw-r--r--tests/ui/cast_abs_to_unsigned.stderr8
4 files changed, 12 insertions, 2 deletions
diff --git a/clippy_lints/src/casts/cast_abs_to_unsigned.rs b/clippy_lints/src/casts/cast_abs_to_unsigned.rs
index 64ea326b75a..6426e8c25ac 100644
--- a/clippy_lints/src/casts/cast_abs_to_unsigned.rs
+++ b/clippy_lints/src/casts/cast_abs_to_unsigned.rs
@@ -37,7 +37,7 @@ pub(super) fn check(
             span,
             &format!("casting the result of `{cast_from}::abs()` to {cast_to}"),
             "replace with",
-            format!("{}.unsigned_abs()", Sugg::hir(cx, &args[0], "..")),
+            format!("{}.unsigned_abs()", Sugg::hir(cx, &args[0], "..").maybe_par()),
             Applicability::MachineApplicable,
         );
     }
diff --git a/tests/ui/cast_abs_to_unsigned.fixed b/tests/ui/cast_abs_to_unsigned.fixed
index a68b32b097e..7ecefd7b134 100644
--- a/tests/ui/cast_abs_to_unsigned.fixed
+++ b/tests/ui/cast_abs_to_unsigned.fixed
@@ -26,4 +26,6 @@ fn main() {
     let _ = a.unsigned_abs() as u32;
     let _ = a.unsigned_abs() as u64;
     let _ = a.unsigned_abs() as u128;
+
+    let _ = (x as i64 - y as i64).unsigned_abs() as u32;
 }
diff --git a/tests/ui/cast_abs_to_unsigned.rs b/tests/ui/cast_abs_to_unsigned.rs
index 110fbc6c2df..30c603fca9a 100644
--- a/tests/ui/cast_abs_to_unsigned.rs
+++ b/tests/ui/cast_abs_to_unsigned.rs
@@ -26,4 +26,6 @@ fn main() {
     let _ = a.abs() as u32;
     let _ = a.abs() as u64;
     let _ = a.abs() as u128;
+
+    let _ = (x as i64 - y as i64).abs() as u32;
 }
diff --git a/tests/ui/cast_abs_to_unsigned.stderr b/tests/ui/cast_abs_to_unsigned.stderr
index 02c24e10659..04553774526 100644
--- a/tests/ui/cast_abs_to_unsigned.stderr
+++ b/tests/ui/cast_abs_to_unsigned.stderr
@@ -96,5 +96,11 @@ error: casting the result of `isize::abs()` to u128
 LL |     let _ = a.abs() as u128;
    |             ^^^^^^^ help: replace with: `a.unsigned_abs()`
 
-error: aborting due to 16 previous errors
+error: casting the result of `i64::abs()` to u32
+  --> $DIR/cast_abs_to_unsigned.rs:30:13
+   |
+LL |     let _ = (x as i64 - y as i64).abs() as u32;
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `(x as i64 - y as i64).unsigned_abs()`
+
+error: aborting due to 17 previous errors