Skip to content

Commit 8974f89

Browse files
authored
Merge pull request #1980 from topecongiro/less-recover_comment_removed
Run recover_comment_removed() only if the text changed after format
2 parents c313fb1 + 18cd196 commit 8974f89

File tree

3 files changed

+7
-13
lines changed

3 files changed

+7
-13
lines changed

src/comment.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use {Indent, Shape};
1818
use config::Config;
1919
use rewrite::RewriteContext;
2020
use string::{rewrite_string, StringFormat};
21-
use utils::{first_line_width, last_line_width, wrap_str};
21+
use utils::{first_line_width, last_line_width};
2222

2323
fn is_custom_comment(comment: &str) -> bool {
2424
if !comment.starts_with("//") {
@@ -823,13 +823,11 @@ pub fn recover_comment_removed(
823823
new: String,
824824
span: Span,
825825
context: &RewriteContext,
826-
shape: Shape,
827826
) -> Option<String> {
828827
let snippet = context.snippet(span);
829-
if changed_comment_content(&snippet, &new) {
830-
// We missed some comments
831-
// Keep previous formatting if it satisfies the constrains
832-
wrap_str(snippet, context.config.max_width(), shape)
828+
if snippet != new && changed_comment_content(&snippet, &new) {
829+
// We missed some comments. Keep the original text.
830+
Some(snippet)
833831
} else {
834832
Some(new)
835833
}

src/expr.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ pub fn format_expr(
328328

329329
expr_rw
330330
.and_then(|expr_str| {
331-
recover_comment_removed(expr_str, expr.span, context, shape)
331+
recover_comment_removed(expr_str, expr.span, context)
332332
})
333333
.and_then(|expr_str| {
334334
let attrs = outer_attributes(&expr.attrs);
@@ -920,9 +920,7 @@ impl Rewrite for ast::Stmt {
920920
}
921921
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
922922
};
923-
result.and_then(|res| {
924-
recover_comment_removed(res, self.span(), context, shape)
925-
})
923+
result.and_then(|res| recover_comment_removed(res, self.span(), context))
926924
}
927925
}
928926

src/items.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,9 +1488,7 @@ pub fn rewrite_static(
14881488
lhs,
14891489
expr,
14901490
Shape::legacy(remaining_width, offset.block_only()),
1491-
).and_then(|res| {
1492-
recover_comment_removed(res, span, context, Shape::indented(offset, context.config))
1493-
})
1491+
).and_then(|res| recover_comment_removed(res, span, context))
14941492
.map(|s| if s.ends_with(';') { s } else { s + ";" })
14951493
} else {
14961494
Some(format!("{}{};", prefix, ty_str))

0 commit comments

Comments
 (0)