Skip to content

Commit 0a6a7d6

Browse files
calebcartwrighttopecongiro
authored andcommitted
switch to non-recursive mode by default (#3938)
1 parent 7713d05 commit 0a6a7d6

File tree

16 files changed

+300
-78
lines changed

16 files changed

+300
-78
lines changed

Configurations.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,3 +2445,6 @@ Internal option
24452445
## `print_misformatted_file_names`
24462446

24472447
Internal option, use `-l` or `--files-with-diff`
2448+
2449+
## `recursive`
2450+
Internal option, use `-r` or `--recursive`

src/bin/main.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,12 @@ fn make_opts() -> Options {
171171
"Don't reformat child modules (unstable).",
172172
);
173173
}
174-
174+
opts.optflag(
175+
"r",
176+
"recursive",
177+
"Format all encountered modules recursively regardless of whether the modules\
178+
are defined inline or in another file",
179+
);
175180
opts.optflag("v", "verbose", "Print verbose output");
176181
opts.optflag("q", "quiet", "Print less output");
177182
opts.optflag("V", "version", "Show version information");
@@ -501,6 +506,7 @@ const STABLE_EMIT_MODES: [EmitMode; 3] = [EmitMode::Files, EmitMode::Stdout, Emi
501506
#[derive(Clone, Debug, Default)]
502507
struct GetOptsOptions {
503508
skip_children: Option<bool>,
509+
recursive: Option<bool>,
504510
quiet: bool,
505511
verbose: bool,
506512
config_path: Option<PathBuf>,
@@ -569,6 +575,16 @@ impl GetOptsOptions {
569575
}
570576
}
571577

578+
if matches.opt_present("recursive") {
579+
if let Some(true) = options.skip_children {
580+
return Err(format_err!(
581+
"Conflicting options `skip_children` and `recursive` were specified. \
582+
`skip_children` has been deprecated and should no longer be used. ",
583+
));
584+
}
585+
options.recursive = Some(true);
586+
}
587+
572588
options.config_path = matches.opt_str("config-path").map(PathBuf::from);
573589

574590
options.inline_config = matches
@@ -659,6 +675,9 @@ impl CliOptions for GetOptsOptions {
659675
if let Some(skip_children) = self.skip_children {
660676
config.set().skip_children(skip_children);
661677
}
678+
if let Some(recursive) = self.recursive {
679+
config.set().recursive(recursive);
680+
}
662681
if let Some(error_on_unformatted) = self.error_on_unformatted {
663682
config.set().error_on_unformatted(error_on_unformatted);
664683
}

src/cargo-fmt/main.rs

Lines changed: 181 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,11 @@ fn execute() -> i32 {
111111
}
112112

113113
let strategy = CargoFmtStrategy::from_opts(&opts);
114-
let mut rustfmt_args = opts.rustfmt_options;
115-
if opts.check {
116-
let check_flag = String::from("--check");
117-
if !rustfmt_args.contains(&check_flag) {
118-
rustfmt_args.push(check_flag);
119-
}
120-
}
121-
if let Some(message_format) = opts.message_format {
122-
if let Err(msg) = convert_message_format_to_rustfmt_args(&message_format, &mut rustfmt_args)
123-
{
124-
print_usage_to_stderr(&msg);
125-
return FAILURE;
126-
}
114+
let mut rustfmt_args = opts.rustfmt_options.to_owned();
115+
if let Err(ref msg) = build_rustfmt_args(&opts, &mut rustfmt_args) {
116+
print_usage_to_stderr(msg);
117+
return FAILURE;
127118
}
128-
129119
let include_nested_test_files = opts.include_nested_test_files;
130120

131121
if let Some(specified_manifest_path) = opts.manifest_path {
@@ -152,13 +142,12 @@ fn execute() -> i32 {
152142
}
153143
}
154144

155-
fn convert_message_format_to_rustfmt_args(
156-
message_format: &str,
157-
rustfmt_args: &mut Vec<String>,
158-
) -> Result<(), String> {
159-
let mut contains_emit_mode = false;
145+
fn build_rustfmt_args(opts: &Opts, rustfmt_args: &mut Vec<String>) -> Result<(), String> {
160146
let mut contains_check = false;
147+
let mut contains_emit_mode = false;
161148
let mut contains_list_files = false;
149+
let mut contains_recursive = false;
150+
162151
for arg in rustfmt_args.iter() {
163152
if arg.starts_with("--emit") {
164153
contains_emit_mode = true;
@@ -169,37 +158,53 @@ fn convert_message_format_to_rustfmt_args(
169158
if arg == "-l" || arg == "--files-with-diff" {
170159
contains_list_files = true;
171160
}
161+
if arg == "-r" || arg == "--recursive" {
162+
contains_recursive = true;
163+
}
164+
}
165+
166+
if opts.check && !contains_check {
167+
rustfmt_args.push(String::from("--check"));
172168
}
173-
match message_format {
174-
"short" => {
175-
if !contains_list_files {
176-
rustfmt_args.push(String::from("-l"));
169+
170+
if !contains_recursive {
171+
rustfmt_args.push(String::from("--recursive"));
172+
}
173+
174+
if let Some(ref format) = opts.message_format {
175+
return match format.as_ref() {
176+
"short" => {
177+
if !contains_list_files {
178+
rustfmt_args.push(String::from("-l"));
179+
}
180+
Ok(())
177181
}
178-
Ok(())
179-
}
180-
"json" => {
181-
if contains_emit_mode {
182-
return Err(String::from(
183-
"cannot include --emit arg when --message-format is set to json",
184-
));
182+
"json" => {
183+
if contains_emit_mode {
184+
return Err(String::from(
185+
"cannot include --emit arg when --message-format is set to json",
186+
));
187+
}
188+
if contains_check {
189+
return Err(String::from(
190+
"cannot include --check arg when --message-format is set to json",
191+
));
192+
}
193+
rustfmt_args.push(String::from("--emit"));
194+
rustfmt_args.push(String::from("json"));
195+
Ok(())
185196
}
186-
if contains_check {
187-
return Err(String::from(
188-
"cannot include --check arg when --message-format is set to json",
197+
"human" => Ok(()),
198+
_ => {
199+
return Err(format!(
200+
"invalid --message-format value: {}. Allowed values are: short|json|human",
201+
format
189202
));
190203
}
191-
rustfmt_args.push(String::from("--emit"));
192-
rustfmt_args.push(String::from("json"));
193-
Ok(())
194-
}
195-
"human" => Ok(()),
196-
_ => {
197-
return Err(format!(
198-
"invalid --message-format value: {}. Allowed values are: short|json|human",
199-
message_format
200-
));
201-
}
204+
};
202205
}
206+
207+
Ok(())
203208
}
204209

205210
fn print_usage_to_stderr(reason: &str) {
@@ -801,13 +806,14 @@ mod cargo_fmt_tests {
801806
);
802807
}
803808

804-
mod convert_message_format_to_rustfmt_args_tests {
809+
mod build_rustfmt_args_tests {
805810
use super::*;
806811

807812
#[test]
808813
fn invalid_message_format() {
814+
let cargo_fmt_opts = Opts::from_iter(&["test", "--message-format", "awesome"]);
809815
assert_eq!(
810-
convert_message_format_to_rustfmt_args("awesome", &mut vec![]),
816+
build_rustfmt_args(&cargo_fmt_opts, &mut vec![]),
811817
Err(String::from(
812818
"invalid --message-format value: awesome. Allowed values are: short|json|human"
813819
)),
@@ -816,9 +822,10 @@ mod cargo_fmt_tests {
816822

817823
#[test]
818824
fn json_message_format_and_check_arg() {
819-
let mut args = vec![String::from("--check")];
825+
let mut rustfmt_args = vec![String::from("--check")];
826+
let cargo_fmt_opts = Opts::from_iter(&["test", "--message-format", "json"]);
820827
assert_eq!(
821-
convert_message_format_to_rustfmt_args("json", &mut args),
828+
build_rustfmt_args(&cargo_fmt_opts, &mut rustfmt_args),
822829
Err(String::from(
823830
"cannot include --check arg when --message-format is set to json"
824831
)),
@@ -827,9 +834,10 @@ mod cargo_fmt_tests {
827834

828835
#[test]
829836
fn json_message_format_and_emit_arg() {
830-
let mut args = vec![String::from("--emit"), String::from("checkstyle")];
837+
let cargo_fmt_opts = Opts::from_iter(&["test", "--message-format", "json"]);
838+
let mut rustfmt_args = vec![String::from("--emit"), String::from("checkstyle")];
831839
assert_eq!(
832-
convert_message_format_to_rustfmt_args("json", &mut args),
840+
build_rustfmt_args(&cargo_fmt_opts, &mut rustfmt_args),
833841
Err(String::from(
834842
"cannot include --emit arg when --message-format is set to json"
835843
)),
@@ -838,50 +846,155 @@ mod cargo_fmt_tests {
838846

839847
#[test]
840848
fn json_message_format() {
841-
let mut args = vec![String::from("--edition"), String::from("2018")];
842-
assert!(convert_message_format_to_rustfmt_args("json", &mut args).is_ok());
849+
let mut rustfmt_args = vec![
850+
String::from("--edition"),
851+
String::from("2018"),
852+
String::from("--recursive"),
853+
];
854+
let cargo_fmt_opts = Opts::from_iter(&["test", "--message-format", "json"]);
855+
assert!(build_rustfmt_args(&cargo_fmt_opts, &mut rustfmt_args).is_ok());
843856
assert_eq!(
844-
args,
857+
rustfmt_args,
845858
vec![
846859
String::from("--edition"),
847860
String::from("2018"),
861+
String::from("--recursive"),
848862
String::from("--emit"),
849-
String::from("json")
863+
String::from("json"),
850864
]
851865
);
852866
}
853867

854868
#[test]
855869
fn human_message_format() {
856-
let exp_args = vec![String::from("--emit"), String::from("json")];
857-
let mut act_args = exp_args.clone();
858-
assert!(convert_message_format_to_rustfmt_args("human", &mut act_args).is_ok());
859-
assert_eq!(act_args, exp_args);
870+
let exp_args = vec![
871+
String::from("--emit"),
872+
String::from("json"),
873+
String::from("--recursive"),
874+
];
875+
let cargo_fmt_opts = Opts::from_iter(&["test", "--message-format", "human"]);
876+
let mut rustfmt_args = exp_args.clone();
877+
assert!(build_rustfmt_args(&cargo_fmt_opts, &mut rustfmt_args).is_ok());
878+
assert_eq!(rustfmt_args, exp_args);
860879
}
861880

862881
#[test]
863882
fn short_message_format() {
864-
let mut args = vec![String::from("--check")];
865-
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
866-
assert_eq!(args, vec![String::from("--check"), String::from("-l")]);
883+
let mut rustfmt_args = vec![String::from("--check"), String::from("--recursive")];
884+
let cargo_fmt_opts = Opts::from_iter(&["test", "--message-format", "short"]);
885+
assert!(build_rustfmt_args(&cargo_fmt_opts, &mut rustfmt_args).is_ok());
886+
assert_eq!(
887+
rustfmt_args,
888+
vec![
889+
String::from("--check"),
890+
String::from("--recursive"),
891+
String::from("-l"),
892+
],
893+
);
867894
}
868895

869896
#[test]
870897
fn short_message_format_included_short_list_files_flag() {
871-
let mut args = vec![String::from("--check"), String::from("-l")];
872-
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
873-
assert_eq!(args, vec![String::from("--check"), String::from("-l")]);
898+
let mut rustfmt_args = vec![
899+
String::from("--check"),
900+
String::from("-l"),
901+
String::from("--recursive"),
902+
];
903+
let cargo_fmt_opts = Opts::from_iter(&["test", "--message-format", "short"]);
904+
assert!(build_rustfmt_args(&cargo_fmt_opts, &mut rustfmt_args).is_ok());
905+
assert_eq!(
906+
rustfmt_args,
907+
vec![
908+
String::from("--check"),
909+
String::from("-l"),
910+
String::from("--recursive"),
911+
],
912+
);
874913
}
875914

876915
#[test]
877916
fn short_message_format_included_long_list_files_flag() {
878-
let mut args = vec![String::from("--check"), String::from("--files-with-diff")];
879-
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
917+
let mut rustfmt_args = vec![
918+
String::from("--check"),
919+
String::from("--files-with-diff"),
920+
String::from("--recursive"),
921+
];
922+
let cargo_fmt_opts = Opts::from_iter(&["test", "--message-format", "short"]);
923+
assert!(build_rustfmt_args(&cargo_fmt_opts, &mut rustfmt_args).is_ok());
924+
assert_eq!(
925+
rustfmt_args,
926+
vec![
927+
String::from("--check"),
928+
String::from("--files-with-diff"),
929+
String::from("--recursive"),
930+
]
931+
);
932+
}
933+
934+
#[test]
935+
fn recursive_shorthand_not_duplicated() {
936+
let mut rustfmt_args = vec![String::from("-r")];
937+
let empty: Vec<String> = vec![];
938+
assert!(build_rustfmt_args(&Opts::from_iter(&empty), &mut rustfmt_args).is_ok());
939+
assert_eq!(rustfmt_args, vec![String::from("-r")]);
940+
}
941+
942+
#[test]
943+
fn recursive_long_not_duplicated() {
944+
let mut rustfmt_args = vec![String::from("--recursive")];
945+
let empty: Vec<String> = vec![];
946+
assert!(build_rustfmt_args(&Opts::from_iter(&empty), &mut rustfmt_args).is_ok());
947+
assert_eq!(rustfmt_args, vec![String::from("--recursive")]);
948+
}
949+
950+
#[test]
951+
fn recursive_added() {
952+
let mut rustfmt_args = vec![];
953+
let empty: Vec<String> = vec![];
954+
assert!(build_rustfmt_args(&Opts::from_iter(&empty), &mut rustfmt_args).is_ok());
955+
assert_eq!(rustfmt_args, vec![String::from("--recursive")]);
956+
}
957+
958+
#[test]
959+
fn check_not_duplicated_when_included_in_cargo_fmt() {
960+
let mut rustfmt_args = vec![String::from("--check"), String::from("--recursive")];
961+
let cargo_fmt_opts = Opts::from_iter(&["test", "--check"]);
962+
assert!(build_rustfmt_args(&cargo_fmt_opts, &mut rustfmt_args).is_ok());
963+
assert_eq!(
964+
rustfmt_args,
965+
vec![String::from("--check"), String::from("--recursive")],
966+
);
967+
}
968+
969+
#[test]
970+
fn check_still_passed_through_when_not_included_in_cargo_fmt() {
971+
let mut rustfmt_args = vec![String::from("--check"), String::from("--recursive")];
972+
let empty: Vec<String> = vec![];
973+
assert!(build_rustfmt_args(&Opts::from_iter(&empty), &mut rustfmt_args).is_ok());
880974
assert_eq!(
881-
args,
882-
vec![String::from("--check"), String::from("--files-with-diff")]
975+
rustfmt_args,
976+
vec![String::from("--check"), String::from("--recursive")],
883977
);
884978
}
979+
980+
#[test]
981+
fn check_added() {
982+
let mut rustfmt_args = vec![String::from("--recursive")];
983+
let cargo_fmt_opts = Opts::from_iter(&["test", "--check"]);
984+
assert!(build_rustfmt_args(&cargo_fmt_opts, &mut rustfmt_args).is_ok());
985+
assert_eq!(
986+
rustfmt_args,
987+
vec![String::from("--recursive"), String::from("--check")],
988+
);
989+
}
990+
991+
#[test]
992+
fn check_not_added_when_flag_disabled() {
993+
let mut rustfmt_args = vec![String::from("--recursive")];
994+
let empty: Vec<String> = vec![];
995+
assert!(build_rustfmt_args(&Opts::from_iter(&empty), &mut rustfmt_args).is_ok());
996+
assert_eq!(rustfmt_args, vec![String::from("--recursive")]);
997+
}
885998
}
886999

8871000
mod get_nested_integration_test_files_tests {

0 commit comments

Comments
 (0)