Skip to content

Commit 99f9f57

Browse files
jugglerchristopecongiro
authored andcommitted
Return an error if --check or --emit json are used with stdin. (#3875)
1 parent 69cf483 commit 99f9f57

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/bin/main.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ pub enum OperationError {
7373
/// An io error during reading or writing.
7474
#[fail(display = "io error: {}", _0)]
7575
IoError(IoError),
76+
/// Attempt to use --check with stdin, which isn't currently
77+
/// supported.
78+
#[fail(display = "The `--check` option is not supported with standard input.")]
79+
CheckWithStdin,
80+
/// Attempt to use --emit=json with stdin, which isn't currently
81+
/// supported.
82+
#[fail(display = "Using `--emit` other than stdout is not supported with standard input.")]
83+
EmitWithStdin,
7684
}
7785

7886
impl From<IoError> for OperationError {
@@ -242,6 +250,14 @@ fn format_string(input: String, options: GetOptsOptions) -> Result<i32, FailureE
242250
// try to read config from local directory
243251
let (mut config, _) = load_config(Some(Path::new(".")), Some(options.clone()))?;
244252

253+
if options.check {
254+
return Err(OperationError::CheckWithStdin.into());
255+
}
256+
if let Some(emit_mode) = options.emit_mode {
257+
if emit_mode != EmitMode::Stdout {
258+
return Err(OperationError::EmitWithStdin.into());
259+
}
260+
}
245261
// emit mode is always Stdout for Stdin.
246262
config.set().emit_mode(EmitMode::Stdout);
247263
config.set().verbose(Verbosity::Quiet);
@@ -486,7 +502,7 @@ struct GetOptsOptions {
486502
verbose: bool,
487503
config_path: Option<PathBuf>,
488504
inline_config: HashMap<String, String>,
489-
emit_mode: EmitMode,
505+
emit_mode: Option<EmitMode>,
490506
backup: bool,
491507
check: bool,
492508
edition: Option<Edition>,
@@ -574,7 +590,7 @@ impl GetOptsOptions {
574590
return Err(format_err!("Invalid to use `--emit` and `--check`"));
575591
}
576592

577-
options.emit_mode = emit_mode_from_emit_str(emit_str)?;
593+
options.emit_mode = Some(emit_mode_from_emit_str(emit_str)?);
578594
}
579595

580596
if let Some(ref edition_str) = matches.opt_str("edition") {
@@ -590,11 +606,13 @@ impl GetOptsOptions {
590606
}
591607

592608
if !rust_nightly {
593-
if !STABLE_EMIT_MODES.contains(&options.emit_mode) {
594-
return Err(format_err!(
595-
"Invalid value for `--emit` - using an unstable \
596-
value without `--unstable-features`",
597-
));
609+
if let Some(ref emit_mode) = options.emit_mode {
610+
if !STABLE_EMIT_MODES.contains(emit_mode) {
611+
return Err(format_err!(
612+
"Invalid value for `--emit` - using an unstable \
613+
value without `--unstable-features`",
614+
));
615+
}
598616
}
599617
}
600618

@@ -643,8 +661,8 @@ impl CliOptions for GetOptsOptions {
643661
}
644662
if self.check {
645663
config.set().emit_mode(EmitMode::Diff);
646-
} else {
647-
config.set().emit_mode(self.emit_mode);
664+
} else if let Some(emit_mode) = self.emit_mode {
665+
config.set().emit_mode(emit_mode);
648666
}
649667
if self.backup {
650668
config.set().make_backup(true);

0 commit comments

Comments
 (0)