-
Notifications
You must be signed in to change notification settings - Fork 1.6k
needless_else: new lint to check for empty else
clauses
#10810
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
Changes from all commits
Commits
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,57 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, source::trim_span, span_extract_comment}; | ||
use rustc_ast::ast::{Expr, ExprKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for empty `else` branches. | ||
/// | ||
/// ### Why is this bad? | ||
/// An empty else branch does nothing and can be removed. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
///# fn check() -> bool { true } | ||
/// if check() { | ||
/// println!("Check successful!"); | ||
/// } else { | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
///# fn check() -> bool { true } | ||
/// if check() { | ||
/// println!("Check successful!"); | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.71.0"] | ||
pub NEEDLESS_ELSE, | ||
style, | ||
"empty else branch" | ||
} | ||
declare_lint_pass!(NeedlessElse => [NEEDLESS_ELSE]); | ||
|
||
impl EarlyLintPass for NeedlessElse { | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { | ||
if let ExprKind::If(_, then_block, Some(else_clause)) = &expr.kind && | ||
let ExprKind::Block(block, _) = &else_clause.kind && | ||
!expr.span.from_expansion() && | ||
!else_clause.span.from_expansion() && | ||
block.stmts.is_empty() { | ||
let span = trim_span(cx.sess().source_map(), expr.span.trim_start(then_block.span).unwrap()); | ||
if span_extract_comment(cx.sess().source_map(), span).is_empty() { | ||
span_lint_and_sugg( | ||
cx, | ||
NEEDLESS_ELSE, | ||
span, | ||
"this else branch is empty", | ||
"you can remove it", | ||
String::new(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
} |
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,40 @@ | ||
//@run-rustfix | ||
#![allow(unused)] | ||
#![warn(clippy::needless_else)] | ||
#![allow(clippy::suspicious_else_formatting)] | ||
|
||
macro_rules! mac { | ||
($test:expr) => { | ||
if $test { | ||
println!("Test successful!"); | ||
} else { | ||
} | ||
}; | ||
} | ||
|
||
fn main() { | ||
let b = std::hint::black_box(true); | ||
|
||
if b { | ||
println!("Foobar"); | ||
} | ||
|
||
if b { | ||
println!("Foobar"); | ||
} else { | ||
// Do not lint because this comment might be important | ||
} | ||
|
||
if b { | ||
println!("Foobar"); | ||
} else | ||
/* Do not lint because this comment might be important */ | ||
{ | ||
} | ||
|
||
// Do not lint because of the expression | ||
let _ = if b { 1 } else { 2 }; | ||
|
||
// Do not lint because inside a macro | ||
mac!(b); | ||
} |
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,41 @@ | ||
//@run-rustfix | ||
#![allow(unused)] | ||
#![warn(clippy::needless_else)] | ||
#![allow(clippy::suspicious_else_formatting)] | ||
|
||
macro_rules! mac { | ||
($test:expr) => { | ||
if $test { | ||
println!("Test successful!"); | ||
} else { | ||
} | ||
}; | ||
} | ||
|
||
fn main() { | ||
let b = std::hint::black_box(true); | ||
|
||
if b { | ||
println!("Foobar"); | ||
} else { | ||
} | ||
|
||
if b { | ||
println!("Foobar"); | ||
} else { | ||
// Do not lint because this comment might be important | ||
} | ||
|
||
if b { | ||
println!("Foobar"); | ||
} else | ||
/* Do not lint because this comment might be important */ | ||
{ | ||
} | ||
|
||
// Do not lint because of the expression | ||
let _ = if b { 1 } else { 2 }; | ||
|
||
// Do not lint because inside a macro | ||
mac!(b); | ||
} |
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,12 @@ | ||
error: this else branch is empty | ||
--> $DIR/needless_else.rs:20:7 | ||
| | ||
LL | } else { | ||
| _______^ | ||
LL | | } | ||
| |_____^ help: you can remove it | ||
| | ||
= note: `-D clippy::needless-else` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|
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.
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 wonder how often that happens. Given that it's a style lint (which is warn by default), I worry that it might drown out more important messages if we leave it that way. On the other hand, if it's rare anyway, perhaps it's not a big deal. I'll run lintcheck and report back.