]> git.lizzy.rs Git - rust.git/commitdiff
Handle nested block comments
authorPhilipp Hansch <dev@phansch.net>
Fri, 30 Mar 2018 10:36:30 +0000 (12:36 +0200)
committerPhilipp Hansch <dev@phansch.net>
Fri, 30 Mar 2018 10:36:50 +0000 (12:36 +0200)
clippy_lints/src/utils/mod.rs
tests/ui/empty_line_after_outer_attribute.rs
tests/without_block_comments.rs

index e3202eed67970d2d8ebe6e150802141696cd531b..e3a7fc851b15215dd57da374bc40294cd3a03fdf 100644 (file)
@@ -1101,19 +1101,18 @@ pub fn clip(tcx: TyCtxt, u: u128, ity: ast::UintTy) -> u128 {
 pub fn without_block_comments(lines: Vec<&str>) -> Vec<&str> {
     let mut without = vec![];
 
-    // naive approach for block comments
-    let mut inside_comment = false;
+    let mut nest_level = 0;
 
     for line in lines.into_iter() {
         if line.contains("/*") {
-            inside_comment = true;
+            nest_level += 1;
             continue;
         } else if line.contains("*/") {
-            inside_comment = false;
+            nest_level -= 1;
             continue;
         }
 
-        if !inside_comment {
+        if nest_level == 0 {
             without.push(line);
         }
     }
index 99e55b2760dcd44d4c051fba057b84840d40ee0c..30063dac0a4659ff7c88717df115b86c9b862326 100644 (file)
@@ -79,11 +79,16 @@ pub enum FooFighter {
     Bar4
 }
 
-// This should not produce a warning because there is a comment in between
+// This should not produce a warning because the empty line is inside a block comment
 #[crate_type = "lib"]
 /*
 
 */
 pub struct S;
 
+// This should not produce a warning
+#[crate_type = "lib"]
+/* test */
+pub struct T;
+
 fn main() { }
index 525a357bdc7513f8e5c50125e34a4f2c5481eb0b..375df05754497b3e9482bf3d0375f8cf4ef37725 100644 (file)
@@ -15,6 +15,15 @@ fn test_lines_without_block_comments() {
     let result = without_block_comments(vec!["/* rust", "", "*/"]);
     assert!(result.is_empty());
 
+    let result = without_block_comments(vec!["/* one-line comment */"]);
+    assert!(result.is_empty());
+
+    let result = without_block_comments(vec!["/* nested", "/* multi-line",  "comment",  "*/", "test", "*/"]);
+    assert!(result.is_empty());
+
+    let result = without_block_comments(vec!["/* nested /* inline /* comment */ test */ */"]);
+    assert!(result.is_empty());
+
     let result = without_block_comments(vec!["foo", "bar", "baz"]);
     assert_eq!(result, vec!["foo", "bar", "baz"]);
 }