Skip to content

Commit c6181b0

Browse files
committed
Abort or warn user when they use unstable options on stable channel
This commit fully implements the feature requested in 5022.
1 parent a3b6d15 commit c6181b0

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

src/bin/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ fn format_string(input: String, options: GetOptsOptions) -> Result<i32> {
276276
}
277277

278278
let out = &mut stdout();
279+
rustfmt::abort_or_warn_on_unstable_options!(config);
279280
let mut session = Session::new(config, Some(out));
280281
format_and_emit_report(&mut session, Input::Text(input));
281282

@@ -302,6 +303,8 @@ fn format(
302303
}
303304

304305
let out = &mut stdout();
306+
307+
rustfmt::abort_or_warn_on_unstable_options!(config);
305308
let mut session = Session::new(config, Some(out));
306309

307310
for file in files {
@@ -326,6 +329,7 @@ fn format(
326329
}
327330
}
328331

332+
rustfmt::abort_or_warn_on_unstable_options!(local_config);
329333
session.override_config(local_config, |sess| {
330334
format_and_emit_report(sess, Input::File(file))
331335
});

src/config/mod.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,24 @@ impl Config {
322322
}
323323
}
324324
}
325+
326+
/// When using nightly options on stable channel decide whether to abort or
327+
/// log out warnings to the user.
328+
pub fn abort_or_warn_on_unstable_options(&self) -> Option<HandleUnstableOptions> {
329+
if !self.using_unstable_options_on_stable_channel() {
330+
return None;
331+
}
332+
333+
if self.abort_on_unrecognised_options() {
334+
Some(HandleUnstableOptions::Abort(
335+
self.unstable_options_abort_message().unwrap(),
336+
))
337+
} else {
338+
Some(HandleUnstableOptions::Warn(
339+
self.unstable_options_warning_message().unwrap(),
340+
))
341+
}
342+
}
325343
}
326344

327345
/// Loads a config by checking the client-supplied options and if appropriate, the
@@ -402,6 +420,29 @@ fn config_path(options: &dyn CliOptions) -> Result<Option<PathBuf>, Error> {
402420
}
403421
}
404422

423+
#[macro_export]
424+
macro_rules! abort_or_warn_on_unstable_options {
425+
($config:expr) => {
426+
match $config.abort_or_warn_on_unstable_options() {
427+
Some($crate::HandleUnstableOptions::Abort(message)) => {
428+
eprint!("{}", message);
429+
return Ok(1);
430+
}
431+
Some($crate::HandleUnstableOptions::Warn(message)) => {
432+
eprint!("{}", message);
433+
}
434+
None => {}
435+
}
436+
};
437+
}
438+
439+
#[allow(unreachable_pub)]
440+
pub use abort_or_warn_on_unstable_options;
441+
pub enum HandleUnstableOptions {
442+
Abort(String),
443+
Warn(String),
444+
}
445+
405446
#[cfg(test)]
406447
mod test {
407448
use super::*;
@@ -647,6 +688,68 @@ Set `abort_on_unrecognised_options = false` to convert this error into a warning
647688
assert!(config.unstable_options_abort_message().is_none())
648689
}
649690

691+
#[test]
692+
fn test_on_stable_handle_unstable_options_by_issuing_a_warning() {
693+
if crate::is_nightly_channel!() {
694+
// This test requires non-nightly
695+
return;
696+
}
697+
let toml = r#"
698+
reorder_impl_items = true
699+
abort_on_unrecognised_options = false
700+
"#;
701+
let config = Config::from_toml(toml, Path::new("")).unwrap();
702+
assert!(matches!(
703+
config.abort_or_warn_on_unstable_options(),
704+
Some(HandleUnstableOptions::Warn(_))
705+
))
706+
}
707+
708+
#[test]
709+
fn test_on_stable_handle_unstable_options_by_aborting() {
710+
if crate::is_nightly_channel!() {
711+
// This test requires non-nightly
712+
return;
713+
}
714+
let toml = r#"
715+
reorder_impl_items = true
716+
abort_on_unrecognised_options = true
717+
"#;
718+
let config = Config::from_toml(toml, Path::new("")).unwrap();
719+
assert!(matches!(
720+
config.abort_or_warn_on_unstable_options(),
721+
Some(HandleUnstableOptions::Abort(_))
722+
))
723+
}
724+
725+
#[test]
726+
fn test_on_stable_no_need_to_handle_unstable_options_since_non_are_used() {
727+
if crate::is_nightly_channel!() {
728+
// This test requires non-nightly
729+
return;
730+
}
731+
use self::*;
732+
let toml = r#"
733+
array_width = 50
734+
"#;
735+
let config = Config::from_toml(toml, Path::new("")).unwrap();
736+
assert!(matches!(config.abort_or_warn_on_unstable_options(), None))
737+
}
738+
739+
#[test]
740+
fn test_on_unstable_no_need_to_warn_or_abort_when_usinig_unstable_options() {
741+
if !crate::is_nightly_channel!() {
742+
// This test requires nightly
743+
return;
744+
}
745+
use self::*;
746+
let toml = r#"
747+
array_width = 50
748+
"#;
749+
let config = Config::from_toml(toml, Path::new("")).unwrap();
750+
assert!(matches!(config.abort_or_warn_on_unstable_options(), None))
751+
}
752+
650753
#[test]
651754
fn test_dump_default_config() {
652755
let default_config = format!(

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ use crate::syntux::parser::DirectoryOwnership;
4545
use crate::utils::indent_next_line;
4646

4747
pub use crate::config::{
48-
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
49-
Range, Verbosity,
48+
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
49+
HandleUnstableOptions, NewlineStyle, Range, Verbosity,
5050
};
5151

5252
pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder};

0 commit comments

Comments
 (0)