Skip to content

Commit 67d36c7

Browse files
authored
Merge pull request #2409 from davidalber/configuration-check-opt-out
Skipping formatting of some configuration snippets
2 parents 63ebe1b + 429dad7 commit 67d36c7

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

Configurations.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,8 @@ lines are found, they are trimmed down to match this integer.
19311931
Original Code:
19321932

19331933
```rust
1934+
#![rustfmt_skip]
1935+
19341936
fn foo() {
19351937
println!("a");
19361938
}
@@ -1988,6 +1990,8 @@ them, additional blank lines are inserted.
19881990
Original Code (rustfmt will not change it with the default value of `0`):
19891991

19901992
```rust
1993+
#![rustfmt_skip]
1994+
19911995
fn foo() {
19921996
println!("a");
19931997
}

rustfmt-core/tests/lib.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -664,26 +664,36 @@ impl ConfigCodeBlock {
664664

665665
fn get_block_config(&self) -> Config {
666666
let mut config = Config::default();
667-
config.override_value(
668-
self.config_name.as_ref().unwrap(),
669-
self.config_value.as_ref().unwrap(),
670-
);
667+
if self.config_value.is_some() && self.config_value.is_some() {
668+
config.override_value(
669+
self.config_name.as_ref().unwrap(),
670+
self.config_value.as_ref().unwrap(),
671+
);
672+
}
671673
config
672674
}
673675

674676
fn code_block_valid(&self) -> bool {
675677
// We never expect to not have a code block.
676678
assert!(self.code_block.is_some() && self.code_block_start.is_some());
677679

678-
if self.config_name.is_none() {
680+
// See if code block begins with #![rustfmt_skip].
681+
let fmt_skip = self.code_block
682+
.as_ref()
683+
.unwrap()
684+
.split("\n")
685+
.nth(0)
686+
.unwrap_or("") == "#![rustfmt_skip]";
687+
688+
if self.config_name.is_none() && !fmt_skip {
679689
write_message(&format!(
680690
"No configuration name for {}:{}",
681691
CONFIGURATIONS_FILE_NAME,
682692
self.code_block_start.unwrap()
683693
));
684694
return false;
685695
}
686-
if self.config_value.is_none() {
696+
if self.config_value.is_none() && !fmt_skip {
687697
write_message(&format!(
688698
"No configuration value for {}:{}",
689699
CONFIGURATIONS_FILE_NAME,
@@ -752,7 +762,7 @@ impl ConfigCodeBlock {
752762
// - Rust code blocks are identifed by lines beginning with "```rust".
753763
// - One explicit configuration setting is supported per code block.
754764
// - Rust code blocks with no configuration setting are illegal and cause an
755-
// assertion failure.
765+
// assertion failure, unless the snippet begins with #![rustfmt_skip].
756766
// - Configuration names in Configurations.md must be in the form of
757767
// "## `NAME`".
758768
// - Configuration values in Configurations.md must be in the form of

0 commit comments

Comments
 (0)