Skip to content

Commit 5614dcb

Browse files
committed
Support multiline comments and hopefully fix panic
1 parent 8753e56 commit 5614dcb

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

clippy_lints/src/collapsible_if.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ fn check_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
113113
}
114114

115115
fn block_starts_with_comment(cx: &EarlyContext<'_>, expr: &ast::Block) -> bool {
116-
// The zeroth character in the trimmed block text is "{", which marks the beginning of the block.
117-
// Therefore, we check if the first string after that is a comment, i.e. starts with //.
118-
let trimmed_block_text = snippet_block(cx, expr.span, "..").trim_left().to_owned();
119-
trimmed_block_text[1..trimmed_block_text.len()].trim_left().starts_with("//")
116+
// We trim all opening braces and whitespaces and then check if the next string is a comment.
117+
let trimmed_block_text =
118+
snippet_block(cx, expr.span, "..").trim_left_matches(|c: char| c.is_whitespace() || c == '{').to_owned();
119+
trimmed_block_text.starts_with("//") || trimmed_block_text.starts_with("/*")
120120
}
121121

122122
fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {

tests/ui/collapsible_if.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,17 @@ fn main() {
196196
println!("world!")
197197
}
198198
}
199+
200+
if x == "hello" {
201+
/* Not collapsible */
202+
if y == "world" {
203+
println!("Hello world!");
204+
}
205+
}
206+
207+
if x == "hello" { /* Not collapsible */
208+
if y == "world" {
209+
println!("Hello world!");
210+
}
211+
}
199212
}

0 commit comments

Comments
 (0)