Auto merge of #10810 - samueltardieu:needless-else, r=llogiq
needless_else: new lint to check for empty `else` clauses Empty `else` clauses are useless. They happen in the wild and are not linted yet: https://github.com/uutils/coreutils/pull/4880/files `else` clauses containing or preceded by comments are not linted as the comments might be important. changelog: [`needless_else`]: new lint
This commit is contained in:
commit
97598e9e8f
18 changed files with 231 additions and 70 deletions
|
|
@ -449,6 +449,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
|
|||
crate::needless_bool::NEEDLESS_BOOL_ASSIGN_INFO,
|
||||
crate::needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE_INFO,
|
||||
crate::needless_continue::NEEDLESS_CONTINUE_INFO,
|
||||
crate::needless_else::NEEDLESS_ELSE_INFO,
|
||||
crate::needless_for_each::NEEDLESS_FOR_EACH_INFO,
|
||||
crate::needless_late_init::NEEDLESS_LATE_INIT_INFO,
|
||||
crate::needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS_INFO,
|
||||
|
|
|
|||
|
|
@ -218,6 +218,7 @@ mod needless_arbitrary_self_type;
|
|||
mod needless_bool;
|
||||
mod needless_borrowed_ref;
|
||||
mod needless_continue;
|
||||
mod needless_else;
|
||||
mod needless_for_each;
|
||||
mod needless_late_init;
|
||||
mod needless_parens_on_range_literals;
|
||||
|
|
@ -992,6 +993,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
store.register_late_pass(|_| Box::new(items_after_test_module::ItemsAfterTestModule));
|
||||
store.register_early_pass(|| Box::new(ref_patterns::RefPatterns));
|
||||
store.register_late_pass(|_| Box::new(default_constructed_unit_structs::DefaultConstructedUnitStructs));
|
||||
store.register_early_pass(|| Box::new(needless_else::NeedlessElse));
|
||||
// add lints here, do not remove this comment, it's used in `new_lint`
|
||||
}
|
||||
|
||||
|
|
|
|||
57
clippy_lints/src/needless_else.rs
Normal file
57
clippy_lints/src/needless_else.rs
Normal file
|
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue