Skip to content

Commit b167157

Browse files
calebcartwrightpietroalbini
authored andcommitted
fix(rustfmt): resolve generated file formatting issue
1 parent 02072b4 commit b167157

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/tools/rustfmt/Configurations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,9 @@ fn add_one(x: i32) -> i32 {
929929
## `format_generated_files`
930930

931931
Format generated files. A file is considered generated
932-
if any of the first five lines contains `@generated` marker.
932+
if any of the first five lines contain a `@generated` comment marker.
933933

934-
- **Default value**: `false`
934+
- **Default value**: `true`
935935
- **Possible values**: `true`, `false`
936936
- **Stable**: No
937937

src/tools/rustfmt/src/config/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ create_config! {
138138
inline_attribute_width: usize, 0, false,
139139
"Write an item and its attribute on the same line \
140140
if their combined width is below a threshold";
141-
format_generated_files: bool, false, false, "Format generated files";
141+
format_generated_files: bool, true, false, "Format generated files";
142142

143143
// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
144144
merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one";
@@ -608,7 +608,7 @@ blank_lines_lower_bound = 0
608608
edition = "2015"
609609
version = "One"
610610
inline_attribute_width = 0
611-
format_generated_files = false
611+
format_generated_files = true
612612
merge_derives = true
613613
use_try_shorthand = false
614614
use_field_init_shorthand = false

src/tools/rustfmt/src/formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn format_project<T: FormatHandler>(
109109
let src = source_file.src.as_ref().expect("SourceFile without src");
110110

111111
let should_ignore = (!input_is_stdin && context.ignore_file(&path))
112-
|| (!config.format_generated_files() && is_generated_file(src));
112+
|| (!input_is_stdin && !config.format_generated_files() && is_generated_file(src));
113113

114114
if (config.skip_children() && path != main_file) || should_ignore {
115115
continue;

src/tools/rustfmt/src/test/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,24 @@ fn stdin_disable_all_formatting_test() {
490490
assert_eq!(input, String::from_utf8(output.stdout).unwrap());
491491
}
492492

493+
#[test]
494+
fn stdin_generated_files_issue_5172() {
495+
init_log();
496+
let input = Input::Text("//@generated\nfn main() {}".to_owned());
497+
let mut config = Config::default();
498+
config.set().emit_mode(EmitMode::Stdout);
499+
config.set().format_generated_files(false);
500+
config.set().newline_style(NewlineStyle::Unix);
501+
let mut buf: Vec<u8> = vec![];
502+
{
503+
let mut session = Session::new(config, Some(&mut buf));
504+
session.format(input).unwrap();
505+
assert!(session.has_no_errors());
506+
}
507+
// N.B. this should be changed once `format_generated_files` is supported with stdin
508+
assert_eq!(buf, "stdin:\n\n//@generated\nfn main() {}\n".as_bytes());
509+
}
510+
493511
#[test]
494512
fn format_lines_errors_are_reported() {
495513
init_log();

0 commit comments

Comments
 (0)