Do not propose to simplify a not expression coming from a macro

This commit is contained in:
Samuel "Sam" Tardieu 2023-03-22 00:34:35 +01:00
parent 6cebe58dfe
commit b138bb587b
2 changed files with 42 additions and 12 deletions

View File

@ -495,18 +495,19 @@ struct NotSimplificationVisitor<'a, 'tcx> {
impl<'a, 'tcx> Visitor<'tcx> for NotSimplificationVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for NotSimplificationVisitor<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) { fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind { if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind &&
if let Some(suggestion) = simplify_not(self.cx, inner) { !inner.span.from_expansion() &&
span_lint_and_sugg( let Some(suggestion) = simplify_not(self.cx, inner)
self.cx, {
NONMINIMAL_BOOL, span_lint_and_sugg(
expr.span, self.cx,
"this boolean expression can be simplified", NONMINIMAL_BOOL,
"try", expr.span,
suggestion, "this boolean expression can be simplified",
Applicability::MachineApplicable, "try",
); suggestion,
} Applicability::MachineApplicable,
);
} }
walk_expr(self, expr); walk_expr(self, expr);

View File

@ -63,3 +63,32 @@ fn issue9428() {
println!("foo"); println!("foo");
} }
} }
fn issue_10523() {
macro_rules! a {
($v:expr) => {
$v.is_some()
};
}
let x: Option<u32> = None;
if !a!(x) {}
}
fn issue_10523_1() {
macro_rules! a {
($v:expr) => {
!$v.is_some()
};
}
let x: Option<u32> = None;
if a!(x) {}
}
fn issue_10523_2() {
macro_rules! a {
() => {
!None::<u32>.is_some()
};
}
if a!() {}
}