Skip to content

Add rebind_fn_arg_as_mut lint#6245

Closed
cauebs wants to merge 1 commit intorust-lang:masterfrom
cauebs:rebind-fn-arg-as-mut
Closed

Add rebind_fn_arg_as_mut lint#6245
cauebs wants to merge 1 commit intorust-lang:masterfrom
cauebs:rebind-fn-arg-as-mut

Conversation

@cauebs
Copy link
Copy Markdown
Contributor

@cauebs cauebs commented Oct 27, 2020

Closes #1657.

I'm really not sure about the way I organized everything, and I think I might be forgetting some corner cases. Any feedback is welcome.

changelog: none

@rust-highfive
Copy link
Copy Markdown

r? @Manishearth

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 27, 2020
Comment thread clippy_lints/src/rebind_fn_arg_as_mut.rs
@cauebs cauebs marked this pull request as ready for review October 31, 2020 21:14
@cauebs
Copy link
Copy Markdown
Contributor Author

cauebs commented Oct 31, 2020

I just re-read the discussion in #1657 and noticed I still have to make it bail out when inside macros, right?

@Manishearth just as in my other PR, can you add the hacktoberfest-accepted label to this one before monday?

Copy link
Copy Markdown
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving for hacktoberfest, but the approach is incorrect.

This should not need a visitor IMO, the way to structure the code should be to look for let mut x = x; statements, and then check if the pattern x was defined in a fn arg by checking the source of its def id.

@Manishearth
Copy link
Copy Markdown
Member

And yes, you'll need to introduce a macro check, see how in_external_macro() is used in this repo to understand better.

@cauebs
Copy link
Copy Markdown
Contributor Author

cauebs commented Nov 1, 2020

Alright, cool! I ended up learning some new things. Thanks for the feedback.

Could you explain the difference between in_external_macro and Span::from_expansion?

@Manishearth
Copy link
Copy Markdown
Member

in_external_macro checks if the span comes from a macro from outside this crate, which is precisely what we want. Expansion spans are part of the puzzle in this check, but in_external_macro is the utility function that does the check.

Copy link
Copy Markdown
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl seems great, but i want a second opinion on the lint level. Unsure if it should be style or pedantic.

/// }
/// ```
pub REBIND_FN_ARG_AS_MUT,
style,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ebroto what do you think of the lint level for this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a style lint, but IMO we should:

  • Check that the scope of the linted binding is the same as the scope of the parameter (so, the whole function body)
  • Fix the known problem

If we want to leave these cases for a follow-up, I would add the lint to nursery for the time being.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can address your first suggestion this weekend.

As to the second, if by fixing you mean generating an applicable suggestion, I only didn't do it because the output looked awful: a suggestion to remove an entire line just prints out a blank that might confuse the user if they don't pay attention to the line number. If it's possible to make it better than that, let me know!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not being precise. I meant to avoid the false positive when the parameter has been shadowed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. But actually, I think that is fixed by my new impl. I'm comparing the resolution id for the variable (path) with the HIR id of the parameter's pattern. The thing is, I'm starting to think we should expand this lint to cover not only function parameters but any binding. Is there some case in which let mut x = x; is actually desired?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that is already covered by the shadow_same lint.

@bors
Copy link
Copy Markdown
Contributor

bors commented Nov 27, 2020

☔ The latest upstream changes (presumably #6389) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@giraffate
Copy link
Copy Markdown
Contributor

ping from triage @cauebs. Do you have any questions on how to proceed here?

@giraffate
Copy link
Copy Markdown
Contributor

ping from triage @cauebs. Thanks for contributing Clippy! Sadly this PR didn't have any update in the last 2 weeks. If you have more time to continue working on this, feel free to reopen.

@giraffate giraffate closed this Dec 22, 2020
@giraffate giraffate added S-inactive-closed Status: Closed due to inactivity and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-inactive-closed Status: Closed due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lint let mut x = x; where x is a function argument

7 participants