Skip to content

Commit 1d1e723

Browse files
committed
Auto merge of rust-lang#10527 - samueltardieu:issue-10523, r=dswij
Do not propose to simplify a not expression coming from a macro Fixes rust-lang#10523 changelog: FP [`nonminimal_bool`]: do not propose to change code coming from a macro
2 parents 5839621 + b138bb5 commit 1d1e723

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

clippy_lints/src/booleans.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -495,18 +495,19 @@ struct NotSimplificationVisitor<'a, 'tcx> {
495495

496496
impl<'a, 'tcx> Visitor<'tcx> for NotSimplificationVisitor<'a, 'tcx> {
497497
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
498-
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind {
499-
if let Some(suggestion) = simplify_not(self.cx, inner) {
500-
span_lint_and_sugg(
501-
self.cx,
502-
NONMINIMAL_BOOL,
503-
expr.span,
504-
"this boolean expression can be simplified",
505-
"try",
506-
suggestion,
507-
Applicability::MachineApplicable,
508-
);
509-
}
498+
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind &&
499+
!inner.span.from_expansion() &&
500+
let Some(suggestion) = simplify_not(self.cx, inner)
501+
{
502+
span_lint_and_sugg(
503+
self.cx,
504+
NONMINIMAL_BOOL,
505+
expr.span,
506+
"this boolean expression can be simplified",
507+
"try",
508+
suggestion,
509+
Applicability::MachineApplicable,
510+
);
510511
}
511512

512513
walk_expr(self, expr);

tests/ui/nonminimal_bool.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,32 @@ fn issue9428() {
6363
println!("foo");
6464
}
6565
}
66+
67+
fn issue_10523() {
68+
macro_rules! a {
69+
($v:expr) => {
70+
$v.is_some()
71+
};
72+
}
73+
let x: Option<u32> = None;
74+
if !a!(x) {}
75+
}
76+
77+
fn issue_10523_1() {
78+
macro_rules! a {
79+
($v:expr) => {
80+
!$v.is_some()
81+
};
82+
}
83+
let x: Option<u32> = None;
84+
if a!(x) {}
85+
}
86+
87+
fn issue_10523_2() {
88+
macro_rules! a {
89+
() => {
90+
!None::<u32>.is_some()
91+
};
92+
}
93+
if a!() {}
94+
}

0 commit comments

Comments
 (0)