-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new lints: manual_and
and manual_or
#12832
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::visitors::for_each_expr_without_closures; | ||
use clippy_utils::{get_parent_expr, higher, peel_blocks}; | ||
use core::ops::ControlFlow; | ||
use rustc_ast::ast::LitKind; | ||
use rustc_ast::BinOpKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Detects `if`-then-`else` that can be replaced with `&&`. | ||
/// | ||
/// ### Why is this bad? | ||
/// `&&` is simpler than `if`-then-`else`. | ||
/// | ||
/// ### Example | ||
/// ```ignore | ||
/// if a { | ||
/// b | ||
/// } else { | ||
/// false | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```ignore | ||
/// a && b | ||
/// ``` | ||
#[clippy::version = "1.80.0"] | ||
pub MANUAL_AND, | ||
complexity, | ||
"this `if`-then-`else` that can be replaced with `&&`." | ||
} | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Detects `if`-then-`else` that can be replaced with `||`. | ||
/// | ||
/// ### Why is this bad? | ||
/// `||` is simpler than `if`-then-`else`. | ||
/// | ||
/// ### Example | ||
/// ```ignore | ||
/// if a { | ||
/// true | ||
/// } else { | ||
/// b | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```ignore | ||
/// a || b | ||
/// ``` | ||
#[clippy::version = "1.80.0"] | ||
pub MANUAL_OR, | ||
complexity, | ||
"this `if`-then-`else` expression can be simplified with `||`" | ||
} | ||
|
||
declare_lint_pass!(ManualAndOr => [MANUAL_AND, MANUAL_OR]); | ||
|
||
fn extract_final_expression_snippet<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<String> { | ||
if let ExprKind::Block(block, _) = expr.kind { | ||
if let Some(final_expr) = block.expr { | ||
return cx.sess().source_map().span_to_snippet(final_expr.span).ok(); | ||
} | ||
} | ||
cx.sess().source_map().span_to_snippet(expr.span).ok() | ||
} | ||
|
||
fn fetch_bool_expr(expr: &Expr<'_>) -> Option<bool> { | ||
if let ExprKind::Lit(lit_ptr) = peel_blocks(expr).kind { | ||
if let LitKind::Bool(value) = lit_ptr.node { | ||
return Some(value); | ||
} | ||
} | ||
None | ||
} | ||
|
||
fn contains_or(cond: &Expr<'_>) -> bool { | ||
for_each_expr_without_closures(cond, |e| { | ||
if let ExprKind::Binary(ref n, _, _) = e.kind { | ||
if n.node == BinOpKind::Or { | ||
ControlFlow::Break(()) | ||
} else { | ||
ControlFlow::Continue(()) | ||
} | ||
} else { | ||
ControlFlow::Continue(()) | ||
} | ||
}) | ||
.is_some() | ||
} | ||
|
||
fn check_and<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, cond: &Expr<'tcx>, then: &Expr<'tcx>) { | ||
if let Some(parent) = get_parent_expr(cx, expr) { | ||
if let ExprKind::If(_, _, _) = parent.kind { | ||
return; | ||
} | ||
} | ||
if contains_or(cond) || contains_or(then) || fetch_bool_expr(then).is_some() { | ||
return; | ||
} | ||
Comment on lines
+103
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The precedence of the context the let _ = c == if a { b } else { false };
// becomes
let _ = c == a && b;
// AKA
let _ = (c == a) && b; |
||
if match then.kind { | ||
ExprKind::Block(block, _) => !block.stmts.is_empty(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should check that the type of if a {
return
} else {
false
} The lint isn't semantically incorrect here but |
||
_ => false, | ||
} { | ||
return; | ||
} | ||
|
||
let applicability = Applicability::MachineApplicable; | ||
let cond_snippet = cx | ||
.sess() | ||
.source_map() | ||
.span_to_snippet(cond.span) | ||
.unwrap_or_else(|_| "..".to_string()); | ||
|
||
let then_snippet = extract_final_expression_snippet(cx, then).unwrap_or_else(|| "..".to_string()); | ||
let suggestion = format!("{cond_snippet} && {then_snippet}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll want to handle when the if a {
// Comment
b
} else {
false
} Preserving them is an option but it might be better to not lint here, if the comment is large it would be weird to have in the middle of an There's |
||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_AND, | ||
expr.span, | ||
"this `if`-then-`else` expression can be simplified with `&&`", | ||
"try", | ||
suggestion, | ||
applicability, | ||
); | ||
} | ||
|
||
fn check_or<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, cond: &Expr<'tcx>, else_expr: &Expr<'tcx>) { | ||
if matches!(else_expr.kind, ExprKind::If(..)) || fetch_bool_expr(else_expr).is_some() { | ||
return; | ||
} | ||
if match else_expr.kind { | ||
ExprKind::Block(block, _) => !block.stmts.is_empty(), | ||
_ => false, | ||
} { | ||
return; | ||
} | ||
|
||
let applicability = Applicability::MachineApplicable; | ||
let cond_snippet = cx | ||
.sess() | ||
.source_map() | ||
.span_to_snippet(cond.span) | ||
.unwrap_or_else(|_| "..".to_string()); | ||
|
||
let else_snippet = extract_final_expression_snippet(cx, else_expr).unwrap_or_else(|| "..".to_string()); | ||
let suggestion = format!("{cond_snippet} || {else_snippet}"); | ||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_OR, | ||
expr.span, | ||
"this `if`-then-`else` expression can be simplified with `||`", | ||
"try", | ||
suggestion, | ||
applicability, | ||
); | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for ManualAndOr { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { | ||
if let Some(higher::If { | ||
cond, | ||
then, | ||
r#else: Some(else_expr), | ||
}) = higher::If::hir(expr) | ||
{ | ||
if let Some(false) = fetch_bool_expr(else_expr) { | ||
check_and(cx, expr, cond, then); | ||
} else if let Some(true) = fetch_bool_expr(then) { | ||
check_or(cx, expr, cond, else_expr); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can use
snippet_opt
here & elsewhere