Skip to content

Commit 8c01672

Browse files
committed
Hoist warning message from fill_from_parsed_config into helper function
Now that we're keeping track of all the unstable options we don't need to emmit the warning when ``config.fill_from_parsed_config`` is called. This also has the added benefit of removing a side-effect from calling ``config.fill_from_parsed_config``, and instead the entire warning string is returned from a pure helper function, which made testing the warning message easier.
1 parent e8908e5 commit 8c01672

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/config/config_type.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,33 @@ macro_rules! create_config {
163163
pub fn using_unstable_options_on_stable_channel(&self) -> bool {
164164
self.configured_unstable_options.is_some()
165165
}
166+
167+
/// Return a String warning users about all unstable options used on the stable channel
168+
#[allow(unreachable_pub)]
169+
pub fn unstable_options_warning_message(&self) -> Option<String> {
170+
let unstable_options = self.unstable_options_set_on_stable_channel()?;
171+
let upgrade_to_abort_message = "\nSet `abort_on_unrecognised_options = true` \
172+
to convert this warning into an error\n\n";
173+
// Capacity of 120 * len should be enough space for all options.
174+
// the Warning string below is ~80 characters long so there is
175+
// ~40 char buffer for option name and value
176+
let mut output = String::with_capacity(
177+
upgrade_to_abort_message.len() + unstable_options.len() * 120
178+
);
179+
180+
for option in unstable_options {
181+
output.push_str(
182+
&format!("Warning: can't set `{}`, unstable features are only \
183+
available in nightly channel.\n", option
184+
)
185+
);
186+
}
187+
188+
output.push_str(upgrade_to_abort_message);
189+
190+
Some(output)
191+
}
192+
166193
fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
167194
let mut unstable_options = vec![];
168195
$(
@@ -175,8 +202,6 @@ macro_rules! create_config {
175202
self.$i.1 = true;
176203
self.$i.2 = val;
177204
} else {
178-
eprintln!("Warning: can't set `{} = {:?}`, unstable features are only \
179-
available in nightly channel.", stringify!($i), val);
180205
// only set abort_on_unrecognised_options, and store all other
181206
// nightly only options
182207
if stringify!($i) != "abort_on_unrecognised_options" {

src/config/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,39 @@ mod test {
578578
assert!(config.using_unstable_options_on_stable_channel())
579579
}
580580

581+
#[test]
582+
fn test_warning_message_when_using_unstable_options_on_stable_channel() {
583+
if crate::is_nightly_channel!() {
584+
// This test requires non-nightly
585+
return;
586+
}
587+
let toml = r#"
588+
reorder_impl_items = true
589+
"#;
590+
let config = Config::from_toml(toml, Path::new("")).unwrap();
591+
let warning = "\
592+
Warning: can't set `reorder_impl_items = true`, unstable features are only available in \
593+
nightly channel.
594+
595+
Set `abort_on_unrecognised_options = true` to convert this warning into an error
596+
597+
";
598+
assert_eq!(warning, config.unstable_options_warning_message().unwrap())
599+
}
600+
601+
#[test]
602+
fn test_no_warning_message_when_using_unstable_options_on_nightly() {
603+
if !crate::is_nightly_channel!() {
604+
// This test requires nightly
605+
return;
606+
}
607+
let toml = r#"
608+
reorder_impl_items = true
609+
"#;
610+
let config = Config::from_toml(toml, Path::new("")).unwrap();
611+
assert!(config.unstable_options_warning_message().is_none())
612+
}
613+
581614
#[test]
582615
fn test_dump_default_config() {
583616
let default_config = format!(

0 commit comments

Comments
 (0)