Skip to content

Commit a3b6d15

Browse files
committed
Add helper function to generate abort message
This helper function has a similiar responsibiltiy as the helper function used to generate the warning message. Because it's a pure function that returns Option<String> it's easy to test.
1 parent 8c01672 commit a3b6d15

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/config/config_type.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ macro_rules! create_config {
190190
Some(output)
191191
}
192192

193+
/// Returns a String explaining why rustfmt exited early when using unstable options
194+
/// on the stable channel
195+
#[allow(unreachable_pub)]
196+
pub fn unstable_options_abort_message(&self) -> Option<String> {
197+
let unstable_options = self.unstable_options_set_on_stable_channel()?;
198+
let first_line = "Can't set nightly options when using stable rustfmt\n";
199+
let to_warning_message = "\nSet `abort_on_unrecognised_options = false` \
200+
to convert this error into a warning\n\n";
201+
// 40 = buffer we'll use for all options. Most will be less than 40 char long
202+
let mut output = String::with_capacity(
203+
to_warning_message.len() + first_line.len() + unstable_options.len() * 40
204+
);
205+
output.push_str(first_line);
206+
207+
for option in unstable_options {
208+
output.push_str(&format!(" - `{}`\n", option));
209+
}
210+
output.push_str(to_warning_message);
211+
Some(output)
212+
}
213+
193214
fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
194215
let mut unstable_options = vec![];
195216
$(

src/config/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,42 @@ Set `abort_on_unrecognised_options = true` to convert this warning into an error
611611
assert!(config.unstable_options_warning_message().is_none())
612612
}
613613

614+
#[test]
615+
fn test_abort_message_when_using_unstable_options_on_stable_channel() {
616+
if crate::is_nightly_channel!() {
617+
// This test requires non-nightly
618+
return;
619+
}
620+
let toml = r#"
621+
reorder_impl_items = true
622+
"#;
623+
let config = Config::from_toml(toml, Path::new("")).unwrap();
624+
let abort_message = "\
625+
Can't set nightly options when using stable rustfmt
626+
- `reorder_impl_items = true`
627+
628+
Set `abort_on_unrecognised_options = false` to convert this error into a warning
629+
630+
";
631+
assert_eq!(
632+
abort_message,
633+
config.unstable_options_abort_message().unwrap()
634+
)
635+
}
636+
637+
#[test]
638+
fn test_no_abort_message_when_using_unstable_options_on_nightly() {
639+
if !crate::is_nightly_channel!() {
640+
// This test requires nightly
641+
return;
642+
}
643+
let toml = r#"
644+
reorder_impl_items = true
645+
"#;
646+
let config = Config::from_toml(toml, Path::new("")).unwrap();
647+
assert!(config.unstable_options_abort_message().is_none())
648+
}
649+
614650
#[test]
615651
fn test_dump_default_config() {
616652
let default_config = format!(

0 commit comments

Comments
 (0)