Auto merge of #12401 - MarcusGrass:dedup-nonminimal-bool, r=blyxyas

Remove double expr lint

Related to #12379.

Previously the code manually checked nested binop exprs in unary exprs, but those were caught anyway by `check_expr`. Removed that code path, the path is used in the tests.

---

changelog: [`nonminimal_bool`] Remove duplicate output on nested Binops in Unary exprs.
This commit is contained in:
bors 2024-03-06 13:32:53 +00:00
commit a79db2aa51
3 changed files with 29 additions and 52 deletions

View File

@ -88,7 +88,6 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
match expr.kind { match expr.kind {
ExprKind::Unary(UnOp::Not, sub) => check_inverted_condition(cx, expr.span, sub),
// This check the case where an element in a boolean comparison is inverted, like: // This check the case where an element in a boolean comparison is inverted, like:
// //
// ``` // ```
@ -119,27 +118,6 @@ fn bin_op_eq_str(op: BinOpKind) -> Option<&'static str> {
} }
} }
fn check_inverted_condition(cx: &LateContext<'_>, expr_span: Span, sub_expr: &Expr<'_>) {
if !expr_span.from_expansion()
&& let ExprKind::Binary(op, left, right) = sub_expr.kind
&& let Some(left) = snippet_opt(cx, left.span)
&& let Some(right) = snippet_opt(cx, right.span)
{
let Some(op) = inverted_bin_op_eq_str(op.node) else {
return;
};
span_lint_and_sugg(
cx,
NONMINIMAL_BOOL,
expr_span,
"this boolean expression can be simplified",
"try",
format!("{left} {op} {right}",),
Applicability::MachineApplicable,
);
}
}
fn check_inverted_bool_in_condition( fn check_inverted_bool_in_condition(
cx: &LateContext<'_>, cx: &LateContext<'_>,
expr_span: Span, expr_span: Span,

View File

@ -1,5 +1,4 @@
//@no-rustfix: overlapping suggestions //@no-rustfix: overlapping suggestions
//@compile-flags: -Zdeduplicate-diagnostics=yes
#![feature(lint_reasons)] #![feature(lint_reasons)]
#![allow( #![allow(

View File

@ -1,5 +1,5 @@
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:20:13 --> tests/ui/nonminimal_bool.rs:19:13
| |
LL | let _ = !true; LL | let _ = !true;
| ^^^^^ help: try: `false` | ^^^^^ help: try: `false`
@ -8,43 +8,43 @@ LL | let _ = !true;
= help: to override `-D warnings` add `#[allow(clippy::nonminimal_bool)]` = help: to override `-D warnings` add `#[allow(clippy::nonminimal_bool)]`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:23:13 --> tests/ui/nonminimal_bool.rs:22:13
| |
LL | let _ = !false; LL | let _ = !false;
| ^^^^^^ help: try: `true` | ^^^^^^ help: try: `true`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:25:13 --> tests/ui/nonminimal_bool.rs:24:13
| |
LL | let _ = !!a; LL | let _ = !!a;
| ^^^ help: try: `a` | ^^^ help: try: `a`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:27:13 --> tests/ui/nonminimal_bool.rs:26:13
| |
LL | let _ = false || a; LL | let _ = false || a;
| ^^^^^^^^^^ help: try: `a` | ^^^^^^^^^^ help: try: `a`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:32:13 --> tests/ui/nonminimal_bool.rs:31:13
| |
LL | let _ = !(!a && b); LL | let _ = !(!a && b);
| ^^^^^^^^^^ help: try: `a || !b` | ^^^^^^^^^^ help: try: `a || !b`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:34:13 --> tests/ui/nonminimal_bool.rs:33:13
| |
LL | let _ = !(!a || b); LL | let _ = !(!a || b);
| ^^^^^^^^^^ help: try: `a && !b` | ^^^^^^^^^^ help: try: `a && !b`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:36:13 --> tests/ui/nonminimal_bool.rs:35:13
| |
LL | let _ = !a && !(b && c); LL | let _ = !a && !(b && c);
| ^^^^^^^^^^^^^^^ help: try: `!(a || b && c)` | ^^^^^^^^^^^^^^^ help: try: `!(a || b && c)`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:45:13 --> tests/ui/nonminimal_bool.rs:44:13
| |
LL | let _ = a == b && c == 5 && a == b; LL | let _ = a == b && c == 5 && a == b;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -57,7 +57,7 @@ LL | let _ = a == b && c == 5;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:47:13 --> tests/ui/nonminimal_bool.rs:46:13
| |
LL | let _ = a == b || c == 5 || a == b; LL | let _ = a == b || c == 5 || a == b;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -70,7 +70,7 @@ LL | let _ = a == b || c == 5;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:49:13 --> tests/ui/nonminimal_bool.rs:48:13
| |
LL | let _ = a == b && c == 5 && b == a; LL | let _ = a == b && c == 5 && b == a;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -83,7 +83,7 @@ LL | let _ = a == b && c == 5;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:51:13 --> tests/ui/nonminimal_bool.rs:50:13
| |
LL | let _ = a != b || !(a != b || c == d); LL | let _ = a != b || !(a != b || c == d);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -96,7 +96,7 @@ LL | let _ = a != b || c != d;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:53:13 --> tests/ui/nonminimal_bool.rs:52:13
| |
LL | let _ = a != b && !(a != b && c == d); LL | let _ = a != b && !(a != b && c == d);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -109,43 +109,43 @@ LL | let _ = a != b && c != d;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:84:8 --> tests/ui/nonminimal_bool.rs:83:8
| |
LL | if matches!(true, true) && true { LL | if matches!(true, true) && true {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(true, true)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(true, true)`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:164:8 --> tests/ui/nonminimal_bool.rs:163:8
| |
LL | if !(12 == a) {} LL | if !(12 == a) {}
| ^^^^^^^^^^ help: try: `12 != a` | ^^^^^^^^^^ help: try: `12 != a`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:165:8 --> tests/ui/nonminimal_bool.rs:164:8
| |
LL | if !(a == 12) {} LL | if !(a == 12) {}
| ^^^^^^^^^^ help: try: `a != 12` | ^^^^^^^^^^ help: try: `a != 12`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:166:8 --> tests/ui/nonminimal_bool.rs:165:8
| |
LL | if !(12 != a) {} LL | if !(12 != a) {}
| ^^^^^^^^^^ help: try: `12 == a` | ^^^^^^^^^^ help: try: `12 == a`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:167:8 --> tests/ui/nonminimal_bool.rs:166:8
| |
LL | if !(a != 12) {} LL | if !(a != 12) {}
| ^^^^^^^^^^ help: try: `a == 12` | ^^^^^^^^^^ help: try: `a == 12`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:171:8 --> tests/ui/nonminimal_bool.rs:170:8
| |
LL | if !b == true {} LL | if !b == true {}
| ^^^^^^^^^^ help: try: `b != true` | ^^^^^^^^^^ help: try: `b != true`
error: this comparison might be written more concisely error: this comparison might be written more concisely
--> tests/ui/nonminimal_bool.rs:171:8 --> tests/ui/nonminimal_bool.rs:170:8
| |
LL | if !b == true {} LL | if !b == true {}
| ^^^^^^^^^^ help: try simplifying it as shown: `b != true` | ^^^^^^^^^^ help: try simplifying it as shown: `b != true`
@ -154,61 +154,61 @@ LL | if !b == true {}
= help: to override `-D warnings` add `#[allow(clippy::bool_comparison)]` = help: to override `-D warnings` add `#[allow(clippy::bool_comparison)]`
error: equality checks against true are unnecessary error: equality checks against true are unnecessary
--> tests/ui/nonminimal_bool.rs:171:8 --> tests/ui/nonminimal_bool.rs:170:8
| |
LL | if !b == true {} LL | if !b == true {}
| ^^^^^^^^^^ help: try simplifying it as shown: `!b` | ^^^^^^^^^^ help: try simplifying it as shown: `!b`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:172:8 --> tests/ui/nonminimal_bool.rs:171:8
| |
LL | if !b != true {} LL | if !b != true {}
| ^^^^^^^^^^ help: try: `b == true` | ^^^^^^^^^^ help: try: `b == true`
error: inequality checks against true can be replaced by a negation error: inequality checks against true can be replaced by a negation
--> tests/ui/nonminimal_bool.rs:172:8 --> tests/ui/nonminimal_bool.rs:171:8
| |
LL | if !b != true {} LL | if !b != true {}
| ^^^^^^^^^^ help: try simplifying it as shown: `!(!b)` | ^^^^^^^^^^ help: try simplifying it as shown: `!(!b)`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:173:8 --> tests/ui/nonminimal_bool.rs:172:8
| |
LL | if true == !b {} LL | if true == !b {}
| ^^^^^^^^^^ help: try: `true != b` | ^^^^^^^^^^ help: try: `true != b`
error: this comparison might be written more concisely error: this comparison might be written more concisely
--> tests/ui/nonminimal_bool.rs:173:8 --> tests/ui/nonminimal_bool.rs:172:8
| |
LL | if true == !b {} LL | if true == !b {}
| ^^^^^^^^^^ help: try simplifying it as shown: `true != b` | ^^^^^^^^^^ help: try simplifying it as shown: `true != b`
error: equality checks against true are unnecessary error: equality checks against true are unnecessary
--> tests/ui/nonminimal_bool.rs:173:8 --> tests/ui/nonminimal_bool.rs:172:8
| |
LL | if true == !b {} LL | if true == !b {}
| ^^^^^^^^^^ help: try simplifying it as shown: `!b` | ^^^^^^^^^^ help: try simplifying it as shown: `!b`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:174:8 --> tests/ui/nonminimal_bool.rs:173:8
| |
LL | if true != !b {} LL | if true != !b {}
| ^^^^^^^^^^ help: try: `true == b` | ^^^^^^^^^^ help: try: `true == b`
error: inequality checks against true can be replaced by a negation error: inequality checks against true can be replaced by a negation
--> tests/ui/nonminimal_bool.rs:174:8 --> tests/ui/nonminimal_bool.rs:173:8
| |
LL | if true != !b {} LL | if true != !b {}
| ^^^^^^^^^^ help: try simplifying it as shown: `!(!b)` | ^^^^^^^^^^ help: try simplifying it as shown: `!(!b)`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:175:8 --> tests/ui/nonminimal_bool.rs:174:8
| |
LL | if !b == !c {} LL | if !b == !c {}
| ^^^^^^^^ help: try: `b == c` | ^^^^^^^^ help: try: `b == c`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:176:8 --> tests/ui/nonminimal_bool.rs:175:8
| |
LL | if !b != !c {} LL | if !b != !c {}
| ^^^^^^^^ help: try: `b != c` | ^^^^^^^^ help: try: `b != c`