Skip to content

Commit 4b0ed96

Browse files
davidBar-Oncalebcartwright
authored andcommitted
Fix for issue 4603 about extra macro body indentation (third version)
1 parent 6170948 commit 4b0ed96

File tree

4 files changed

+120
-5
lines changed

4 files changed

+120
-5
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_unformatted_code_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_unformatted_code_errors |= other.has_unformatted_code_errors;
345349
}
346350
}
347351

src/lib.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,22 @@ 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_unformatted_code_errors
214+
{
214215
return;
215216
}
216217
for err in new_errors {
217218
match err.kind {
218-
ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace => {
219+
ErrorKind::LineOverflow(..) => {
219220
errs.has_operational_errors = true;
220221
}
222+
ErrorKind::TrailingWhitespace => {
223+
errs.has_operational_errors = true;
224+
errs.has_unformatted_code_errors = true;
225+
}
226+
ErrorKind::LostComment => {
227+
errs.has_unformatted_code_errors = true;
228+
}
221229
ErrorKind::BadIssue(_)
222230
| ErrorKind::LicenseCheck
223231
| ErrorKind::DeprecatedAttr
@@ -294,6 +302,9 @@ fn format_snippet(snippet: &str, config: &Config, is_macro_def: bool) -> Option<
294302
config.set().emit_mode(config::EmitMode::Stdout);
295303
config.set().verbose(Verbosity::Quiet);
296304
config.set().hide_parse_errors(true);
305+
if is_macro_def {
306+
config.set().error_on_unformatted(true);
307+
}
297308

298309
let (formatting_error, result) = {
299310
let input = Input::Text(snippet.into());
@@ -302,7 +313,8 @@ fn format_snippet(snippet: &str, config: &Config, is_macro_def: bool) -> Option<
302313
(
303314
session.errors.has_macro_format_failure
304315
|| session.out.as_ref().unwrap().is_empty() && !snippet.is_empty()
305-
|| result.is_err(),
316+
|| result.is_err()
317+
|| (is_macro_def && session.has_unformatted_code_errors()),
306318
result,
307319
)
308320
};
@@ -477,13 +489,18 @@ impl<'b, T: Write + 'b> Session<'b, T> {
477489
self.errors.has_diff
478490
}
479491

492+
pub fn has_unformatted_code_errors(&self) -> bool {
493+
self.errors.has_unformatted_code_errors
494+
}
495+
480496
pub fn has_no_errors(&self) -> bool {
481497
!(self.has_operational_errors()
482498
|| self.has_parsing_errors()
483499
|| self.has_formatting_errors()
484500
|| self.has_check_errors()
485-
|| self.has_diff())
486-
|| self.errors.has_macro_format_failure
501+
|| self.has_diff()
502+
|| self.has_unformatted_code_errors()
503+
|| self.errors.has_macro_format_failure)
487504
}
488505
}
489506

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)