Preserve comments inside attributes

This commit is contained in:
topecongiro 2017-06-17 05:44:54 +09:00
parent 05559fc8a4
commit f60a810730
3 changed files with 19 additions and 6 deletions

View file

@ -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) => {

View file

@ -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)
}
}
})
}

View file

@ -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() {}