Skip to content

Commit 39ba449

Browse files
committed
Fix collapsible_if false positive with attributes
1 parent 1021327 commit 39ba449

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

clippy_lints/src/collapsible_if.rs

+2
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,6 +144,7 @@ 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;
147149
// Prevent triggering on `if c { if let a = b { .. } }`.
148150
if !matches!(check_inner.kind, ast::ExprKind::Let(..));

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)