Rollup merge of #144008 - anatawa12:fix-double-negations, r=compiler-errors

Fix false positive double negations with macro invocation

This PR fixes false positive double_negations lint when macro expansion has negation and macro caller also has negations.

Fix rust-lang/rust#143980
This commit is contained in:
Matthias Krüger
2025-07-18 04:27:53 +02:00
committed by GitHub
3 changed files with 38 additions and 0 deletions
+2
View File
@@ -1585,6 +1585,8 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
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,
@@ -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);
}
@@ -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