Skip to content

Commit 4834547

Browse files
authored
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 55fcbab commit 4834547

File tree

4 files changed

+192
-12
lines changed

4 files changed

+192
-12
lines changed

src/formatting/items.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,7 @@ impl<'a> FmtVisitor<'a> {
349349
self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1);
350350
self.block_indent = self.block_indent.block_indent(self.config);
351351

352-
if item.body.is_empty() {
353-
self.format_missing_no_indent(item.span.hi() - BytePos(1));
354-
self.block_indent = self.block_indent.block_unindent(self.config);
355-
let indent_str = self.block_indent.to_string(self.config);
356-
self.push_str(&indent_str);
357-
} else {
352+
if !item.body.is_empty() {
358353
let first_non_ws = item.body.first().map(|s| s.span().lo());
359354
if let Some(opening_nls) = self.advance_to_first_block_item(first_non_ws) {
360355
self.push_str(&opening_nls);
@@ -363,10 +358,11 @@ impl<'a> FmtVisitor<'a> {
363358
for item in &item.body {
364359
self.format_body_element(item);
365360
}
366-
367-
self.block_indent = self.block_indent.block_unindent(self.config);
368-
self.format_missing_with_indent(item.span.hi() - BytePos(1));
369361
}
362+
self.format_missing_no_indent(item.span.hi() - BytePos(1));
363+
self.block_indent = self.block_indent.block_unindent(self.config);
364+
let indent_str = self.block_indent.to_string(self.config);
365+
self.push_str(&indent_str);
370366

371367
self.push_str("}");
372368
} else {

src/formatting/visitor.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::{symbol, BytePos, Pos, Span, DUMMY_SP};
77
use crate::config::{BraceStyle, Config};
88
use crate::formatting::{
99
attr::*,
10-
comment::{rewrite_comment, CodeCharKind, CommentCodeSlices},
10+
comment::{contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices},
1111
items::{
1212
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
1313
rewrite_associated_impl_type, rewrite_extern_crate, rewrite_opaque_impl_type,
@@ -316,8 +316,17 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
316316
self.block_indent = self.block_indent.block_unindent(config);
317317
}
318318

319+
let comment_snippet = self.snippet(span);
320+
321+
let align_to_right = if unindent_comment && contains_comment(&comment_snippet) {
322+
let first_lines = comment_snippet.splitn(2, '/').next().unwrap_or("");
323+
last_line_width(first_lines) > last_line_width(&comment_snippet)
324+
} else {
325+
false
326+
};
327+
319328
let mut iter = CommentCodeSlices::with_offset(
320-
self.snippet(span),
329+
comment_snippet,
321330
last_line_offset,
322331
self.config.tab_spaces(),
323332
)
@@ -380,8 +389,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
380389
_ => self.push_str("\n"),
381390
}
382391
newline_inserted = true;
383-
392+
if unindent_comment && align_to_right {
393+
self.block_indent = self.block_indent.block_indent(self.config);
394+
}
384395
self.push_str(&self.block_indent.to_string_with_newline(config));
396+
if unindent_comment && align_to_right {
397+
self.block_indent = self.block_indent.block_unindent(self.config);
398+
}
385399
}
386400
}
387401
prev_kind = kind;

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+
}

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)