diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2024-02-08 22:36:41 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2024-02-11 21:22:33 +0100 |
| commit | a18e0a11f7a0a62a7fcaec178aad34f880b659dc (patch) | |
| tree | 6332fa56590e3036ae6d706107e543ba78659ef3 | |
| parent | 9fb41079ca03a68e1ffc63d82643eb0afe68d3a1 (diff) | |
| download | rust-a18e0a11f7a0a62a7fcaec178aad34f880b659dc.tar.gz rust-a18e0a11f7a0a62a7fcaec178aad34f880b659dc.zip | |
Extend `NONMINIMAL_BOOL` lint
| -rw-r--r-- | clippy_lints/src/booleans.rs | 26 | ||||
| -rw-r--r-- | tests/ui/nonminimal_bool.rs | 8 | ||||
| -rw-r--r-- | tests/ui/nonminimal_bool.stderr | 26 |
3 files changed, 59 insertions, 1 deletions
diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index 2d1c250ace9..3c17f65f0d3 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -85,6 +85,32 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool { ) { NonminimalBoolVisitor { cx }.visit_body(body); } + + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { + if let ExprKind::Unary(UnOp::Not, sub) = expr.kind + && !expr.span.from_expansion() + && let ExprKind::Binary(op, left, right) = sub.kind + { + let new_op = match op.node { + BinOpKind::Eq => "!=", + BinOpKind::Ne => "==", + _ => return, + }; + let Some(left) = snippet_opt(cx, left.span) else { return }; + let Some(right) = snippet_opt(cx, right.span) else { + return; + }; + span_lint_and_sugg( + cx, + NONMINIMAL_BOOL, + expr.span, + "this boolean expression can be simplified", + "try", + format!("{left} {new_op} {right}"), + Applicability::MachineApplicable, + ); + } + } } struct NonminimalBoolVisitor<'a, 'tcx> { cx: &'a LateContext<'tcx>, diff --git a/tests/ui/nonminimal_bool.rs b/tests/ui/nonminimal_bool.rs index f7c3df7066f..ee092b9aca6 100644 --- a/tests/ui/nonminimal_bool.rs +++ b/tests/ui/nonminimal_bool.rs @@ -156,3 +156,11 @@ fn issue11932() { x % 3 == 0 }; } + +fn issue_5794() { + let a = 0; + if !(12 == a) {} //~ ERROR: this boolean expression can be simplified + if !(a == 12) {} //~ ERROR: this boolean expression can be simplified + if !(12 != a) {} //~ ERROR: this boolean expression can be simplified + if !(a != 12) {} //~ ERROR: this boolean expression can be simplified +} diff --git a/tests/ui/nonminimal_bool.stderr b/tests/ui/nonminimal_bool.stderr index fd1568d94e3..856b5cd0869 100644 --- a/tests/ui/nonminimal_bool.stderr +++ b/tests/ui/nonminimal_bool.stderr @@ -114,5 +114,29 @@ error: this boolean expression can be simplified LL | if matches!(true, true) && true { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(true, true)` -error: aborting due to 13 previous errors +error: this boolean expression can be simplified + --> $DIR/nonminimal_bool.rs:162:8 + | +LL | if !(12 == a) {} + | ^^^^^^^^^^ help: try: `12 != a` + +error: this boolean expression can be simplified + --> $DIR/nonminimal_bool.rs:163:8 + | +LL | if !(a == 12) {} + | ^^^^^^^^^^ help: try: `a != 12` + +error: this boolean expression can be simplified + --> $DIR/nonminimal_bool.rs:164:8 + | +LL | if !(12 != a) {} + | ^^^^^^^^^^ help: try: `12 == a` + +error: this boolean expression can be simplified + --> $DIR/nonminimal_bool.rs:165:8 + | +LL | if !(a != 12) {} + | ^^^^^^^^^^ help: try: `a == 12` + +error: aborting due to 17 previous errors |
