From 429dad72b64c9bc274a884b2a69162b051b16c0f Mon Sep 17 00:00:00 2001 From: David Alber Date: Sat, 3 Feb 2018 10:55:50 -0800 Subject: [PATCH] Formatting snippets without configuration option marked as skip --- Configurations.md | 4 ++++ rustfmt-core/tests/lib.rs | 24 +++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Configurations.md b/Configurations.md index 353c1efd6d27..4390d49d1254 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1931,6 +1931,8 @@ lines are found, they are trimmed down to match this integer. Original Code: ```rust +#![rustfmt_skip] + fn foo() { println!("a"); } @@ -1988,6 +1990,8 @@ them, additional blank lines are inserted. Original Code (rustfmt will not change it with the default value of `0`): ```rust +#![rustfmt_skip] + fn foo() { println!("a"); } diff --git a/rustfmt-core/tests/lib.rs b/rustfmt-core/tests/lib.rs index 6ebd7d80df89..c618ab9c7604 100644 --- a/rustfmt-core/tests/lib.rs +++ b/rustfmt-core/tests/lib.rs @@ -664,10 +664,12 @@ impl ConfigCodeBlock { fn get_block_config(&self) -> Config { let mut config = Config::default(); - config.override_value( - self.config_name.as_ref().unwrap(), - self.config_value.as_ref().unwrap(), - ); + if self.config_value.is_some() && self.config_value.is_some() { + config.override_value( + self.config_name.as_ref().unwrap(), + self.config_value.as_ref().unwrap(), + ); + } config } @@ -675,7 +677,15 @@ impl ConfigCodeBlock { // We never expect to not have a code block. assert!(self.code_block.is_some() && self.code_block_start.is_some()); - if self.config_name.is_none() { + // See if code block begins with #![rustfmt_skip]. + let fmt_skip = self.code_block + .as_ref() + .unwrap() + .split("\n") + .nth(0) + .unwrap_or("") == "#![rustfmt_skip]"; + + if self.config_name.is_none() && !fmt_skip { write_message(&format!( "No configuration name for {}:{}", CONFIGURATIONS_FILE_NAME, @@ -683,7 +693,7 @@ impl ConfigCodeBlock { )); return false; } - if self.config_value.is_none() { + if self.config_value.is_none() && !fmt_skip { write_message(&format!( "No configuration value for {}:{}", CONFIGURATIONS_FILE_NAME, @@ -752,7 +762,7 @@ impl ConfigCodeBlock { // - Rust code blocks are identifed by lines beginning with "```rust". // - One explicit configuration setting is supported per code block. // - Rust code blocks with no configuration setting are illegal and cause an - // assertion failure. + // assertion failure, unless the snippet begins with #![rustfmt_skip]. // - Configuration names in Configurations.md must be in the form of // "## `NAME`". // - Configuration values in Configurations.md must be in the form of