Skip to content

Commit 9c3b43e

Browse files
committed
Auto merge of #6701 - camsteffen:collapsible-if, r=flip1995
Fix collapsible_if with attributes changelog: Fix collapsible_if FP with attributes Fixes #6593
2 parents eb80ac4 + 39ba449 commit 9c3b43e

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

clippy_lints/src/collapsible_if.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
122122
if let ast::ExprKind::Block(ref block, _) = else_.kind;
123123
if !block_starts_with_comment(cx, block);
124124
if let Some(else_) = expr_block(block);
125+
if else_.attrs.is_empty();
125126
if !else_.span.from_expansion();
126127
if let ast::ExprKind::If(..) = else_.kind;
127128
then {
@@ -143,16 +144,12 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &
143144
if_chain! {
144145
if !block_starts_with_comment(cx, then);
145146
if let Some(inner) = expr_block(then);
147+
if inner.attrs.is_empty();
146148
if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.kind;
149+
// Prevent triggering on `if c { if let a = b { .. } }`.
150+
if !matches!(check_inner.kind, ast::ExprKind::Let(..));
151+
if expr.span.ctxt() == inner.span.ctxt();
147152
then {
148-
if let ast::ExprKind::Let(..) = check_inner.kind {
149-
// Prevent triggering on `if c { if let a = b { .. } }`.
150-
return;
151-
}
152-
153-
if expr.span.ctxt() != inner.span.ctxt() {
154-
return;
155-
}
156153
span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this `if` statement can be collapsed", |diag| {
157154
let lhs = Sugg::ast(cx, check, "..");
158155
let rhs = Sugg::ast(cx, check_inner, "..");

tests/ui/collapsible_else_if.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,13 @@ fn main() {
6565
else {
6666
println!("!")
6767
}
68+
69+
if x == "hello" {
70+
print!("Hello ");
71+
} else {
72+
#[cfg(not(roflol))]
73+
if y == "world" {
74+
println!("world!")
75+
}
76+
}
6877
}

tests/ui/collapsible_else_if.rs

+9
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,13 @@ fn main() {
7979
println!("!")
8080
}
8181
}
82+
83+
if x == "hello" {
84+
print!("Hello ");
85+
} else {
86+
#[cfg(not(roflol))]
87+
if y == "world" {
88+
println!("world!")
89+
}
90+
}
8291
}

tests/ui/collapsible_if.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,11 @@ fn main() {
138138

139139
// Fix #5962
140140
if matches!(true, true) && matches!(true, true) {}
141+
142+
if true {
143+
#[cfg(not(teehee))]
144+
if true {
145+
println!("Hello world!");
146+
}
147+
}
141148
}

tests/ui/collapsible_if.rs

+7
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,11 @@ fn main() {
154154
if matches!(true, true) {
155155
if matches!(true, true) {}
156156
}
157+
158+
if true {
159+
#[cfg(not(teehee))]
160+
if true {
161+
println!("Hello world!");
162+
}
163+
}
157164
}

0 commit comments

Comments
 (0)