Skip to content

Commit bc00d7b

Browse files
committed
Auto merge of rust-lang#12845 - cookie-s:dedup-boolmethods-diags, r=Jarcho
Dedup nonminimal_bool_methods diags Relates to rust-lang#12379 Fix `nonminimal_bool` lint so that it doesn't check the same span multiple times. `NotSimplificationVisitor` was called for each expression from `NonminimalBoolVisitor` whereas `NotSimplificationVisitor` also recursively checked all expressions. --- changelog: [`nonminimal_bool`]: Fix duplicate diagnostics
2 parents 10d1f32 + b6350e9 commit bc00d7b

File tree

4 files changed

+36
-49
lines changed

4 files changed

+36
-49
lines changed

clippy_lints/src/booleans.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,25 @@ fn check_inverted_bool_in_condition(
174174
);
175175
}
176176

177+
fn check_simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) {
178+
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind
179+
&& !expr.span.from_expansion()
180+
&& !inner.span.from_expansion()
181+
&& let Some(suggestion) = simplify_not(cx, inner)
182+
&& cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, expr.hir_id).0 != Level::Allow
183+
{
184+
span_lint_and_sugg(
185+
cx,
186+
NONMINIMAL_BOOL,
187+
expr.span,
188+
"this boolean expression can be simplified",
189+
"try",
190+
suggestion,
191+
Applicability::MachineApplicable,
192+
);
193+
}
194+
}
195+
177196
struct NonminimalBoolVisitor<'a, 'tcx> {
178197
cx: &'a LateContext<'tcx>,
179198
}
@@ -542,8 +561,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
542561
}
543562
};
544563
if improvements.is_empty() {
545-
let mut visitor = NotSimplificationVisitor { cx: self.cx };
546-
visitor.visit_expr(e);
564+
check_simplify_not(self.cx, e);
547565
} else {
548566
nonminimal_bool_lint(
549567
improvements
@@ -586,30 +604,3 @@ fn implements_ord(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
586604
.get_diagnostic_item(sym::Ord)
587605
.map_or(false, |id| implements_trait(cx, ty, id, &[]))
588606
}
589-
590-
struct NotSimplificationVisitor<'a, 'tcx> {
591-
cx: &'a LateContext<'tcx>,
592-
}
593-
594-
impl<'a, 'tcx> Visitor<'tcx> for NotSimplificationVisitor<'a, 'tcx> {
595-
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
596-
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind
597-
&& !expr.span.from_expansion()
598-
&& !inner.span.from_expansion()
599-
&& let Some(suggestion) = simplify_not(self.cx, inner)
600-
&& self.cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, expr.hir_id).0 != Level::Allow
601-
{
602-
span_lint_and_sugg(
603-
self.cx,
604-
NONMINIMAL_BOOL,
605-
expr.span,
606-
"this boolean expression can be simplified",
607-
"try",
608-
suggestion,
609-
Applicability::MachineApplicable,
610-
);
611-
}
612-
613-
walk_expr(self, expr);
614-
}
615-
}

tests/ui/nonminimal_bool_methods.fixed

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@compile-flags: -Zdeduplicate-diagnostics=yes
2-
31
#![allow(unused, clippy::diverging_sub_expression, clippy::needless_if)]
42
#![warn(clippy::nonminimal_bool)]
53

tests/ui/nonminimal_bool_methods.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@compile-flags: -Zdeduplicate-diagnostics=yes
2-
31
#![allow(unused, clippy::diverging_sub_expression, clippy::needless_if)]
42
#![warn(clippy::nonminimal_bool)]
53

tests/ui/nonminimal_bool_methods.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this boolean expression can be simplified
2-
--> tests/ui/nonminimal_bool_methods.rs:10:13
2+
--> tests/ui/nonminimal_bool_methods.rs:8:13
33
|
44
LL | let _ = !a.is_some();
55
| ^^^^^^^^^^^^ help: try: `a.is_none()`
@@ -8,91 +8,91 @@ LL | let _ = !a.is_some();
88
= help: to override `-D warnings` add `#[allow(clippy::nonminimal_bool)]`
99

1010
error: this boolean expression can be simplified
11-
--> tests/ui/nonminimal_bool_methods.rs:12:13
11+
--> tests/ui/nonminimal_bool_methods.rs:10:13
1212
|
1313
LL | let _ = !a.is_none();
1414
| ^^^^^^^^^^^^ help: try: `a.is_some()`
1515

1616
error: this boolean expression can be simplified
17-
--> tests/ui/nonminimal_bool_methods.rs:14:13
17+
--> tests/ui/nonminimal_bool_methods.rs:12:13
1818
|
1919
LL | let _ = !b.is_err();
2020
| ^^^^^^^^^^^ help: try: `b.is_ok()`
2121

