Skip to content

Commit 0969c6d

Browse files
Improve error for nightly-only '--message-format' args
Previously on a stable build (`json` is only supported in `nightly` builds): $ cargo fmt --message-format json Invalid value for `--emit` - using an unstable value without `--unstable-features` With this change: $ CFG_RELEASE_CHANNEL=stable cargo make bin-build $ PATH="./target/debug:$PATH" cargo fmt --message-format json --message-format json is only supported in nightly builds This utility formats all bin and lib files of the current crate using rustfmt. <-- SNIP: the rest of the usage follows --> This behaviour is similar to how args to `--emit` are handled in `rustfmt`. This change also drops displaying `json` as an option to `--message-format` on stable builds: $ PATH="./target/debug:$PATH" cargo fmt --help | grep --max-count 1 --after-context 1 message-format --message-format <message-format> Specify message-format: short|human This required rewriting the check for nightly builds (copying from the one in `src/release_channel.rs`) to be `const` so I could rewrite the `--message-format` flag's help string. Issue: #6752
1 parent 4b620cd commit 0969c6d

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

src/cargo-fmt/main.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ use clap::{CommandFactory, Parser};
2020
#[cfg(test)]
2121
mod cargo_fmt_tests;
2222

23+
const fn is_nightly() -> bool {
24+
match option_env!("CFG_RELEASE_CHANNEL") {
25+
None => true,
26+
Some(c) => matches!(c.as_bytes(), b"nightly" | b"dev"),
27+
}
28+
}
29+
30+
const MESSAGE_FORMATS: &str = if is_nightly() {
31+
"short|json|human"
32+
} else {
33+
"short|human"
34+
};
35+
2336
#[derive(Parser)]
2437
#[command(
2538
disable_version_flag = true,
@@ -54,8 +67,11 @@ pub struct Opts {
5467
#[arg(long = "manifest-path", value_name = "manifest-path")]
5568
manifest_path: Option<String>,
5669

57-
/// Specify message-format: short|json|human
58-
#[arg(long = "message-format", value_name = "message-format")]
70+
#[arg(
71+
long = "message-format",
72+
value_name = "message-format",
73+
help = format!("Specify message-format: {MESSAGE_FORMATS}")
74+
)]
5975
message_format: Option<String>,
6076

6177
/// Options passed to rustfmt
@@ -186,6 +202,11 @@ fn convert_message_format_to_rustfmt_args(
186202
Ok(())
187203
}
188204
"json" => {
205+
if !is_nightly() {
206+
return Err(String::from(
207+
"--message-format json is only supported in nightly builds",
208+
));
209+
}
189210
if contains_emit_mode {
190211
return Err(String::from(
191212
"cannot include --emit arg when --message-format is set to json",
@@ -202,7 +223,8 @@ fn convert_message_format_to_rustfmt_args(
202223
}
203224
"human" => Ok(()),
204225
_ => Err(format!(
205-
"invalid --message-format value: {message_format}. Allowed values are: short|json|human"
226+
"invalid --message-format value: {message_format}. Allowed values are: \
227+
{MESSAGE_FORMATS}"
206228
)),
207229
}
208230
}

src/cargo-fmt/test/message_format.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use super::*;
22

3+
use rustfmt_config_proc_macro::{nightly_only_test, stable_only_test};
4+
5+
#[nightly_only_test]
36
#[test]
4-
fn invalid_message_format() {
7+
fn invalid_message_format_nightly() {
58
assert_eq!(
69
convert_message_format_to_rustfmt_args("awesome", &mut vec![]),
710
Err(String::from(
@@ -10,6 +13,18 @@ fn invalid_message_format() {
1013
);
1114
}
1215

16+
#[stable_only_test]
17+
#[test]
18+
fn invalid_message_format_stable() {
19+
assert_eq!(
20+
convert_message_format_to_rustfmt_args("awesome", &mut vec![]),
21+
Err(String::from(
22+
"invalid --message-format value: awesome. Allowed values are: short|human"
23+
)),
24+
);
25+
}
26+
27+
#[nightly_only_test]
1328
#[test]
1429
fn json_message_format_and_check_arg() {
1530
let mut args = vec![String::from("--check")];
@@ -21,6 +36,7 @@ fn json_message_format_and_check_arg() {
2136
);
2237
}
2338

39+
#[nightly_only_test]
2440
#[test]
2541
fn json_message_format_and_emit_arg() {
2642
let mut args = vec![String::from("--emit"), String::from("checkstyle")];
@@ -32,6 +48,18 @@ fn json_message_format_and_emit_arg() {
3248
);
3349
}
3450

51+
#[stable_only_test]
52+
#[test]
53+
fn json_message_format_non_nightly() {
54+
assert_eq!(
55+
convert_message_format_to_rustfmt_args("json", &mut vec![]),
56+
Err(String::from(
57+
"--message-format json is only supported in nightly builds"
58+
)),
59+
);
60+
}
61+
62+
#[nightly_only_test]
3563
#[test]
3664
fn json_message_format() {
3765
let mut args = vec![String::from("--edition"), String::from("2018")];

0 commit comments

Comments
 (0)