change booleans file and update tests

This commit is contained in:
disco07 2023-05-31 00:17:26 +02:00
parent d3534a6521
commit e4927f98fa
3 changed files with 20 additions and 40 deletions

View File

@ -8,12 +8,10 @@
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor}; use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp}; use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
use rustc_lint::{LateContext, LateLintPass, Level}; use rustc_lint::{LateContext, LateLintPass, Level};
use rustc_middle::ty::{self, Ty};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId; use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::sym; use rustc_span::sym;
use rustc_span::symbol::Ident;
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### What it does
@ -90,28 +88,6 @@ fn check_fn(
NonminimalBoolVisitor { cx }.visit_body(body); NonminimalBoolVisitor { cx }.visit_body(body);
} }
} }
fn is_impl_not_trait_with_bool_out<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
cx.tcx
.lang_items()
.not_trait()
.filter(|trait_id| implements_trait(cx, ty, *trait_id, &[]))
.and_then(|trait_id| {
cx.tcx.associated_items(trait_id).find_by_name_and_kind(
cx.tcx,
Ident::from_str("Output"),
ty::AssocKind::Type,
trait_id,
)
})
.map_or(false, |assoc_item| {
let proj = cx.tcx.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(ty, []));
let nty = cx.tcx.normalize_erasing_regions(cx.param_env, proj);
nty.is_bool()
})
}
struct NonminimalBoolVisitor<'a, 'tcx> { struct NonminimalBoolVisitor<'a, 'tcx> {
cx: &'a LateContext<'tcx>, cx: &'a LateContext<'tcx>,
} }
@ -496,12 +472,10 @@ fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
self.bool_expr(e); self.bool_expr(e);
}, },
ExprKind::Unary(UnOp::Not, inner) => { ExprKind::Unary(UnOp::Not, inner) => {
if let ExprKind::Unary(UnOp::Not, ex) = inner.kind { if let ExprKind::Unary(UnOp::Not, ex) = inner.kind &&
let ty = self.cx.typeck_results().expr_ty(ex); !self.cx.typeck_results().node_types()[ex.hir_id].is_bool() {
if is_impl_not_trait_with_bool_out(self.cx, ty) {
return; return;
} }
}
if self.cx.typeck_results().node_types()[inner.hir_id].is_bool() { if self.cx.typeck_results().node_types()[inner.hir_id].is_bool() {
self.bool_expr(e); self.bool_expr(e);
} }

View File

@ -10,7 +10,6 @@ fn main() {
let e: bool = unimplemented!(); let e: bool = unimplemented!();
let _ = !true; let _ = !true;
let _ = !false; let _ = !false;
// vvv Should not lint
let _ = !!a; let _ = !!a;
let _ = false || a; let _ = false || a;
// don't lint on cfgs // don't lint on cfgs
@ -55,6 +54,7 @@ fn f(_i: u32, _j: u32) -> u32 {
fn check_expect() { fn check_expect() {
let a: bool = unimplemented!(); let a: bool = unimplemented!();
#[expect(clippy::nonminimal_bool)]
let _ = !!a; let _ = !!a;
} }

View File

@ -13,31 +13,37 @@ LL | let _ = !false;
| ^^^^^^ help: try: `true` | ^^^^^^ help: try: `true`
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:15:13 --> $DIR/nonminimal_bool.rs:13:13
|
LL | let _ = !!a;
| ^^^ help: try: `a`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:14: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
--> $DIR/nonminimal_bool.rs:19:13 --> $DIR/nonminimal_bool.rs:18: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
--> $DIR/nonminimal_bool.rs:20:13 --> $DIR/nonminimal_bool.rs:19: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
--> $DIR/nonminimal_bool.rs:21:13 --> $DIR/nonminimal_bool.rs:20: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
--> $DIR/nonminimal_bool.rs:29:13 --> $DIR/nonminimal_bool.rs:28:13
| |
LL | let _ = a == b && c == 5 && a == b; LL | let _ = a == b && c == 5 && a == b;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -50,7 +56,7 @@ LL | let _ = a == b && c == 5;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:30:13 --> $DIR/nonminimal_bool.rs:29:13
| |
LL | let _ = a == b || c == 5 || a == b; LL | let _ = a == b || c == 5 || a == b;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -63,7 +69,7 @@ LL | let _ = a == b || c == 5;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:31:13 --> $DIR/nonminimal_bool.rs:30:13
| |
LL | let _ = a == b && c == 5 && b == a; LL | let _ = a == b && c == 5 && b == a;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -76,7 +82,7 @@ LL | let _ = a == b && c == 5;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:32:13 --> $DIR/nonminimal_bool.rs:31:13
| |
LL | let _ = a != b || !(a != b || c == d); LL | let _ = a != b || !(a != b || c == d);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -89,7 +95,7 @@ LL | let _ = a != b || c != d;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: this boolean expression can be simplified error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:33:13 --> $DIR/nonminimal_bool.rs:32:13
| |
LL | let _ = a != b && !(a != b && c == d); LL | let _ = a != b && !(a != b && c == d);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -107,5 +113,5 @@ error: this boolean expression can be simplified
LL | if matches!(true, true) && true { LL | if matches!(true, true) && true {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(true, true)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(true, true)`
error: aborting due to 12 previous errors error: aborting due to 13 previous errors