diff --git a/src/expr.rs b/src/expr.rs index 892e6ef905e..1b17ced8a42 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -157,9 +157,8 @@ fn format_expr( ast::ExprKind::Loop(..) | ast::ExprKind::While(..) | ast::ExprKind::WhileLet(..) => { - to_control_flow(expr, expr_type).and_then(|control_flow| { - control_flow.rewrite(context, shape) - }) + to_control_flow(expr, expr_type) + .and_then(|control_flow| control_flow.rewrite(context, shape)) } ast::ExprKind::Block(ref block) => block.rewrite(context, shape), ast::ExprKind::Match(ref cond, ref arms) => { diff --git a/src/visitor.rs b/src/visitor.rs index f0aeb6bfa3f..17f48933499 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -19,7 +19,7 @@ use strings::string_buffer::StringBuffer; use {Indent, Shape}; use utils::{self, mk_sp}; use codemap::{LineRangeUtils, SpanUtils}; -use comment::FindUncommented; +use comment::{contains_comment, FindUncommented}; use config::Config; use rewrite::{Rewrite, RewriteContext}; use comment::rewrite_comment; @@ -761,7 +761,7 @@ impl Rewrite for ast::MetaItem { ast::MetaItemKind::NameValue(ref literal) => { let name = self.name.as_str(); let value = context.snippet(literal.span); - if &*name == "doc" && value.starts_with("///") { + if &*name == "doc" && contains_comment(&value) { let doc_shape = Shape { width: cmp::min(shape.width, context.config.comment_width()) .checked_sub(shape.indent.width()) @@ -786,7 +786,12 @@ impl Rewrite for ast::Attribute { if rw.starts_with("///") { rw } else { - format!("#[{}]", rw) + let original = context.snippet(self.span); + if contains_comment(&original) { + original + } else { + format!("#[{}]", rw) + } } }) } diff --git a/tests/target/issue-1703.rs b/tests/target/issue-1703.rs new file mode 100644 index 00000000000..4079ef4cfca --- /dev/null +++ b/tests/target/issue-1703.rs @@ -0,0 +1,9 @@ +// rustfmt should not remove doc comments or comments inside attributes. + +/** +This function has a block doc comment. + */ +fn test_function() {} + +#[foo /* do not remove this! */] +fn foo() {}