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_errors::Applicability;
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}; use rustc_lint::{LateContext, LateLintPass, Level};
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;
@ -430,23 +430,25 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
} }
} }
let nonminimal_bool_lint = |suggestions: Vec<_>| { let nonminimal_bool_lint = |suggestions: Vec<_>| {
span_lint_hir_and_then( if self.cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, e.hir_id).0 != Level::Allow {
self.cx, span_lint_hir_and_then(
NONMINIMAL_BOOL, self.cx,
e.hir_id, NONMINIMAL_BOOL,
e.span, e.hir_id,
"this boolean expression can be simplified", e.span,
|diag| { "this boolean expression can be simplified",
diag.span_suggestions( |diag| {
e.span, diag.span_suggestions(
"try", e.span,
suggestions.into_iter(), "try",
// nonminimal_bool can produce minimal but suggestions.into_iter(),
// not human readable expressions (#3141) // nonminimal_bool can produce minimal but
Applicability::Unspecified, // not human readable expressions (#3141)
); Applicability::Unspecified,
}, );
); },
);
}
}; };
if improvements.is_empty() { if improvements.is_empty() {
let mut visitor = NotSimplificationVisitor { cx: self.cx }; 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 && if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind &&
!inner.span.from_expansion() && !inner.span.from_expansion() &&
let Some(suggestion) = simplify_not(self.cx, inner) 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( span_lint_and_sugg(
self.cx, self.cx,

View File

@ -92,3 +92,21 @@ fn issue_10523_2() {
} }
if a!() {} 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!());
}
}