Skip to content

Commit 7fe83ed

Browse files
committed
Auto merge of #10588 - blyxyas:fix-allow_nonminimal_bool, r=llogiq
Fix `nonminimal_bool` `#[allow]` attributes. Closes #10435 changelog: [`nonminimal_bool`]: Fix false-positive where the lint ignore `#[allow]` attributes. r? `@llogiq`
2 parents 8f0ba1f + 36047b0 commit 7fe83ed

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

clippy_lints/src/booleans.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::ast::LitKind;
77
use rustc_errors::Applicability;
88
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
99
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
10-
use rustc_lint::{LateContext, LateLintPass};
10+
use rustc_lint::{LateContext, LateLintPass, Level};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
1212
use rustc_span::def_id::LocalDefId;
1313
use rustc_span::source_map::Span;
@@ -430,23 +430,25 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
430430
}
431431
}
432432
let nonminimal_bool_lint = |suggestions: Vec<_>| {
433-
span_lint_hir_and_then(
434-
self.cx,
435-
NONMINIMAL_BOOL,
436-
e.hir_id,
437-
e.span,
438-
"this boolean expression can be simplified",
439-
|diag| {
440-
diag.span_suggestions(
441-
e.span,
442-
"try",
443-
suggestions.into_iter(),
444-
// nonminimal_bool can produce minimal but
445-
// not human readable expressions (#3141)
446-
Applicability::Unspecified,
447-
);
448-
},
449-
);
433+
if self.cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, e.hir_id).0 != Level::Allow {
434+
span_lint_hir_and_then(
435+
self.cx,
436+
NONMINIMAL_BOOL,
437+
e.hir_id,
438+
e.span,
439+
"this boolean expression can be simplified",
440+
|diag| {
441+
diag.span_suggestions(
442+
e.span,
443+
"try",
444+
suggestions.into_iter(),
445+
// nonminimal_bool can produce minimal but
446+
// not human readable expressions (#3141)
447+
Applicability::Unspecified,
448+
);
449+
},
450+
);
451+
}
450452
};
451453
if improvements.is_empty() {
452454
let mut visitor = NotSimplificationVisitor { cx: self.cx };
@@ -498,6 +500,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NotSimplificationVisitor<'a, 'tcx> {
498500
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind &&
499501
!inner.span.from_expansion() &&
500502
let Some(suggestion) = simplify_not(self.cx, inner)
503+
&& self.cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, expr.hir_id).0 != Level::Allow
501504
{
502505
span_lint_and_sugg(
503506
self.cx,

tests/ui/nonminimal_bool.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,21 @@ fn issue_10523_2() {
9292
}
9393
if a!() {}
9494
}
95+
96+
fn issue_10435() {
97+
let x = vec![0];
98+
let y = vec![1];
99+
let z = vec![2];
100+
101+
// vvv Should not lint
102+
#[allow(clippy::nonminimal_bool)]
103+
if !x.is_empty() && !(y.is_empty() || z.is_empty()) {
104+
println!("{}", line!());
105+
}
106+
107+
// vvv Should not lint (#10435 talks about a bug where it lints)
108+
#[allow(clippy::nonminimal_bool)]
109+
if !(x == [0]) {
110+
println!("{}", line!());
111+
}
112+
}

0 commit comments

Comments
 (0)