Skip to content

Commit a7c6c3f

Browse files
committed
Fix for issue 4603 about extra macro body indentation
1 parent 7de6968 commit a7c6c3f

File tree

4 files changed

+114
-3
lines changed

4 files changed

+114
-3
lines changed

src/formatting.rs

+4
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ pub(crate) struct ReportedErrors {
331331

332332
/// Formatted code differs from existing code (--check only).
333333
pub(crate) has_diff: bool,
334+
335+
/// Formatted code missed something, like lost comments or extra trailing space
336+
pub(crate) has_missing_errors: bool,
334337
}
335338

336339
impl ReportedErrors {
@@ -342,6 +345,7 @@ impl ReportedErrors {
342345
self.has_macro_format_failure |= other.has_macro_format_failure;
343346
self.has_check_errors |= other.has_check_errors;
344347
self.has_diff |= other.has_diff;
348+
self.has_missing_errors |= other.has_missing_errors;
345349
}
346350
}
347351

src/lib.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,21 @@ impl FormatReport {
210210
if !new_errors.is_empty() {
211211
errs.has_formatting_errors = true;
212212
}
213-
if errs.has_operational_errors && errs.has_check_errors {
213+
if errs.has_operational_errors && errs.has_check_errors && errs.has_missing_errors {
214214
return;
215215
}
216216
for err in new_errors {
217217
match err.kind {
218-
ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace => {
218+
ErrorKind::LineOverflow(..) => {
219219
errs.has_operational_errors = true;
220220
}
221+
ErrorKind::TrailingWhitespace => {
222+
errs.has_operational_errors = true;
223+
errs.has_missing_errors = true;
224+
}
225+
ErrorKind::LostComment => {
226+
errs.has_missing_errors = true;
227+
}
221228
ErrorKind::BadIssue(_)
222229
| ErrorKind::LicenseCheck
223230
| ErrorKind::DeprecatedAttr
@@ -302,7 +309,8 @@ fn format_snippet(snippet: &str, config: &Config, is_macro_def: bool) -> Option<
302309
(
303310
session.errors.has_macro_format_failure
304311
|| session.out.as_ref().unwrap().is_empty() && !snippet.is_empty()
305-
|| result.is_err(),
312+
|| result.is_err()
313+
|| (is_macro_def && session.has_missing_errors()),
306314
result,
307315
)
308316
};
@@ -477,12 +485,17 @@ impl<'b, T: Write + 'b> Session<'b, T> {
477485
self.errors.has_diff
478486
}
479487

488+
pub fn has_missing_errors(&self) -> bool {
489+
self.errors.has_missing_errors
490+
}
491+
480492
pub fn has_no_errors(&self) -> bool {
481493
!(self.has_operational_errors()
482494
|| self.has_parsing_errors()
483495
|| self.has_formatting_errors()
484496
|| self.has_check_errors()
485497
|| self.has_diff())
498+
|| self.has_missing_errors()
486499
|| self.errors.has_macro_format_failure
487500
}
488501
}

tests/source/issue-4603.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Formatting when original macro snippet is used
2+
3+
// Original issue #4603 code
4+
#![feature(or_patterns)]
5+
macro_rules! t_or_f {
6+
() => {
7+
(true // some comment
8+
| false)
9+
};
10+
}
11+
12+
// Other test cases variations
13+
macro_rules! RULES {
14+
() => {
15+
(
16+
xxxxxxx // COMMENT
17+
| yyyyyyy
18+
)
19+
};
20+
}
21+
macro_rules! RULES {
22+
() => {
23+
(xxxxxxx // COMMENT
24+
| yyyyyyy)
25+
};
26+
}
27+
28+
fn main() {
29+
macro_rules! RULES {
30+
() => {
31+
(xxxxxxx // COMMENT
32+
| yyyyyyy)
33+
};
34+
}
35+
}
36+
37+
macro_rules! RULES {
38+
() => {
39+
(xxxxxxx /* COMMENT */ | yyyyyyy)
40+
};
41+
}
42+
macro_rules! RULES {
43+
() => {
44+
(xxxxxxx /* COMMENT */
45+
| yyyyyyy)
46+
};
47+
}

tests/target/issue-4603.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Formatting when original macro snippet is used
2+
3+
// Original issue #4603 code
4+
#![feature(or_patterns)]
5+
macro_rules! t_or_f {
6+
() => {
7+
(true // some comment
8+
| false)
9+
};
10+
}
11+
12+
// Other test cases variations
13+
macro_rules! RULES {
14+
() => {
15+
(
16+
xxxxxxx // COMMENT
17+
| yyyyyyy
18+
)
19+
};
20+
}
21+
macro_rules! RULES {
22+
() => {
23+
(xxxxxxx // COMMENT
24+
| yyyyyyy)
25+
};
26+
}
27+
28+
fn main() {
29+
macro_rules! RULES {
30+
() => {
31+
(xxxxxxx // COMMENT
32+
| yyyyyyy)
33+
};
34+
}
35+
}
36+
37+
macro_rules! RULES {
38+
() => {
39+
(xxxxxxx /* COMMENT */ | yyyyyyy)
40+
};
41+
}
42+
macro_rules! RULES {
43+
() => {
44+
(xxxxxxx /* COMMENT */
45+
| yyyyyyy)
46+
};
47+
}

0 commit comments

Comments
 (0)