summary refs log tree commit diff
diff options
context:
space:
mode:
authoryanglsh <yanglsh@shanghaitech.edu.cn>2025-03-17 23:28:43 +0800
committeryanglsh <yanglsh@shanghaitech.edu.cn>2025-03-22 22:20:22 +0800
commit282b61b64174f8dca382932c5f5f3385c28c4a11 (patch)
treeecb70921a7fd1cae2d542eae51f2ad4761debb0a
parent48005578576c7820590f680b92f5f6213b9165f5 (diff)
downloadrust-282b61b64174f8dca382932c5f5f3385c28c4a11.tar.gz
rust-282b61b64174f8dca382932c5f5f3385c28c4a11.zip
fix: `nonminimal_bool` wrongly showed the macro definition
-rw-r--r--clippy_lints/src/booleans.rs12
-rw-r--r--tests/ui/nonminimal_bool.rs20
-rw-r--r--tests/ui/nonminimal_bool.stderr10
3 files changed, 36 insertions, 6 deletions
diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs
index 48b5d4da888..36c1aea2637 100644
--- a/clippy_lints/src/booleans.rs
+++ b/clippy_lints/src/booleans.rs
@@ -13,7 +13,7 @@ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
 use rustc_lint::{LateContext, LateLintPass, Level};
 use rustc_session::impl_lint_pass;
 use rustc_span::def_id::LocalDefId;
-use rustc_span::{Span, sym};
+use rustc_span::{Span, SyntaxContext, sym};
 
 declare_clippy_lint! {
     /// ### What it does
@@ -349,9 +349,13 @@ impl SuggestContext<'_, '_, '_> {
                     if let Some(str) = simplify_not(self.cx, self.msrv, terminal) {
                         self.output.push_str(&str);
                     } else {
-                        self.output.push('!');
-                        self.output
-                            .push_str(&Sugg::hir_opt(self.cx, terminal)?.maybe_par().to_string());
+                        let mut app = Applicability::MachineApplicable;
+                        let snip = Sugg::hir_with_context(self.cx, terminal, SyntaxContext::root(), "", &mut app);
+                        // Ignore the case If the expression is inside a macro expansion, or the default snippet is used
+                        if app != Applicability::MachineApplicable {
+                            return None;
+                        }
+                        self.output.push_str(&(!snip).to_string());
                     }
                 },
                 True | False | Not(_) => {
diff --git a/tests/ui/nonminimal_bool.rs b/tests/ui/nonminimal_bool.rs
index a155ff3508b..1eecc3dee3d 100644
--- a/tests/ui/nonminimal_bool.rs
+++ b/tests/ui/nonminimal_bool.rs
@@ -216,3 +216,23 @@ fn issue14184(a: f32, b: bool) {
         println!("Hi");
     }
 }
+
+mod issue14404 {
+    enum TyKind {
+        Ref(i32, i32, i32),
+        Other,
+    }
+
+    struct Expr;
+
+    fn is_mutable(expr: &Expr) -> bool {
+        todo!()
+    }
+
+    fn should_not_give_macro(ty: TyKind, expr: Expr) {
+        if !(matches!(ty, TyKind::Ref(_, _, _)) && !is_mutable(&expr)) {
+            //~^ nonminimal_bool
+            todo!()
+        }
+    }
+}
diff --git a/tests/ui/nonminimal_bool.stderr b/tests/ui/nonminimal_bool.stderr
index 336cce40abf..0e3e4cf7988 100644
--- a/tests/ui/nonminimal_bool.stderr
+++ b/tests/ui/nonminimal_bool.stderr
@@ -227,7 +227,13 @@ error: this boolean expression can be simplified
   --> tests/ui/nonminimal_bool.rs:214:8
    |
 LL |     if !(a < 2.0 && !b) {
-   |        ^^^^^^^^^^^^^^^^ help: try: `!(a < 2.0) || b`
+   |        ^^^^^^^^^^^^^^^^ help: try: `a >= 2.0 || b`
 
-error: aborting due to 30 previous errors
+error: this boolean expression can be simplified
+  --> tests/ui/nonminimal_bool.rs:233:12
+   |
+LL |         if !(matches!(ty, TyKind::Ref(_, _, _)) && !is_mutable(&expr)) {
+   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!matches!(ty, TyKind::Ref(_, _, _)) || is_mutable(&expr)`
+
+error: aborting due to 31 previous errors