about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Stevens <mail@lukas-stevens.de>2018-10-18 18:57:16 +0200
committerLukas Stevens <mail@lukas-stevens.de>2018-10-18 18:57:16 +0200
commit5614dcb4ead82724e5873172961a26ffbfddcd18 (patch)
tree9d03622ba8f1425051ed7359a5a4387b3b4ab1e3
parent8753e568bf0d8bdc591ca56d9c3bc442efffaede (diff)
downloadrust-5614dcb4ead82724e5873172961a26ffbfddcd18.tar.gz
rust-5614dcb4ead82724e5873172961a26ffbfddcd18.zip
Support multiline comments and hopefully fix panic
-rw-r--r--clippy_lints/src/collapsible_if.rs8
-rw-r--r--tests/ui/collapsible_if.rs13
2 files changed, 17 insertions, 4 deletions
diff --git a/clippy_lints/src/collapsible_if.rs b/clippy_lints/src/collapsible_if.rs
index 67ef1048299..a55ca04f706 100644
--- a/clippy_lints/src/collapsible_if.rs
+++ b/clippy_lints/src/collapsible_if.rs
@@ -113,10 +113,10 @@ fn check_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
 }
 
 fn block_starts_with_comment(cx: &EarlyContext<'_>, expr: &ast::Block) -> bool {
-    // The zeroth character in the trimmed block text is "{", which marks the beginning of the block.
-    // Therefore, we check if the first string after that is a comment, i.e. starts with //.
-    let trimmed_block_text = snippet_block(cx, expr.span, "..").trim_left().to_owned();
-    trimmed_block_text[1..trimmed_block_text.len()].trim_left().starts_with("//")
+    // We trim all opening braces and whitespaces and then check if the next string is a comment.
+    let trimmed_block_text =
+        snippet_block(cx, expr.span, "..").trim_left_matches(|c: char| c.is_whitespace() || c == '{').to_owned();
+    trimmed_block_text.starts_with("//") || trimmed_block_text.starts_with("/*")
 }
 
 fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
diff --git a/tests/ui/collapsible_if.rs b/tests/ui/collapsible_if.rs
index c186d9e577f..1bc866010fd 100644
--- a/tests/ui/collapsible_if.rs
+++ b/tests/ui/collapsible_if.rs
@@ -196,4 +196,17 @@ fn main() {
             println!("world!")
         }
     }
+
+    if x == "hello" {
+        /* Not collapsible */
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+
+    if x == "hello" { /* Not collapsible */
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
 }