Skip to content

Commit 889d96f

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 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 all-at-once at text level marker might be easier to use and more reliable for generated code.
1 parent 6495024 commit 889d96f

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/formatting.rs

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ 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+
if snippet_provider.entire_snippet().contains("@generated") {
151+
return Ok(());
152+
}
153+
149154
let mut visitor = FmtVisitor::from_parse_sess(
150155
&self.parse_session,
151156
&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)