From d3d3d7d7be481086063ee2174776e93c6e2d6c08 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Wed, 21 Feb 2018 21:11:38 +0100 Subject: [PATCH] Lint multiline attributes properly This makes it so that the `empty_line_after_outer_attribute` lint only checks for newlines between the end of the attribute and the beginning of the following item. We need to check for the empty line count being bigger than 2 because now the snippet of valid code contains only `\n` and splitting it produces `["", ""]` Invalid code will contain more than 2 empty strings. --- clippy_lints/src/attrs.rs | 12 ++++++------ tests/ui/empty_line_after_outer_attribute.rs | 9 +++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 50aaa66aadaf..417ddbe8c12b 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -271,18 +271,18 @@ fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) { return; } - let attr_to_item_span = Span::new(attr.span.lo(), span.lo(), span.ctxt()); + let begin_of_attr_to_item = Span::new(attr.span.lo(), span.lo(), span.ctxt()); + let end_of_attr_to_item = Span::new(attr.span.hi(), span.lo(), span.ctxt()); - if let Some(snippet) = snippet_opt(cx, attr_to_item_span) { + if let Some(snippet) = snippet_opt(cx, end_of_attr_to_item) { let lines = snippet.split('\n').collect::>(); - if lines.iter().filter(|l| l.trim().is_empty()).count() > 1 { + if lines.iter().filter(|l| l.trim().is_empty()).count() > 2 { span_lint( cx, EMPTY_LINE_AFTER_OUTER_ATTR, - attr_to_item_span, + begin_of_attr_to_item, "Found an empty line after an outer attribute. Perhaps you forgot to add a '!' to make it an inner attribute?" - ); - + ); } } } diff --git a/tests/ui/empty_line_after_outer_attribute.rs b/tests/ui/empty_line_after_outer_attribute.rs index ef78ca530c19..beaa98953da7 100644 --- a/tests/ui/empty_line_after_outer_attribute.rs +++ b/tests/ui/empty_line_after_outer_attribute.rs @@ -58,4 +58,13 @@ mod foo { #[allow(missing_docs)] fn three_attributes() { assert!(true) } +// This should not produce a warning +#[doc = " +Returns the escaped value of the textual representation of + +"] +pub fn function() -> bool { + true +} + fn main() { }