Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo fix renames unused local variables from foo to _foo #54196

Closed
Tracked by #84
MaikKlein opened this issue Sep 13, 2018 · 6 comments · Fixed by #120470
Closed
Tracked by #84

cargo fix renames unused local variables from foo to _foo #54196

MaikKlein opened this issue Sep 13, 2018 · 6 comments · Fixed by #120470
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@MaikKlein
Copy link
Contributor

For example:

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.

@frewsxcv
Copy link
Member

@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?

@zackmdavis zackmdavis added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 14, 2018
@MaikKlein
Copy link
Contributor Author

MaikKlein commented Sep 14, 2018

For example

let foo = Foo::new();
...
foo.do_something();

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.


impl Foo for Bar {
    fn foo(&self, _baz: Baz){}
}

@zackmdavis
Copy link
Member

I think I'm persuaded? Here's where the lint is issued:

if is_assigned {
self.ir.tcx
.lint_hir_note(lint::builtin::UNUSED_VARIABLES, hir_id, sp,
&format!("variable `{}` is assigned to, but never used",
name),
&suggest_underscore_msg);
} else if name != "self" {
let msg = format!("unused variable: `{}`", name);
let mut err = self.ir.tcx
.struct_span_lint_hir(lint::builtin::UNUSED_VARIABLES, hir_id, sp, &msg);
if self.ir.variable_is_shorthand(var) {
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.

@killercup
Copy link
Member

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
Copy link
Contributor Author

I only ran cargo fix once for a project where I had a few warnings after I did some refactoring.

@crlf0710 crlf0710 added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 11, 2020
@KarelPeeters
Copy link

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.

estebank added a commit to estebank/rust that referenced this issue Jan 29, 2024
Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
estebank added a commit to estebank/rust that referenced this issue Jan 30, 2024
Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 30, 2024
…rrors

Mark "unused binding" suggestion as maybe incorrect

Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
estebank added a commit to estebank/rust that referenced this issue Jan 30, 2024
Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 30, 2024
…rrors

Mark "unused binding" suggestion as maybe incorrect

Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
Nadrieril added a commit to Nadrieril/rust that referenced this issue Jan 31, 2024
…rrors

Mark "unused binding" suggestion as maybe incorrect

Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
Nadrieril added a commit to Nadrieril/rust that referenced this issue Jan 31, 2024
…rrors

Mark "unused binding" suggestion as maybe incorrect

Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 31, 2024
…rrors

Mark "unused binding" suggestion as maybe incorrect

Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 7, 2024
…rrors

Mark "unused binding" suggestion as maybe incorrect

Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
Nadrieril added a commit to Nadrieril/rust that referenced this issue Feb 7, 2024
…rrors

Mark "unused binding" suggestion as maybe incorrect

Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
Nadrieril added a commit to Nadrieril/rust that referenced this issue Feb 7, 2024
…rrors

Mark "unused binding" suggestion as maybe incorrect

Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
@bors bors closed this as completed in aef18c9 Feb 7, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 7, 2024
Rollup merge of rust-lang#120470 - estebank:issue-54196, r=compiler-errors

Mark "unused binding" suggestion as maybe incorrect

Ignoring unused bindings should be a determination made by a human, `rustfix` shouldn't auto-apply the suggested change.

Fix rust-lang#54196.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants