Skip to content

Commit

Permalink
Handle nested block comments
Browse files Browse the repository at this point in the history
  • Loading branch information
phansch committed Mar 30, 2018
1 parent bb4af19 commit db1ec44
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
9 changes: 4 additions & 5 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
7 changes: 6 additions & 1 deletion tests/ui/empty_line_after_outer_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
9 changes: 9 additions & 0 deletions tests/without_block_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
}

0 comments on commit db1ec44

Please sign in to comment.