Skip to content

Commit 38aaea9

Browse files
committed
Fix false positive in empty_line_after_outer_attr
Before, when you had a block comment between an attribute and the following item like this: ```rust \#[crate_type = "lib"] /* */ pub struct Rust; ``` It would cause a false positive on the lint, because there is an empty line inside the block comment. This makes sure that basic block comments are detected and removed from the snippet that was created before.
1 parent 9887b97 commit 38aaea9

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

clippy_lints/src/attrs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::ty::{self, TyCtxt};
77
use semver::Version;
88
use syntax::ast::{Attribute, AttrStyle, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
99
use syntax::codemap::Span;
10-
use utils::{in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_then};
10+
use utils::{in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_then, without_block_comments};
1111

1212
/// **What it does:** Checks for items annotated with `#[inline(always)]`,
1313
/// unless the annotated function is empty or simply panics.
@@ -276,6 +276,8 @@ fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
276276

277277
if let Some(snippet) = snippet_opt(cx, end_of_attr_to_item) {
278278
let lines = snippet.split('\n').collect::<Vec<_>>();
279+
let lines = without_block_comments(lines);
280+
279281
if lines.iter().filter(|l| l.trim().is_empty()).count() > 2 {
280282
span_lint(
281283
cx,

clippy_lints/src/utils/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,3 +1086,37 @@ pub fn clip(tcx: TyCtxt, u: u128, ity: ast::UintTy) -> u128 {
10861086
let amt = 128 - bits;
10871087
(u << amt) >> amt
10881088
}
1089+
1090+
/// Remove block comments from the given Vec of lines
1091+
///
1092+
/// # Examples
1093+
///
1094+
/// ```rust,ignore
1095+
/// without_block_comments(vec!["/*", "foo", "*/"]);
1096+
/// // => vec![]
1097+
///
1098+
/// without_block_comments(vec!["bar", "/*", "foo", "*/"]);
1099+
/// // => vec!["bar"]
1100+
/// ```
1101+
pub fn without_block_comments(lines: Vec<&str>) -> Vec<&str> {
1102+
let mut without = vec![];
1103+
1104+
// naive approach for block comments
1105+
let mut inside_comment = false;
1106+
1107+
for line in lines.into_iter() {
1108+
if line.contains("/*") {
1109+
inside_comment = true;
1110+
continue;
1111+
} else if line.contains("*/") {
1112+
inside_comment = false;
1113+
continue;
1114+
}
1115+
1116+
if !inside_comment {
1117+
without.push(line);
1118+
}
1119+
}
1120+
1121+
without
1122+
}

tests/ui/empty_line_after_outer_attribute.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,11 @@ pub enum FooFighter {
7979
Bar4
8080
}
8181

82+
// This should not produce a warning because there is a comment in between
83+
#[crate_type = "lib"]
84+
/*
85+
86+
*/
87+
pub struct S;
88+
8289
fn main() { }

tests/without_block_comments.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
extern crate clippy_lints;
2+
use clippy_lints::utils::without_block_comments;
3+
4+
#[test]
5+
fn test_lines_without_block_comments() {
6+
let result = without_block_comments(vec!["/*", "", "*/"]);
7+
println!("result: {:?}", result);
8+
assert!(result.is_empty());
9+
10+
let result = without_block_comments(
11+
vec!["", "/*", "", "*/", "#[crate_type = \"lib\"]", "/*", "", "*/", ""]
12+
);
13+
assert_eq!(result, vec!["", "#[crate_type = \"lib\"]", ""]);
14+
15+
let result = without_block_comments(vec!["/* rust", "", "*/"]);
16+
assert!(result.is_empty());
17+
18+
let result = without_block_comments(vec!["foo", "bar", "baz"]);
19+
assert_eq!(result, vec!["foo", "bar", "baz"]);
20+
}

0 commit comments

Comments
 (0)