-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Redundant else lint #6330
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
Merged
Add Redundant else lint #6330
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,135 @@ | ||
use crate::utils::span_lint_and_help; | ||
use rustc_ast::ast::{Block, Expr, ExprKind, Stmt, StmtKind}; | ||
use rustc_ast::visit::{walk_expr, Visitor}; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for `else` blocks that can be removed without changing semantics. | ||
/// | ||
/// **Why is this bad?** The `else` block adds unnecessary indentation and verbosity. | ||
/// | ||
/// **Known problems:** Some may prefer to keep the `else` block for clarity. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// fn my_func(count: u32) { | ||
/// if count == 0 { | ||
/// print!("Nothing to do"); | ||
/// return; | ||
/// } else { | ||
/// print!("Moving on..."); | ||
/// } | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// fn my_func(count: u32) { | ||
/// if count == 0 { | ||
/// print!("Nothing to do"); | ||
/// return; | ||
/// } | ||
/// print!("Moving on..."); | ||
/// } | ||
/// ``` | ||
pub REDUNDANT_ELSE, | ||
pedantic, | ||
"`else` branch that can be removed without changing semantics" | ||
} | ||
|
||
declare_lint_pass!(RedundantElse => [REDUNDANT_ELSE]); | ||
|
||
impl EarlyLintPass for RedundantElse { | ||
fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &Stmt) { | ||
if in_external_macro(cx.sess, stmt.span) { | ||
return; | ||
} | ||
// Only look at expressions that are a whole statement | ||
let expr: &Expr = match &stmt.kind { | ||
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr, | ||
_ => return, | ||
}; | ||
// if else | ||
let (mut then, mut els): (&Block, &Expr) = match &expr.kind { | ||
ExprKind::If(_, then, Some(els)) => (then, els), | ||
_ => return, | ||
}; | ||
loop { | ||
if !BreakVisitor::default().check_block(then) { | ||
// then block does not always break | ||
return; | ||
} | ||
match &els.kind { | ||
// else if else | ||
ExprKind::If(_, next_then, Some(next_els)) => { | ||
then = next_then; | ||
els = next_els; | ||
continue; | ||
}, | ||
// else if without else | ||
ExprKind::If(..) => return, | ||
// done | ||
_ => break, | ||
} | ||
} | ||
span_lint_and_help( | ||
cx, | ||
REDUNDANT_ELSE, | ||
els.span, | ||
"redundant else block", | ||
None, | ||
"remove the `else` block and move the contents out", | ||
); | ||
} | ||
} | ||
|
||
/// Call `check` functions to check if an expression always breaks control flow | ||
#[derive(Default)] | ||
struct BreakVisitor { | ||
is_break: bool, | ||
} | ||
|
||
impl<'ast> Visitor<'ast> for BreakVisitor { | ||
fn visit_block(&mut self, block: &'ast Block) { | ||
self.is_break = match block.stmts.as_slice() { | ||
[.., last] => self.check_stmt(last), | ||
_ => false, | ||
}; | ||
} | ||
|
||
fn visit_expr(&mut self, expr: &'ast Expr) { | ||
self.is_break = match expr.kind { | ||
ExprKind::Break(..) | ExprKind::Continue(..) | ExprKind::Ret(..) => true, | ||
ExprKind::Match(_, ref arms) => arms.iter().all(|arm| self.check_expr(&arm.body)), | ||
ExprKind::If(_, ref then, Some(ref els)) => self.check_block(then) && self.check_expr(els), | ||
ExprKind::If(_, _, None) | ||
// ignore loops for simplicity | ||
| ExprKind::While(..) | ExprKind::ForLoop(..) | ExprKind::Loop(..) => false, | ||
_ => { | ||
walk_expr(self, expr); | ||
return; | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
impl BreakVisitor { | ||
fn check<T>(&mut self, item: T, visit: fn(&mut Self, T)) -> bool { | ||
visit(self, item); | ||
std::mem::replace(&mut self.is_break, false) | ||
ebroto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
fn check_block(&mut self, block: &Block) -> bool { | ||
self.check(block, Self::visit_block) | ||
} | ||
|
||
fn check_expr(&mut self, expr: &Expr) -> bool { | ||
self.check(expr, Self::visit_expr) | ||
} | ||
|
||
fn check_stmt(&mut self, stmt: &Stmt) -> bool { | ||
self.check(stmt, Self::visit_stmt) | ||
} | ||
} |
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.
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.
The type annotations do not seem to be needed (here and below)
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.
The type annotation coerces
&P<Expr>
to&Expr
. I think this is preferable to doing&**expr
inside the match?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.
Hmm, just removing the annotations compiles fine. I guess it could be related with match ergonomics being able to auto-deref if the pattern matches?
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.
Yes I think so.
&**expr
- manual deref before binding - less clear and smart-pointer-dependantUh oh!
There was an error while loading. Please reload this page.
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.
I think it won't make a big difference, so I think it's fine to merge it as is. Thanks!
To explain my reasoning:
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.
But yeah, I see your point, there would be cases where you could need more derefs than if you do it once when binding 👍