about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryanglsh <yanglsh@shanghaitech.edu.cn>2025-07-13 20:55:06 +0800
committeryanglsh <yanglsh@shanghaitech.edu.cn>2025-07-14 00:19:05 +0800
commit901ab5b8bd84cc69b6a979847141d5077cc5b693 (patch)
tree77b4bd1ae87998bd7a6965e0231c0f1357d263ca
parent7e2d26f4b29f316dd049750b35f9004bb43aece7 (diff)
downloadrust-901ab5b8bd84cc69b6a979847141d5077cc5b693.tar.gz
rust-901ab5b8bd84cc69b6a979847141d5077cc5b693.zip
fix: `manual_assert` suggests wrongly for macros
-rw-r--r--clippy_lints/src/manual_assert.rs3
-rw-r--r--tests/ui/manual_assert.edition2018.stderr20
-rw-r--r--tests/ui/manual_assert.edition2021.stderr20
-rw-r--r--tests/ui/manual_assert.rs14
4 files changed, 54 insertions, 3 deletions
diff --git a/clippy_lints/src/manual_assert.rs b/clippy_lints/src/manual_assert.rs
index 8378e15c581..ea6b01a053a 100644
--- a/clippy_lints/src/manual_assert.rs
+++ b/clippy_lints/src/manual_assert.rs
@@ -60,7 +60,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
                 ExprKind::Unary(UnOp::Not, e) => (e, ""),
                 _ => (cond, "!"),
             };
-            let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_paren();
+            let cond_sugg =
+                sugg::Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "..", &mut applicability).maybe_paren();
             let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
             let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");
             // we show to the user the suggestion without the comments, but when applying the fix, include the
diff --git a/tests/ui/manual_assert.edition2018.stderr b/tests/ui/manual_assert.edition2018.stderr
index 8cedf2c6863..221cddf069d 100644
--- a/tests/ui/manual_assert.edition2018.stderr
+++ b/tests/ui/manual_assert.edition2018.stderr
@@ -189,5 +189,23 @@ LL -         };
 LL +         const BAR: () = assert!(!(N == 0), );
    |
 
-error: aborting due to 10 previous errors
+error: only a `panic!` in `if`-then statement
+  --> tests/ui/manual_assert.rs:116:5
+   |
+LL | /     if !is_x86_feature_detected!("ssse3") {
+LL | |
+LL | |         panic!("SSSE3 is not supported");
+LL | |     }
+   | |_____^
+   |
+help: try instead
+   |
+LL -     if !is_x86_feature_detected!("ssse3") {
+LL -
+LL -         panic!("SSSE3 is not supported");
+LL -     }
+LL +     assert!(is_x86_feature_detected!("ssse3"), "SSSE3 is not supported");
+   |
+
+error: aborting due to 11 previous errors
 
diff --git a/tests/ui/manual_assert.edition2021.stderr b/tests/ui/manual_assert.edition2021.stderr
index 8cedf2c6863..221cddf069d 100644
--- a/tests/ui/manual_assert.edition2021.stderr
+++ b/tests/ui/manual_assert.edition2021.stderr
@@ -189,5 +189,23 @@ LL -         };
 LL +         const BAR: () = assert!(!(N == 0), );
    |
 
-error: aborting due to 10 previous errors
+error: only a `panic!` in `if`-then statement
+  --> tests/ui/manual_assert.rs:116:5
+   |
+LL | /     if !is_x86_feature_detected!("ssse3") {
+LL | |
+LL | |         panic!("SSSE3 is not supported");
+LL | |     }
+   | |_____^
+   |
+help: try instead
+   |
+LL -     if !is_x86_feature_detected!("ssse3") {
+LL -
+LL -         panic!("SSSE3 is not supported");
+LL -     }
+LL +     assert!(is_x86_feature_detected!("ssse3"), "SSSE3 is not supported");
+   |
+
+error: aborting due to 11 previous errors
 
diff --git a/tests/ui/manual_assert.rs b/tests/ui/manual_assert.rs
index 46a42c3d00a..ab02bd5f5e5 100644
--- a/tests/ui/manual_assert.rs
+++ b/tests/ui/manual_assert.rs
@@ -105,3 +105,17 @@ fn issue12505() {
         };
     }
 }
+
+fn issue15227(left: u64, right: u64) -> u64 {
+    macro_rules! is_x86_feature_detected {
+        ($feature:literal) => {
+            $feature.len() > 0 && $feature.starts_with("ss")
+        };
+    }
+
+    if !is_x86_feature_detected!("ssse3") {
+        //~^ manual_assert
+        panic!("SSSE3 is not supported");
+    }
+    unsafe { todo!() }
+}