cargo: `cargo fix`: Improve handling of MaybeIncorrect suggestions
When the compiler or clippy produce suggestions, some of them are marked as MaybeIncorrect; i.e. the suggestion should probably not be automatically applied. rustfix currently doesn’t touch these.
This leads to confusion like in https://github.com/rust-lang/cargo/issues/8806, and is somewhat frustrating. We should at the very least be clear that such suggestions were not applied.
Ideally, we should provide a mode where you can apply them anyway (cargo clippy --fix --apply-all-suggestions?), or apply them using a “pick and choose” mode. We can then mention it when people have suggestions that were not applied.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 11
- Comments: 43 (35 by maintainers)
This exists today as
__CARGO_FIX_YOLO=1. It would be great to make that more discoverable with a CLI option.All the kids are saying it
@camsteffen It lets users set some minimum applicability level with rustfix, instead of going with the default.
The way I see it, the applicabilities go in order of least to most applicable:
For example, quite often with the latter two I’m actually comfortable running rustfix on my codebase, with the former two I kinda want to review them case by case.
That also sounds reasonable to me. (Edit: no pun intended)
Especially this reason could be used in such a pick and choose prompt.
In Clippy
MaybeIncorrectis used interchangeable for both cases. Sometimes we use it, if the suggestion might result in new errors (e.g. the borrow checker might complain or an additional (trait) import is needed). And sometimes, because it’s a semantic change (e.g. removes a possibly panicking expression). At least, that’s how I use it and recommend it in reviews.So maybe splitting it up in
MayIntroduceNewErrorandSemanticChange(with possibly better names) would make sense.The idea behind splitting is that
MayNotCompilesuggestions may compile, and guarantee not to change behavior if they do. So a tool like rust fix could apply them, check compiling the result and roll them back on failure. On the other handMayChangeBehaviorhave no such guarantee.Changing the names will help us keep track of which suggestions have been vetted.
I think it does make sense to split that. In Clippy, we use this level for both of those use cases interchangeably.
EDIT: Wait, I suggested that above. I couldn’t remember that I did that 😅
Yeah I guess that r-a isn’t batching things the way
rustfixdoes so it’s all user-driven anyway.It also means that the user workflow can be “run rustfix with
PotentialCompileError, and then manually fix any errors they see”.Whereas for
PotentialBehaviorChangethe workflow will probably be to review them one by one, and ideally put them in a separate commit for cleaner review (as a reviewer I’d want the direct rustfix fixes in one commit,PotentialCompileErrorfixups in another, andPotentialBehaviorChangeones in a third)Yeah, having that split makes sense to me. Migration is going to be annoying though.
Thanks a lot @jyn514 this is incredibly helpful!
In my case, I need to rename struct fields from
inputtoinputsandoutputtooutputsbecause they are bothVecs (carrying different types so mixup is impossible). If I do this as a single change - change the field names and then funcargo fixthen this is not only entirely correct but there’s literally nothing that can go wrong. All the code is tested properly and there are no fields that have similar names and same types to be confused with. I’ve just made a demo commit changing >200 LOC in a matter of minutes.So I think for cases like this it’s not really “YOLO” and it’d be nice to expose it to users more cleanly but even having it at all is already a huge deal.
I think Unspecified was for diagnostics that have not been vetted as to whether or not they work correctly. I believe that was used when the applicability system was put in, and there was a huge list of diagnostics to update, so Unspecified was the default until a more appropriate category could be determined.
That’s
Unspecified, no?https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/enum.Applicability.html
As a clippy user I would also be interested in a better handling of
MaybeIncorrectoptions.clippy::semicolon_if_nothing_returnedis the canonical example of the lint that if failing sometimes automatically is not an issue, and on a large codebase it’s much easier to dealt with only the corner cases manually than all the occurrences.The split of
MaybeIncorrectis probably worth being nominated in a clippy meeting?We may use the split values for reporting. Users would likely be interested what they should expect. Also if we had a “might not compile, but if it does, it’s OK” category, rustfix could try to auto-fix and back out on errors.
I also agree with splitting
MaybeIncorrect. PerhapsMayFailToCompile,MayChangeBehaviorandMayBeCounterproductive(for things that might compile and keep behavior, but not help readability or perf).Hello, I’ve been following this conversation but haven’t had a chance to dig in properly. I YOLO’d this evening to help me with some module refactoring and it certainly did what it said on the tin. Just recording four and a half potential bugs here while I’ve got them in front of me. This is git bash running on regular Windows 10 for reference:
The
ufromusegot dropped somehow:Where use was required in multiple places, it was added multiple times:
This could have been because of poor code quality initially with non-standard imports, but it seems like
usesometimes reexports types from the module and sometimes takes them from the original crate? I’m not good enough at Rust to know exactly what’s happening here, and what should happen:Do macro import suggestions count as
MaybeIncorrect? YOLO wouldn’t implement those suggestions, which perhaps means it doesn’t?I don’t know if this is really a bug, but the format that was used for the
uselines seems a bit odd - lots of blank lines added between statements. Are fixes supposed to be rustfmt compatible?Proabably “you only live once”, which is
The problem is that MaybeIncorrect is often not stuff that is incorrect because it won’t compile, it’s incorrect because it changed the semantics of the program in a way clippy thinks is according to intention but might not be