Skip to content

Commit faf97a6

Browse files
whizsidcalebcartwright
authored andcommitted
Fixed 'Incorrect comment indent inside if/else' issue. (#4459)
* Added test cases * Fixed if condition comment issue * Fixed extern C issue * Removed previous test case * Removed tmp file * honor the authors intent * Changed the file name to its original name * Removed extra whitespace
1 parent ce13ff1 commit faf97a6

File tree

4 files changed

+199
-12
lines changed

4 files changed

+199
-12
lines changed

Diff for: src/items.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ enum BodyElement<'a> {
161161
ForeignItem(&'a ast::ForeignItem),
162162
}
163163

164+
impl BodyElement<'_> {
165+
pub(crate) fn span(&self) -> Span {
166+
match self {
167+
BodyElement::ForeignItem(fi) => fi.span(),
168+
}
169+
}
170+
}
171+
164172
/// Represents a fn's signature.
165173
pub(crate) struct FnSig<'a> {
166174
decl: &'a ast::FnDecl,
@@ -268,19 +276,19 @@ impl<'a> FmtVisitor<'a> {
268276
self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1);
269277
self.block_indent = self.block_indent.block_indent(self.config);
270278

271-
if item.body.is_empty() {
272-
self.format_missing_no_indent(item.span.hi() - BytePos(1));
273-
self.block_indent = self.block_indent.block_unindent(self.config);
274-
let indent_str = self.block_indent.to_string(self.config);
275-
self.push_str(&indent_str);
276-
} else {
279+
if !item.body.is_empty() {
280+
// Advance to first item (statement or inner attribute)
281+
// within the block.
282+
self.last_pos = item.body[0].span().lo();
277283
for item in &item.body {
278284
self.format_body_element(item);
279285
}
280-
281-
self.block_indent = self.block_indent.block_unindent(self.config);
282-
self.format_missing_with_indent(item.span.hi() - BytePos(1));
283286
}
287+
288+
self.format_missing_no_indent(item.span.hi() - BytePos(1));
289+
self.block_indent = self.block_indent.block_unindent(self.config);
290+
let indent_str = self.block_indent.to_string(self.config);
291+
self.push_str(&indent_str);
284292
}
285293

286294
self.push_str("}");

Diff for: src/visitor.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::{ast, attr::HasAttrs, token::DelimToken, visit};
55
use rustc_span::{symbol, BytePos, Pos, Span, DUMMY_SP};
66

77
use crate::attr::*;
8-
use crate::comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
8+
use crate::comment::{contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices};
99
use crate::config::Version;
1010
use crate::config::{BraceStyle, Config};
1111
use crate::coverage::transform_missing_snippet;
@@ -261,14 +261,23 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
261261
trimmed.is_empty() || trimmed.chars().all(|c| c == ';')
262262
};
263263

264-
for (kind, offset, sub_slice) in CommentCodeSlices::new(self.snippet(span)) {
264+
let comment_snippet = self.snippet(span);
265+
266+
let align_to_right = if unindent_comment && contains_comment(&comment_snippet) {
267+
let first_lines = comment_snippet.splitn(2, '/').next().unwrap_or("");
268+
last_line_width(first_lines) > last_line_width(&comment_snippet)
269+
} else {
270+
false
271+
};
272+
273+
for (kind, offset, sub_slice) in CommentCodeSlices::new(comment_snippet) {
265274
let sub_slice = transform_missing_snippet(config, sub_slice);
266275

267276
debug!("close_block: {:?} {:?} {:?}", kind, offset, sub_slice);
268277

269278
match kind {
270279
CodeCharKind::Comment => {
271-
if !unindented && unindent_comment {
280+
if !unindented && unindent_comment && !align_to_right {
272281
unindented = true;
273282
self.block_indent = self.block_indent.block_unindent(config);
274283
}

Diff for: tests/source/issue-4120.rs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
fn main() {
2+
let x = if true {
3+
1
4+
// In if
5+
} else {
6+
0
7+
// In else
8+
};
9+
10+
let x = if true {
11+
1
12+
/* In if */
13+
} else {
14+
0
15+
/* In else */
16+
};
17+
18+
let z = if true {
19+
if true {
20+
1
21+
22+
// In if level 2
23+
} else {
24+
2
25+
}
26+
} else {
27+
3
28+
};
29+
30+
let a = if true {
31+
1
32+
// In if
33+
} else {
34+
0
35+
// In else
36+
};
37+
38+
let a = if true {
39+
1
40+
41+
// In if
42+
} else {
43+
0
44+
// In else
45+
};
46+
47+
let b = if true {
48+
1
49+
50+
// In if
51+
} else {
52+
0
53+
// In else
54+
};
55+
56+
let c = if true {
57+
1
58+
59+
// In if
60+
} else {
61+
0
62+
// In else
63+
};
64+
for i in 0..2 {
65+
println!("Something");
66+
// In for
67+
}
68+
69+
for i in 0..2 {
70+
println!("Something");
71+
/* In for */
72+
}
73+
74+
extern "C" {
75+
fn first();
76+
77+
// In foreign mod
78+
}
79+
80+
extern "C" {
81+
fn first();
82+
83+
/* In foreign mod */
84+
}
85+
}

Diff for: tests/target/issue-4120.rs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
fn main() {
2+
let x = if true {
3+
1
4+
// In if
5+
} else {
6+
0
7+
// In else
8+
};
9+
10+
let x = if true {
11+
1
12+
/* In if */
13+
} else {
14+
0
15+
/* In else */
16+
};
17+
18+
let z = if true {
19+
if true {
20+
1
21+
22+
// In if level 2
23+
} else {
24+
2
25+
}
26+
} else {
27+
3
28+
};
29+
30+
let a = if true {
31+
1
32+
// In if
33+
} else {
34+
0
35+
// In else
36+
};
37+
38+
let a = if true {
39+
1
40+
41+
// In if
42+
} else {
43+
0
44+
// In else
45+
};
46+
47+
let b = if true {
48+
1
49+
50+
// In if
51+
} else {
52+
0
53+
// In else
54+
};
55+
56+
let c = if true {
57+
1
58+
59+
// In if
60+
} else {
61+
0
62+
// In else
63+
};
64+
for i in 0..2 {
65+
println!("Something");
66+
// In for
67+
}
68+
69+
for i in 0..2 {
70+
println!("Something");
71+
/* In for */
72+
}
73+
74+
extern "C" {
75+
fn first();
76+
77+
// In foreign mod
78+
}
79+
80+
extern "C" {
81+
fn first();
82+
83+
/* In foreign mod */
84+
}
85+
}

0 commit comments

Comments
 (0)