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`
This commit is contained in:
bors 2023-04-02 22:12:46 +00:00
commit 7fe83edc11
2 changed files with 39 additions and 18 deletions

View File

@ -7,7 +7,7 @@ use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_lint::{LateContext, LateLintPass, Level};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span;
@ -430,6 +430,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
}
}
let nonminimal_bool_lint = |suggestions: Vec<_>| {
if self.cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, e.hir_id).0 != Level::Allow {
span_lint_hir_and_then(
self.cx,
NONMINIMAL_BOOL,
@ -447,6 +448,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
);
},
);
}
};
if improvements.is_empty() {
let mut visitor = NotSimplificationVisitor { cx: self.cx };
@ -498,6 +500,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NotSimplificationVisitor<'a, 'tcx> {
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind &&
!inner.span.from_expansion() &&
let Some(suggestion) = simplify_not(self.cx, inner)
&& self.cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, expr.hir_id).0 != Level::Allow
{
span_lint_and_sugg(
self.cx,

View File

@ -92,3 +92,21 @@ fn issue_10523_2() {
}
if a!() {}
}
fn issue_10435() {
let x = vec![0];
let y = vec![1];
let z = vec![2];
// vvv Should not lint
#[allow(clippy::nonminimal_bool)]
if !x.is_empty() && !(y.is_empty() || z.is_empty()) {
println!("{}", line!());
}
// vvv Should not lint (#10435 talks about a bug where it lints)
#[allow(clippy::nonminimal_bool)]
if !(x == [0]) {
println!("{}", line!());
}
}