From db1ec446160ef990675082f3208616de3157a91f Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Fri, 30 Mar 2018 12:36:30 +0200 Subject: [PATCH] Handle nested block comments --- clippy_lints/src/utils/mod.rs | 9 ++++----- tests/ui/empty_line_after_outer_attribute.rs | 7 ++++++- tests/without_block_comments.rs | 9 +++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index e3202eed679..e3a7fc851b1 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -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); } } diff --git a/tests/ui/empty_line_after_outer_attribute.rs b/tests/ui/empty_line_after_outer_attribute.rs index 99e55b2760d..30063dac0a4 100644 --- a/tests/ui/empty_line_after_outer_attribute.rs +++ b/tests/ui/empty_line_after_outer_attribute.rs @@ -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() { } diff --git a/tests/without_block_comments.rs b/tests/without_block_comments.rs index 525a357bdc7..375df057544 100644 --- a/tests/without_block_comments.rs +++ b/tests/without_block_comments.rs @@ -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"]); } -- 2.44.0