From 7eb8bdbbd29bfc8c781948215e9291d64aa69cf4 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 14 May 2018 16:04:15 +1200 Subject: [PATCH] Format attributes with paths --- src/attr.rs | 21 ++++++++++++--------- tests/source/attrib.rs | 7 +++++++ tests/target/attrib.rs | 6 ++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/attr.rs b/src/attr.rs index 894b99815b4..e754144a727 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -20,6 +20,7 @@ use lists::{itemize_list, write_list, ListFormatting}; use rewrite::{Rewrite, RewriteContext}; use shape::Shape; +use types::{rewrite_path, PathContext}; use utils::{count_newlines, mk_sp}; /// Returns attributes on the given statement. @@ -200,9 +201,11 @@ fn allow_mixed_tactic_for_nested_metaitem_list(list: &[ast::NestedMetaItem]) -> impl Rewrite for ast::MetaItem { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { Some(match self.node { - ast::MetaItemKind::Word => String::from(&*self.name().as_str()), + ast::MetaItemKind::Word => { + rewrite_path(context, PathContext::Type, None, &self.ident, shape)? + } ast::MetaItemKind::List(ref list) => { - let name = self.name().as_str(); + let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?; let item_shape = match context.config.indent_style() { IndentStyle::Block => shape .block_indent(context.config.tab_spaces()) @@ -210,7 +213,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { // 1 = `(`, 2 = `]` and `)` IndentStyle::Visual => shape .visual_indent(0) - .shrink_left(name.len() + 1) + .shrink_left(path.len() + 1) .and_then(|s| s.sub_width(2))?, }; let items = itemize_list( @@ -248,21 +251,21 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { }; let item_str = write_list(&item_vec, &fmt)?; // 3 = "()" and "]" - let one_line_budget = shape.offset_left(name.len())?.sub_width(3)?.width; + let one_line_budget = shape.offset_left(path.len())?.sub_width(3)?.width; if context.config.indent_style() == IndentStyle::Visual || (!item_str.contains('\n') && item_str.len() <= one_line_budget) { - format!("{}({})", name, item_str) + format!("{}({})", path, item_str) } else { let indent = shape.indent.to_string_with_newline(context.config); let nested_indent = item_shape.indent.to_string_with_newline(context.config); - format!("{}({}{}{})", name, nested_indent, item_str, indent) + format!("{}({}{}{})", path, nested_indent, item_str, indent) } } ast::MetaItemKind::NameValue(ref literal) => { - let name = self.name().as_str(); + let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?; // 3 = ` = ` - let lit_shape = shape.shrink_left(name.len() + 3)?; + let lit_shape = shape.shrink_left(path.len() + 3)?; // `rewrite_literal` returns `None` when `literal` exceeds max // width. Since a literal is basically unformattable unless it // is a string literal (and only if `format_strings` is set), @@ -271,7 +274,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { // See #2479 for example. let value = rewrite_literal(context, literal, lit_shape) .unwrap_or_else(|| context.snippet(literal.span).to_owned()); - format!("{} = {}", name, value) + format!("{} = {}", path, value) } }) } diff --git a/tests/source/attrib.rs b/tests/source/attrib.rs index d5c244a9bf8..409e5f5a4a9 100644 --- a/tests/source/attrib.rs +++ b/tests/source/attrib.rs @@ -161,3 +161,10 @@ struct A { #[doc = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // #2647 #[cfg(feature = "this_line_is_101_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")] pub fn foo() {} + +// path attrs +#[clippy::bar] +#[clippy::bar=foo] +#[clippy::bar(a, b, c)] +pub fn foo() {} + diff --git a/tests/target/attrib.rs b/tests/target/attrib.rs index 45dc4ea8ba9..b0b8ea89f82 100644 --- a/tests/target/attrib.rs +++ b/tests/target/attrib.rs @@ -171,3 +171,9 @@ struct A { feature = "this_line_is_101_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" )] pub fn foo() {} + +// path attrs +#[clippy::bar] +#[clippy::bar=foo] +#[clippy::bar(a, b, c)] +pub fn foo() {}