Skip to content

Commit fad8dec

Browse files
committed
fix: false positive double_negations when it jumps macro boundary
1 parent 3014e79 commit fad8dec

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,8 @@ impl EarlyLintPass for DoubleNegations {
15841584
if let ExprKind::Unary(UnOp::Neg, ref inner) = expr.kind
15851585
&& let ExprKind::Unary(UnOp::Neg, ref inner2) = inner.kind
15861586
&& !matches!(inner2.kind, ExprKind::Unary(UnOp::Neg, _))
1587+
// Don't lint if this jumps macro expansion boundary (Issue #143980)
1588+
&& expr.span.eq_ctxt(inner.span)
15871589
{
15881590
cx.emit_span_lint(
15891591
DOUBLE_NEGATIONS,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ check-pass
2+
macro_rules! neg {
3+
($e: expr) => {
4+
-$e
5+
};
6+
}
7+
macro_rules! bad_macro {
8+
($e: expr) => {
9+
--$e //~ WARN use of a double negation
10+
};
11+
}
12+
13+
fn main() {
14+
neg!(-1);
15+
bad_macro!(1);
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: use of a double negation
2+
--> $DIR/lint-double-negations-macro.rs:9:9
3+
|
4+
LL | --$e
5+
| ^^^^
6+
...
7+
LL | bad_macro!(1);
8+
| ------------- in this macro invocation
9+
|
10+
= note: the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
11+
= note: use `-= 1` if you meant to decrement the value
12+
= note: `#[warn(double_negations)]` on by default
13+
= note: this warning originates in the macro `bad_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
14+
help: add parentheses for clarity
15+
|
16+
LL | -(-$e)
17+
| + +
18+
19+
warning: 1 warning emitted
20+

0 commit comments

Comments
 (0)