2222
error: this boolean expression can be simplified
23-
--> tests/ui/nonminimal_bool_methods.rs:16:13
23+
--> tests/ui/nonminimal_bool_methods.rs:14:13
2424
|
2525
LL | let _ = !b.is_ok();
2626
| ^^^^^^^^^^ help: try: `b.is_err()`
2727

2828
error: this boolean expression can be simplified
29-
--> tests/ui/nonminimal_bool_methods.rs:18:13
29+
--> tests/ui/nonminimal_bool_methods.rs:16:13
3030
|
3131
LL | let _ = !(a.is_some() && !c);
3232
| ^^^^^^^^^^^^^^^^^^^^ help: try: `a.is_none() || c`
3333

3434
error: this boolean expression can be simplified
35-
--> tests/ui/nonminimal_bool_methods.rs:19:13
35+
--> tests/ui/nonminimal_bool_methods.rs:17:13
3636
|
3737
LL | let _ = !(a.is_some() || !c);
3838
| ^^^^^^^^^^^^^^^^^^^^ help: try: `a.is_none() && c`
3939

4040
error: this boolean expression can be simplified
41-
--> tests/ui/nonminimal_bool_methods.rs:20:26
41+
--> tests/ui/nonminimal_bool_methods.rs:18:26
4242
|
4343
LL | let _ = !(!c ^ c) || !a.is_some();
4444
| ^^^^^^^^^^^^ help: try: `a.is_none()`
4545

4646
error: this boolean expression can be simplified
47-
--> tests/ui/nonminimal_bool_methods.rs:21:25
47+
--> tests/ui/nonminimal_bool_methods.rs:19:25
4848
|
4949
LL | let _ = (!c ^ c) || !a.is_some();
5050
| ^^^^^^^^^^^^ help: try: `a.is_none()`
5151

5252
error: this boolean expression can be simplified
53-
--> tests/ui/nonminimal_bool_methods.rs:22:23
53+
--> tests/ui/nonminimal_bool_methods.rs:20:23
5454
|
5555
LL | let _ = !c ^ c || !a.is_some();
5656
| ^^^^^^^^^^^^ help: try: `a.is_none()`
5757

5858
error: this boolean expression can be simplified
59-
--> tests/ui/nonminimal_bool_methods.rs:94:8
59+
--> tests/ui/nonminimal_bool_methods.rs:92:8
6060
|
6161
LL | if !res.is_ok() {}
6262
| ^^^^^^^^^^^^ help: try: `res.is_err()`
6363

6464
error: this boolean expression can be simplified
65-
--> tests/ui/nonminimal_bool_methods.rs:95:8
65+
--> tests/ui/nonminimal_bool_methods.rs:93:8
6666
|
6767
LL | if !res.is_err() {}
6868
| ^^^^^^^^^^^^^ help: try: `res.is_ok()`
6969

7070
error: this boolean expression can be simplified
71-
--> tests/ui/nonminimal_bool_methods.rs:98:8
71+
--> tests/ui/nonminimal_bool_methods.rs:96:8
7272
|
7373
LL | if !res.is_some() {}
7474
| ^^^^^^^^^^^^^^ help: try: `res.is_none()`
7575

7676
error: this boolean expression can be simplified
77-
--> tests/ui/nonminimal_bool_methods.rs:99:8
77+
--> tests/ui/nonminimal_bool_methods.rs:97:8
7878
|
7979
LL | if !res.is_none() {}
8080
| ^^^^^^^^^^^^^^ help: try: `res.is_some()`
8181

8282
error: this boolean expression can be simplified
83-
--> tests/ui/nonminimal_bool_methods.rs:115:8
83+
--> tests/ui/nonminimal_bool_methods.rs:113:8
8484
|
8585
LL | if !(a as u64 >= b) {}
8686
| ^^^^^^^^^^^^^^^^ help: try: `(a as u64) < b`
8787

8888
error: this boolean expression can be simplified
89-
--> tests/ui/nonminimal_bool_methods.rs:116:8
89+
--> tests/ui/nonminimal_bool_methods.rs:114:8
9090
|
9191
LL | if !((a as u64) >= b) {}
9292
| ^^^^^^^^^^^^^^^^^^ help: try: `(a as u64) < b`
9393

9494
error: this boolean expression can be simplified
95-
--> tests/ui/nonminimal_bool_methods.rs:117:8
95+
--> tests/ui/nonminimal_bool_methods.rs:115:8
9696
|
9797
LL | if !(a as u64 <= b) {}
9898
| ^^^^^^^^^^^^^^^^ help: try: `a as u64 > b`

0 commit comments

Comments
 (0)