Skip to content

Commit e4927f9

Browse files
committed
change booleans file and update tests
1 parent d3534a6 commit e4927f9

File tree

3 files changed

+20
-40
lines changed

3 files changed

+20
-40
lines changed

clippy_lints/src/booleans.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ use rustc_errors::Applicability;
88
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
99
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
1010
use rustc_lint::{LateContext, LateLintPass, Level};
11-
use rustc_middle::ty::{self, Ty};
1211
use rustc_session::{declare_lint_pass, declare_tool_lint};
1312
use rustc_span::def_id::LocalDefId;
1413
use rustc_span::source_map::Span;
1514
use rustc_span::sym;
16-
use rustc_span::symbol::Ident;
1715

1816
declare_clippy_lint! {
1917
/// ### What it does
@@ -90,28 +88,6 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
9088
NonminimalBoolVisitor { cx }.visit_body(body);
9189
}
9290
}
93-
94-
fn is_impl_not_trait_with_bool_out<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
95-
cx.tcx
96-
.lang_items()
97-
.not_trait()
98-
.filter(|trait_id| implements_trait(cx, ty, *trait_id, &[]))
99-
.and_then(|trait_id| {
100-
cx.tcx.associated_items(trait_id).find_by_name_and_kind(
101-
cx.tcx,
102-
Ident::from_str("Output"),
103-
ty::AssocKind::Type,
104-
trait_id,
105-
)
106-
})
107-
.map_or(false, |assoc_item| {
108-
let proj = cx.tcx.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(ty, []));
109-
let nty = cx.tcx.normalize_erasing_regions(cx.param_env, proj);
110-
111-
nty.is_bool()
112-
})
113-
}
114-
11591
struct NonminimalBoolVisitor<'a, 'tcx> {
11692
cx: &'a LateContext<'tcx>,
11793
}
@@ -496,11 +472,9 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
496472
self.bool_expr(e);
497473
},
498474
ExprKind::Unary(UnOp::Not, inner) => {
499-
if let ExprKind::Unary(UnOp::Not, ex) = inner.kind {
500-
let ty = self.cx.typeck_results().expr_ty(ex);
501-
if is_impl_not_trait_with_bool_out(self.cx, ty) {
502-
return;
503-
}
475+
if let ExprKind::Unary(UnOp::Not, ex) = inner.kind &&
476+
!self.cx.typeck_results().node_types()[ex.hir_id].is_bool() {
477+
return;
504478
}
505479
if self.cx.typeck_results().node_types()[inner.hir_id].is_bool() {
506480
self.bool_expr(e);

tests/ui/nonminimal_bool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ fn main() {
1010
let e: bool = unimplemented!();
1111
let _ = !true;
1212
let _ = !false;
13-
// vvv Should not lint
1413
let _ = !!a;
1514
let _ = false || a;
1615
// don't lint on cfgs
@@ -55,6 +54,7 @@ fn issue4548() {
5554

5655
fn check_expect() {
5756
let a: bool = unimplemented!();
57+
#[expect(clippy::nonminimal_bool)]
5858
let _ = !!a;
5959
}
6060

tests/ui/nonminimal_bool.stderr

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,37 @@ LL | let _ = !false;
1313
| ^^^^^^ help: try: `true`
1414

1515
error: this boolean expression can be simplified
16-
--> $DIR/nonminimal_bool.rs:15:13
16+
--> $DIR/nonminimal_bool.rs:13:13
17+
|
18+
LL | let _ = !!a;
19+
| ^^^ help: try: `a`
20+
21+
error: this boolean expression can be simplified
22+
--> $DIR/nonminimal_bool.rs:14:13
1723
|
1824
LL | let _ = false || a;
1925
| ^^^^^^^^^^ help: try: `a`
2026

2127
error: this boolean expression can be simplified
22-
--> $DIR/nonminimal_bool.rs:19:13
28+
--> $DIR/nonminimal_bool.rs:18:13
2329
|
2430
LL | let _ = !(!a && b);
2531
| ^^^^^^^^^^ help: try: `a || !b`
2632

2733
error: this boolean expression can be simplified
28-
--> $DIR/nonminimal_bool.rs:20:13
34+
--> $DIR/nonminimal_bool.rs:19:13
2935
|
3036
LL | let _ = !(!a || b);
3137
| ^^^^^^^^^^ help: try: `a && !b`
3238

3339
error: this boolean expression can be simplified
34-
--> $DIR/nonminimal_bool.rs:21:13
40+
--> $DIR/nonminimal_bool.rs:20:13
3541
|
3642
LL | let _ = !a && !(b && c);
3743
| ^^^^^^^^^^^^^^^ help: try: `!(a || b && c)`
3844

3945
error: this boolean expression can be simplified
40-
--> $DIR/nonminimal_bool.rs:29:13
46+
--> $DIR/nonminimal_bool.rs:28:13
4147
|
4248
LL | let _ = a == b && c == 5 && a == b;
4349
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -50,7 +56,7 @@ LL | let _ = a == b && c == 5;
5056
| ~~~~~~~~~~~~~~~~
5157

5258
error: this boolean expression can be simplified
53-
--> $DIR/nonminimal_bool.rs:30:13
59+
--> $DIR/nonminimal_bool.rs:29:13
5460
|
5561
LL | let _ = a == b || c == 5 || a == b;
5662
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -63,7 +69,7 @@ LL | let _ = a == b || c == 5;
6369
| ~~~~~~~~~~~~~~~~
6470

6571
error: this boolean expression can be simplified
66-
--> $DIR/nonminimal_bool.rs:31:13
72+
--> $DIR/nonminimal_bool.rs:30:13
6773
|
6874
LL | let _ = a == b && c == 5 && b == a;
6975
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -76,7 +82,7 @@ LL | let _ = a == b && c == 5;
7682
| ~~~~~~~~~~~~~~~~
7783

7884
error: this boolean expression can be simplified
79-
--> $DIR/nonminimal_bool.rs:32:13
85+
--> $DIR/nonminimal_bool.rs:31:13
8086
|
8187
LL | let _ = a != b || !(a != b || c == d);
8288
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -89,7 +95,7 @@ LL | let _ = a != b || c != d;
8995
| ~~~~~~~~~~~~~~~~
9096

9197
error: this boolean expression can be simplified
92-
--> $DIR/nonminimal_bool.rs:33:13
98+
--> $DIR/nonminimal_bool.rs:32:13
9399
|
94100
LL | let _ = a != b && !(a != b && c == d);
95101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -107,5 +113,5 @@ error: this boolean expression can be simplified
107113
LL | if matches!(true, true) && true {
108114
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(true, true)`
109115

110-
error: aborting due to 12 previous errors
116+
error: aborting due to 13 previous errors
111117

0 commit comments

Comments
 (0)