Skip to content

Commit db1ec44

Browse files
committed
Handle nested block comments
1 parent bb4af19 commit db1ec44

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

clippy_lints/src/utils/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1101,19 +1101,18 @@ pub fn clip(tcx: TyCtxt, u: u128, ity: ast::UintTy) -> u128 {
11011101
pub fn without_block_comments(lines: Vec<&str>) -> Vec<&str> {
11021102
let mut without = vec![];
11031103

1104-
// naive approach for block comments
1105-
let mut inside_comment = false;
1104+
let mut nest_level = 0;
11061105

11071106
for line in lines.into_iter() {
11081107
if line.contains("/*") {
1109-
inside_comment = true;
1108+
nest_level += 1;
11101109
continue;
11111110
} else if line.contains("*/") {
1112-
inside_comment = false;
1111+
nest_level -= 1;
11131112
continue;
11141113
}
11151114

1116-
if !inside_comment {
1115+
if nest_level == 0 {
11171116
without.push(line);
11181117
}
11191118
}

tests/ui/empty_line_after_outer_attribute.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,16 @@ pub enum FooFighter {
7979
Bar4
8080
}
8181

82-
// This should not produce a warning because there is a comment in between
82+
// This should not produce a warning because the empty line is inside a block comment
8383
#[crate_type = "lib"]
8484
/*
8585
8686
*/
8787
pub struct S;
8888

89+
// This should not produce a warning
90+
#[crate_type = "lib"]
91+
/* test */
92+
pub struct T;
93+
8994
fn main() { }

tests/without_block_comments.rs

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ fn test_lines_without_block_comments() {
1515
let result = without_block_comments(vec!["/* rust", "", "*/"]);
1616
assert!(result.is_empty());
1717

18+
let result = without_block_comments(vec!["/* one-line comment */"]);
19+
assert!(result.is_empty());
20+
21+
let result = without_block_comments(vec!["/* nested", "/* multi-line", "comment", "*/", "test", "*/"]);
22+
assert!(result.is_empty());
23+
24+
let result = without_block_comments(vec!["/* nested /* inline /* comment */ test */ */"]);
25+
assert!(result.is_empty());
26+
1827
let result = without_block_comments(vec!["foo", "bar", "baz"]);
1928
assert_eq!(result, vec!["foo", "bar", "baz"]);
2029
}

0 commit comments

Comments
 (0)