Skip to content

Commit bcf72a7

Browse files
fix(rustfmt): resolve generated file formatting issue
1 parent ad46af2 commit bcf72a7

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

src/tools/rustfmt/Configurations.md

+2-2
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 (tracking issue: [#5080](https://github.com/rust-lang/rustfmt/issues/5080))
937937

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

+2-2
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";
@@ -606,7 +606,7 @@ blank_lines_lower_bound = 0
606606
edition = "2015"
607607
version = "One"
608608
inline_attribute_width = 0
609-
format_generated_files = false
609+
format_generated_files = true
610610
merge_derives = true
611611
use_try_shorthand = false
612612
use_field_init_shorthand = false

src/tools/rustfmt/src/formatting.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ fn should_skip_module<T: FormatHandler>(
8080
return true;
8181
}
8282

83-
if !config.format_generated_files() {
83+
// FIXME(calebcartwright) - we need to determine how we'll handle the
84+
// `format_generated_files` option with stdin based input.
85+
if !input_is_stdin && !config.format_generated_files() {
8486
let source_file = context.parse_session.span_to_file_contents(module.span);
8587
let src = source_file.src.as_ref().expect("SourceFile without src");
8688

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

+18
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,24 @@ fn stdin_disable_all_formatting_test() {
487487
assert_eq!(input, String::from_utf8(output.stdout).unwrap());
488488
}
489489

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

0 commit comments

Comments
 (0)