-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add needless_if
lint
#10921
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
Merged
+839
−476
Merged
Add needless_if
lint
#10921
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26f5039
Add `needless_if` lint
Centri3 7ba9042
make cargo test pass
Centri3 b2bdc37
add description
Centri3 59bca09
don't lint on `if let`
Centri3 d989f43
Update needless_if.fixed
Centri3 108c04a
Stop visiting once it's found `Let`
Centri3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, is_from_proc_macro, source::snippet_with_applicability}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{ | ||
intravisit::{walk_expr, Visitor}, | ||
Expr, ExprKind, Node, | ||
}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for empty `if` branches with no else branch. | ||
/// | ||
/// ### Why is this bad? | ||
/// It can be entirely omitted, and often the condition too. | ||
/// | ||
/// ### Known issues | ||
/// This will usually only suggest to remove the `if` statement, not the condition. Other lints | ||
/// such as `no_effect` will take care of removing the condition if it's unnecessary. | ||
/// | ||
/// ### Example | ||
/// ```rust,ignore | ||
/// if really_expensive_condition(&i) {} | ||
/// if really_expensive_condition_with_side_effects(&mut i) {} | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust,ignore | ||
/// // <omitted> | ||
/// really_expensive_condition_with_side_effects(&mut i); | ||
/// ``` | ||
#[clippy::version = "1.72.0"] | ||
pub NEEDLESS_IF, | ||
complexity, | ||
"checks for empty if branches" | ||
} | ||
declare_lint_pass!(NeedlessIf => [NEEDLESS_IF]); | ||
|
||
impl LateLintPass<'_> for NeedlessIf { | ||
fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { | ||
if let ExprKind::If(if_expr, block, else_expr) = &expr.kind | ||
&& let ExprKind::Block(block, ..) = block.kind | ||
&& block.stmts.is_empty() | ||
&& block.expr.is_none() | ||
&& else_expr.is_none() | ||
&& !in_external_macro(cx.sess(), expr.span) | ||
{ | ||
// Ignore `else if` | ||
if let Some(parent_id) = cx.tcx.hir().opt_parent_id(expr.hir_id) | ||
&& let Some(Node::Expr(Expr { | ||
kind: ExprKind::If(_, _, Some(else_expr)), | ||
.. | ||
})) = cx.tcx.hir().find(parent_id) | ||
&& else_expr.hir_id == expr.hir_id | ||
{ | ||
return; | ||
} | ||
|
||
if is_any_if_let(if_expr) || is_from_proc_macro(cx, expr) { | ||
return; | ||
} | ||
|
||
let mut app = Applicability::MachineApplicable; | ||
let snippet = snippet_with_applicability(cx, if_expr.span, "{ ... }", &mut app); | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
NEEDLESS_IF, | ||
expr.span, | ||
"this `if` branch is empty", | ||
"you can remove it", | ||
if if_expr.can_have_side_effects() { | ||
format!("{snippet};") | ||
} else { | ||
String::new() | ||
}, | ||
app, | ||
); | ||
} | ||
} | ||
} | ||
|
||
/// Returns true if any `Expr` contained within this `Expr` is a `Let`, else false. | ||
/// | ||
/// Really wish `Expr` had a `walk` method... | ||
fn is_any_if_let(expr: &Expr<'_>) -> bool { | ||
let mut v = IsAnyLetVisitor(false); | ||
|
||
v.visit_expr(expr); | ||
v.0 | ||
} | ||
|
||
struct IsAnyLetVisitor(bool); | ||
|
||
impl Visitor<'_> for IsAnyLetVisitor { | ||
fn visit_expr(&mut self, expr: &Expr<'_>) { | ||
if matches!(expr.kind, ExprKind::Let(..)) { | ||
self.0 = true; | ||
Centri3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
walk_expr(self, expr); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.