You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cargo fix renames unused local variables from foo to _foo #54196
let device = &self.ctx.device;
let _device = &self.ctx.device;
I don't like that refactor because I might end up with local variables that do nothing. I think cargo fix should just ignore unused local variables all together. If cargo fix just renames local variables to hide the warnings from the compiler, then the compiler lint is a bit pointless.
Renaming unused function parameters seems fine.
Reacted by runiq, pyfisch, Todd York, Karel Peeters, Jokler, Jakub Beránek and Bennet Bleßmann
@MaikKlein To get a better understanding of your thought process, why do you feel cargo fix should treat local variables and function parameters differently here?
Now at one point you remove foo.do_something();, and then cargo fix will rename let foo = Foo::new(); to let _foo = Foo::new();
Now you have you might never find this in your code base. You really wanted to remove it, or maybe it was a bug in the first place that you are not using it anymore.
Just renaming it for the sake of removing the warning seems wrong.
Maybe it should also not rename parameters for free standing functions. I think that there are some cases in functions were you just want to rely on type inference and then the parameter should always be _foo, but I think that decision should come from the programmer.
It seems reasonable to rename parameters for trait impls because you can't just change the function signature, and you might not always want to use the parameter.
err.span_suggestion_with_applicability(sp,"try ignoring the field",
format!("{}: _", name),
Applicability::MachineApplicable);
}else{
err.span_suggestion_short_with_applicability(
sp,&suggest_underscore_msg,
format!("_{}", name),
Applicability::MachineApplicable,
);
We just need to change Applicability::MachineApplicable to Applicability::MaybeIncorrect to tell cargo fix not to auto-apply the suggestion. We have access to the HIR here, so setting the applicability conditionally based on whether it's a let binding or a method argument should be doable, although potentially slightly tricky.
I think you are right, this is misleading in your use case. Just wondering: At which points in your workflow are you running cargo fix?
I also think this is one of the cases where we need to differentiate between "cargo fix should fix it" and "get a lightbulb with auto-fix suggestions in your editor". Just because it might be auto-fixable doesn't mean we should automatically fix it. This is a discussion for a higher level issue, though.
Is anyone against changing this suggestion to simply be MaybeIncorrect instead of MachineApplicable?
Prepending an underscore to variable names is saying to the compiler "yes, I intentionally won't use this value", which should very much be a manual action.
I'm hesitant to use cargo fix, since it might silently hide a bunch of leftover variables from an in-process refactoring. I mostly want it to remove unnecessary parenthesis and fix my imports, this is overreaching a bit.
Reacted by Philipp, repi, Tor Hovland, Jokler, pwlodarczyk92 and Bennet Bleßmann
Activity
frewsxcv commentedon Sep 14, 2018
@MaikKlein To get a better understanding of your thought process, why do you feel
cargo fixshould treat local variables and function parameters differently here?MaikKlein commentedon Sep 14, 2018
For example
Now at one point you remove
foo.do_something();, and then cargo fix will renamelet foo = Foo::new();tolet _foo = Foo::new();Now you have you might never find this in your code base. You really wanted to remove it, or maybe it was a bug in the first place that you are not using it anymore.
Just renaming it for the sake of removing the warning seems wrong.
Maybe it should also not rename parameters for free standing functions. I think that there are some cases in functions were you just want to rely on type inference and then the parameter should always be
_foo, but I think that decision should come from the programmer.It seems reasonable to rename parameters for trait impls because you can't just change the function signature, and you might not always want to use the parameter.
zackmdavis commentedon Sep 15, 2018
I think I'm persuaded? Here's where the lint is issued:
rust/src/librustc/middle/liveness.rs
Lines 1546 to 1565 in f789b6b
We just need to change
Applicability::MachineApplicabletoApplicability::MaybeIncorrectto tellcargo fixnot to auto-apply the suggestion. We have access to the HIR here, so setting the applicability conditionally based on whether it's aletbinding or a method argument should be doable, although potentially slightly tricky.killercup commentedon Sep 15, 2018
I think you are right, this is misleading in your use case. Just wondering: At which points in your workflow are you running cargo fix?
I also think this is one of the cases where we need to differentiate between "cargo fix should fix it" and "get a lightbulb with auto-fix suggestions in your editor". Just because it might be auto-fixable doesn't mean we should automatically fix it. This is a discussion for a higher level issue, though.
MaikKlein commentedon Sep 18, 2018
I only ran
cargo fixonce for a project where I had a few warnings after I did some refactoring.KarelPeeters commentedon Jan 19, 2022
Is anyone against changing this suggestion to simply be
MaybeIncorrectinstead ofMachineApplicable?Prepending an underscore to variable names is saying to the compiler "yes, I intentionally won't use this value", which should very much be a manual action.
I'm hesitant to use
cargo fix, since it might silently hide a bunch of leftover variables from an in-process refactoring. I mostly want it to remove unnecessary parenthesis and fix my imports, this is overreaching a bit.Mark "unused binding" suggestion as maybe incorrect
Mark "unused binding" suggestion as maybe incorrect
Rollup merge of rust-lang#120470 - estebank:issue-54196, r=compiler-e…
Mark "unused binding" suggestion as maybe incorrect
Rollup merge of rust-lang#120470 - estebank:issue-54196, r=compiler-e…
Rollup merge of rust-lang#120470 - estebank:issue-54196, r=compiler-e…
Rollup merge of rust-lang#120470 - estebank:issue-54196, r=compiler-e…
Rollup merge of rust-lang#120470 - estebank:issue-54196, r=compiler-e…
Mark "unused binding" suggestion as maybe incorrect
Rollup merge of rust-lang#120470 - estebank:issue-54196, r=compiler-e…
Rollup merge of rust-lang#120470 - estebank:issue-54196, r=compiler-e…
Rollup merge of rust-lang#120470 - estebank:issue-54196, r=compiler-e…
Unrolled build for rust-lang#120470