Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --check flag to cargo fmt #3890

Merged
merged 1 commit into from
Oct 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ pub struct Opts {
/// Format all packages (only usable in workspaces)
#[structopt(long = "all")]
format_all: bool,

/// Run rustfmt in check mode
#[structopt(long = "check")]
check: bool,
}

fn main() {
Expand Down Expand Up @@ -104,6 +108,12 @@ fn execute() -> i32 {

let strategy = CargoFmtStrategy::from_opts(&opts);
let mut rustfmt_args = opts.rustfmt_options;
if opts.check {
let check_flag = String::from("--check");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can defer the allocation of a String until we absolutely need it:

let check_flag = "--check";
if !rustfmt_args.contains(check_flag) {
    rustfmt_args.push(check_flag.to_owned());
}

Copy link
Member Author

@calebcartwright calebcartwright Oct 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I believe the limitations with Vec::contains that we ran into in #3752 (comment) still exist which prevent using a literal as an arg.

On a related note, I was going to volunteer to work on #3587 and while working on that I'll also look into consolidating the way cargo fmt is transforming/passing flags (like --message-format --> --emit, --check, etc.) to rustfmt which I think may help remove some of these Vec::contains

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my god, I am so dumb 😩 Sorry about that!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries!

if !rustfmt_args.contains(&check_flag) {
rustfmt_args.push(check_flag);
}
}
if let Some(message_format) = opts.message_format {
if let Err(msg) = convert_message_format_to_rustfmt_args(&message_format, &mut rustfmt_args)
{
Expand Down Expand Up @@ -544,6 +554,7 @@ mod cargo_fmt_tests {
assert_eq!(false, o.quiet);
assert_eq!(false, o.verbose);
assert_eq!(false, o.version);
assert_eq!(false, o.check);
assert_eq!(empty, o.packages);
assert_eq!(empty, o.rustfmt_options);
assert_eq!(false, o.format_all);
Expand All @@ -562,13 +573,15 @@ mod cargo_fmt_tests {
"p2",
"--message-format",
"short",
"--check",
"--",
"--edition",
"2018",
]);
assert_eq!(true, o.quiet);
assert_eq!(false, o.verbose);
assert_eq!(false, o.version);
assert_eq!(true, o.check);
assert_eq!(vec!["p1", "p2"], o.packages);
assert_eq!(vec!["--edition", "2018"], o.rustfmt_options);
assert_eq!(false, o.format_all);
Expand Down Expand Up @@ -597,12 +610,12 @@ mod cargo_fmt_tests {
fn mandatory_separator() {
assert!(
Opts::clap()
.get_matches_from_safe(&["test", "--check"])
.get_matches_from_safe(&["test", "--emit"])
.is_err()
);
assert!(
!Opts::clap()
.get_matches_from_safe(&["test", "--", "--check"])
.get_matches_from_safe(&["test", "--", "--emit"])
.is_err()
);
}
Expand Down