about summary refs log tree commit diff
diff options
context:
space:
mode:
authoranatawa12 <anatawa12@icloud.com>2025-07-16 15:39:31 +0900
committeranatawa12 <anatawa12@icloud.com>2025-07-17 11:24:17 +0900
commitfad8dec970f51a2d4c23593b059b1684bd85472a (patch)
tree669c7037e4b68a0c52d7a3cf5e9108ad5d4963ba
parent3014e79f9c8d5510ea7b3a3b70d171d0948b1e96 (diff)
downloadrust-fad8dec970f51a2d4c23593b059b1684bd85472a.tar.gz
rust-fad8dec970f51a2d4c23593b059b1684bd85472a.zip
fix: false positive double_negations when it jumps macro boundary
-rw-r--r--compiler/rustc_lint/src/builtin.rs2
-rw-r--r--tests/ui/lint/lint-double-negations-macro.rs16
-rw-r--r--tests/ui/lint/lint-double-negations-macro.stderr20
3 files changed, 38 insertions, 0 deletions
diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs
index 255ff56f62b..93df056c43b 100644
--- a/compiler/rustc_lint/src/builtin.rs
+++ b/compiler/rustc_lint/src/builtin.rs
@@ -1584,6 +1584,8 @@ impl EarlyLintPass for DoubleNegations {
         if let ExprKind::Unary(UnOp::Neg, ref inner) = expr.kind
             && let ExprKind::Unary(UnOp::Neg, ref inner2) = inner.kind
             && !matches!(inner2.kind, ExprKind::Unary(UnOp::Neg, _))
+            // Don't lint if this jumps macro expansion boundary (Issue #143980)
+            && expr.span.eq_ctxt(inner.span)
         {
             cx.emit_span_lint(
                 DOUBLE_NEGATIONS,
diff --git a/tests/ui/lint/lint-double-negations-macro.rs b/tests/ui/lint/lint-double-negations-macro.rs
new file mode 100644
index 00000000000..a6583271d5a
--- /dev/null
+++ b/tests/ui/lint/lint-double-negations-macro.rs
@@ -0,0 +1,16 @@
+//@ check-pass
+macro_rules! neg {
+    ($e: expr) => {
+        -$e
+    };
+}
+macro_rules! bad_macro {
+    ($e: expr) => {
+        --$e //~ WARN use of a double negation
+    };
+}
+
+fn main() {
+    neg!(-1);
+    bad_macro!(1);
+}
diff --git a/tests/ui/lint/lint-double-negations-macro.stderr b/tests/ui/lint/lint-double-negations-macro.stderr
new file mode 100644
index 00000000000..d6ac9be48f3
--- /dev/null
+++ b/tests/ui/lint/lint-double-negations-macro.stderr
@@ -0,0 +1,20 @@
+warning: use of a double negation
+  --> $DIR/lint-double-negations-macro.rs:9:9
+   |
+LL |         --$e
+   |         ^^^^
+...
+LL |     bad_macro!(1);
+   |     ------------- in this macro invocation
+   |
+   = note: the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
+   = note: use `-= 1` if you meant to decrement the value
+   = note: `#[warn(double_negations)]` on by default
+   = note: this warning originates in the macro `bad_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
+help: add parentheses for clarity
+   |
+LL |         -(-$e)
+   |          +   +
+
+warning: 1 warning emitted
+