Skip to content

Fix false positive double negations with macro invocation #144008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/lint/lint-double-negations-macro.rs
Original file line number Diff line number Diff line change
@@ -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);
}
20 changes: 20 additions & 0 deletions tests/ui/lint/lint-double-negations-macro.stderr
Original file line number Diff line number Diff line change
@@ -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

Loading