Skip to content

Commit f78adda

Browse files
committed
Support @generated marker to skip code formatting
`@generated` marker is used by certain tools to understand that the file is generated, so it should be treated differently than a file written by a human: * linters should not be invoked on these files, * diffs in these files are less important, * and these files should not be reformatted. This PR proposes builtin support for `@generated` marker. I have not found a standard for a generated file marker, but: * Facebook [uses `@generated` marker](https://tinyurl.com/fb-generated) * Phabricator tool which was spawned from Facebook internal tool [also understands `@generated` marker](https://git.io/JnVHa) * Cargo inserts `@generated` marker into [generated Cargo.lock files](https://git.io/JnVHP) My personal story is that rust-protobuf project which I maintain was broken twice because of incompatibilities/bugs in rustfmt marker handling: [one](stepancheg/rust-protobuf#493), [two](stepancheg/rust-protobuf#551). While rustfmt AST markers are useful to apply to a certain AST elements, disable whole-file-at-once all-tools-at-once text level marker might be easier to use and more reliable for generated code.
1 parent 6495024 commit f78adda

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/formatting.rs

+6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
146146
is_macro_def: bool,
147147
) -> Result<(), ErrorKind> {
148148
let snippet_provider = self.parse_session.snippet_provider(module.span);
149+
150+
let generated_marker = concat!("@", "generated");
151+
if snippet_provider.entire_snippet().contains(generated_marker) {
152+
return Ok(());
153+
}
154+
149155
let mut visitor = FmtVisitor::from_parse_sess(
150156
&self.parse_session,
151157
&self.config,

tests/source/generated.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn
2+
3+
foo
4+
5+
(
6+
x: u32
7+
) {
8+
9+
10+
11+
12+
}
13+
14+
// This file is @generated

tests/target/generated.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn
2+
3+
foo
4+
5+
(
6+
x: u32
7+
) {
8+
9+
10+
11+
12+
}
13+
14+
// This file is @generated

0 commit comments

Comments
 (0)