Keep --check non-mutating when emit_mode is set via --config - #7002
Conversation
There was a problem hiding this comment.
This is not the direction I'd like to go with. Maybe it wasn't clear from my earlier comment (#6999 (comment)), but I'd like rustfmt to treat this as a hard error. When passing --check from the CLI user's already can't set --emit so I'd also like to prevent them from setting --config=emit_mode={any_emit_mode}
|
Reminder, once the PR becomes ready for a review, use |
62135a9 to
4759ee3
Compare
|
Thanks for the steer — updated to reject the combination instead of silently overriding. @rustbot ready |
There was a problem hiding this comment.
I know you're using AI, and I know that AI loves to leave comments, but this one isn't necessary. Let's remove it.
View changes since this review
| // `--check` runs in a non-mutating mode, so just as `--emit` is rejected | ||
| // above, reject an `emit_mode` supplied through `--config` (e.g. | ||
| // `--config=emit_mode=files`), which would otherwise let a check write | ||
| // the file in place. |
There was a problem hiding this comment.
I don't think this comment is necessary. The code is self explanatory
`rustfmt --check --config emit_mode=files <file>` bypassed the existing `--emit files` + `--check` incompatibility check and formatted the file in place. Reject an `emit_mode` supplied through `--config` in check mode too, so `--check` stays non-mutating.
4759ee3 to
8f2a9c3
Compare
|
Removed, thanks. @rustbot ready |
| if let Some(ref emit_str) = matches.opt_str("emit") { | ||
| if options.check { | ||
| return Err(format_err!("Invalid to use `--emit` and `--check`")); | ||
| } |
There was a problem hiding this comment.
For completeness I think we should also disallow --emit and --config=emit_mode, since you could set contradictory values for each, which is the same issue we're trying to fix for --check. For example, something like --emit=stdout --config=emit_mode=File.
| let opts = make_opts(); | ||
| let matches = opts | ||
| .parse(["--check", "--config", "emit_mode=Files"]) | ||
| .unwrap(); | ||
| assert!(GetOptsOptions::from_matches(&matches).is_err()); |
There was a problem hiding this comment.
For completeness let's check all emit_mod variants.
|
Done — the |
--checkset the emit mode toDiffinapply_to_configbefore the inline--configoverrides were applied, sorustfmt --check --config emit_mode=files <file>clobbered that withemit_mode=filesand formatted the file in place — the same combination that--emit files --checkalready rejects.This moves the
--check→Diffenforcement to after the inline--configoverrides so it can't be clobbered, keeping--checknon-mutating regardless of the supplied config. Added a regression test.Before / after on
fn main(){println!("hello");}:--check --config emit_mode=filesexits0and rewrites the file in place1, and leaves the file unchanged (plain--emit filesstill writes, as expected)Closes #6999
Disclosure: prepared with AI assistance.