Skip to content

Commit 3fe2233

Browse files
committed
Fix error_on_unformatted and skip_children override
Currently, error_on_unformatted and skip_children options specified in the config file are discarded. This happens because CLI options have a higher priority, but we coerce an absence of a `bool` option to `false`. In this scenario, an absence of a `bool` option is indistinguishable from explicetely set as `false`. We should coerce it to `None` instead, so it does not override the one in the config file.
1 parent b7ba6f7 commit 3fe2233

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/bin/main.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,18 @@ enum Operation {
5757
/// Parsed command line options.
5858
#[derive(Clone, Debug, Default)]
5959
struct CliOptions {
60-
skip_children: bool,
60+
skip_children: Option<bool>,
6161
verbose: bool,
6262
write_mode: Option<WriteMode>,
6363
color: Option<Color>,
6464
file_lines: FileLines, // Default is all lines in all files.
6565
unstable_features: bool,
66-
error_on_unformatted: bool,
66+
error_on_unformatted: Option<bool>,
6767
}
6868

6969
impl CliOptions {
7070
fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
7171
let mut options = CliOptions::default();
72-
options.skip_children = matches.opt_present("skip-children");
7372
options.verbose = matches.opt_present("verbose");
7473
let unstable_features = matches.opt_present("unstable-features");
7574
let rust_nightly = option_env!("CFG_RELEASE_CHANNEL")
@@ -105,19 +104,26 @@ impl CliOptions {
105104
options.file_lines = file_lines.parse()?;
106105
}
107106

107+
if matches.opt_present("skip-children") {
108+
options.skip_children = Some(true);
109+
}
108110
if matches.opt_present("error-on-unformatted") {
109-
options.error_on_unformatted = true;
111+
options.error_on_unformatted = Some(true);
110112
}
111113

112114
Ok(options)
113115
}
114116

115117
fn apply_to(self, config: &mut Config) {
116-
config.set().skip_children(self.skip_children);
117118
config.set().verbose(self.verbose);
118119
config.set().file_lines(self.file_lines);
119120
config.set().unstable_features(self.unstable_features);
120-
config.set().error_on_unformatted(self.error_on_unformatted);
121+
if let Some(skip_children) = self.skip_children {
122+
config.set().skip_children(skip_children);
123+
}
124+
if let Some(error_on_unformatted) = self.error_on_unformatted {
125+
config.set().error_on_unformatted(error_on_unformatted);
126+
}
121127
if let Some(write_mode) = self.write_mode {
122128
config.set().write_mode(write_mode);
123129
}

0 commit comments

Comments
 (0)