Refactor if_not_else:

* Check HIR tree first.
* Merge lint calls.
This commit is contained in:
Jason Newcomb 2024-06-11 14:47:29 -04:00
parent 776a5238b7
commit b26b820f3f

View File

@ -56,44 +56,33 @@ fn is_zero_const(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
} }
impl LateLintPass<'_> for IfNotElse { impl LateLintPass<'_> for IfNotElse {
fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'_>, e: &Expr<'_>) {
// While loops will be desugared to ExprKind::If. This will cause the lint to fire. if let ExprKind::If(cond, _, Some(els)) = e.kind
// To fix this, return early if this span comes from a macro or desugaring. && let ExprKind::DropTemps(cond) = cond.kind
if item.span.from_expansion() { && let ExprKind::Block(..) = els.kind
return; {
} let (msg, help) = match cond.kind {
if let ExprKind::If(cond, _, Some(els)) = item.kind { ExprKind::Unary(UnOp::Not, _) => (
if let ExprKind::Block(..) = els.kind { "unnecessary boolean `not` operation",
// Disable firing the lint in "else if" expressions. "remove the `!` and swap the blocks of the `if`/`else`",
if is_else_clause(cx.tcx, item) { ),
return; // Don't lint on `… != 0`, as these are likely to be bit tests.
} // For example, `if foo & 0x0F00 != 0 { … } else { … }` is already in the "proper" order.
ExprKind::Binary(op, _, rhs) if op.node == BinOpKind::Ne && !is_zero_const(rhs, cx) => (
"unnecessary `!=` operation",
"change to `==` and swap the blocks of the `if`/`else`",
),
_ => return,
};
match cond.peel_drop_temps().kind { // `from_expansion` will also catch `while` loops which appear in the HIR as:
ExprKind::Unary(UnOp::Not, _) => { // ```rust
span_lint_and_help( // loop {
cx, // if cond { ... } else { break; }
IF_NOT_ELSE, // }
item.span, // ```
"unnecessary boolean `not` operation", if !e.span.from_expansion() && !is_else_clause(cx.tcx, e) {
None, span_lint_and_help(cx, IF_NOT_ELSE, e.span, msg, None, help);
"remove the `!` and swap the blocks of the `if`/`else`",
);
},
ExprKind::Binary(ref kind, _, lhs) if kind.node == BinOpKind::Ne && !is_zero_const(lhs, cx) => {
// Disable firing the lint on `… != 0`, as these are likely to be bit tests.
// For example, `if foo & 0x0F00 != 0 { … } else { … }` already is in the "proper" order.
span_lint_and_help(
cx,
IF_NOT_ELSE,
item.span,
"unnecessary `!=` operation",
None,
"change to `==` and swap the blocks of the `if`/`else`",
);
},
_ => (),
}
} }
} }
} }