Skip to content

Commit c9ddcf0

Browse files
committed
Auto merge of #10822 - Alexendoo:needless-else-cfg, r=llogiq
Ignore `#[cfg]`'d out code in `needless_else` changelog: none (same release as #10810) `#[cfg]` making things fun once more This lead me to think about macro calls that expand to nothing as well, but apparently they produce an empty stmt in the AST so are already handled, added a test for that r? `@llogiq`
2 parents f1fd467 + 021b739 commit c9ddcf0

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

clippy_lints/src/needless_else.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use clippy_utils::{diagnostics::span_lint_and_sugg, source::trim_span, span_extract_comment};
1+
use clippy_utils::source::snippet_opt;
2+
use clippy_utils::{diagnostics::span_lint_and_sugg, source::trim_span};
23
use rustc_ast::ast::{Expr, ExprKind};
34
use rustc_errors::Applicability;
45
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
@@ -35,23 +36,26 @@ declare_lint_pass!(NeedlessElse => [NEEDLESS_ELSE]);
3536

3637
impl EarlyLintPass for NeedlessElse {
3738
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
38-
if let ExprKind::If(_, then_block, Some(else_clause)) = &expr.kind &&
39-
let ExprKind::Block(block, _) = &else_clause.kind &&
40-
!expr.span.from_expansion() &&
41-
!else_clause.span.from_expansion() &&
42-
block.stmts.is_empty() {
43-
let span = trim_span(cx.sess().source_map(), expr.span.trim_start(then_block.span).unwrap());
44-
if span_extract_comment(cx.sess().source_map(), span).is_empty() {
45-
span_lint_and_sugg(
46-
cx,
47-
NEEDLESS_ELSE,
48-
span,
49-
"this else branch is empty",
50-
"you can remove it",
51-
String::new(),
52-
Applicability::MachineApplicable,
53-
);
54-
}
55-
}
39+
if let ExprKind::If(_, then_block, Some(else_clause)) = &expr.kind
40+
&& let ExprKind::Block(block, _) = &else_clause.kind
41+
&& !expr.span.from_expansion()
42+
&& !else_clause.span.from_expansion()
43+
&& block.stmts.is_empty()
44+
&& let Some(trimmed) = expr.span.trim_start(then_block.span)
45+
&& let span = trim_span(cx.sess().source_map(), trimmed)
46+
&& let Some(else_snippet) = snippet_opt(cx, span)
47+
// Ignore else blocks that contain comments or #[cfg]s
48+
&& !else_snippet.contains(['/', '#'])
49+
{
50+
span_lint_and_sugg(
51+
cx,
52+
NEEDLESS_ELSE,
53+
span,
54+
"this else branch is empty",
55+
"you can remove it",
56+
String::new(),
57+
Applicability::MachineApplicable,
58+
);
59+
}
5660
}
5761
}

tests/ui/needless_else.fixed

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ macro_rules! mac {
1212
};
1313
}
1414

15+
macro_rules! empty_expansion {
16+
() => {};
17+
}
18+
1519
fn main() {
1620
let b = std::hint::black_box(true);
1721

@@ -37,4 +41,17 @@ fn main() {
3741

3842
// Do not lint because inside a macro
3943
mac!(b);
44+
45+
if b {
46+
println!("Foobar");
47+
} else {
48+
#[cfg(foo)]
49+
"Do not lint cfg'd out code"
50+
}
51+
52+
if b {
53+
println!("Foobar");
54+
} else {
55+
empty_expansion!();
56+
}
4057
}

tests/ui/needless_else.rs

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ macro_rules! mac {
1212
};
1313
}
1414

15+
macro_rules! empty_expansion {
16+
() => {};
17+
}
18+
1519
fn main() {
1620
let b = std::hint::black_box(true);
1721

@@ -38,4 +42,17 @@ fn main() {
3842

3943
// Do not lint because inside a macro
4044
mac!(b);
45+
46+
if b {
47+
println!("Foobar");
48+
} else {
49+
#[cfg(foo)]
50+
"Do not lint cfg'd out code"
51+
}
52+
53+
if b {
54+
println!("Foobar");
55+
} else {
56+
empty_expansion!();
57+
}
4158
}

tests/ui/needless_else.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this else branch is empty
2-
--> $DIR/needless_else.rs:20:7
2+
--> $DIR/needless_else.rs:24:7
33
|
44
LL | } else {
55
| _______^

0 commit comments

Comments
 (0)