Skip to content

overhaul mut_mut #15417

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

overhaul mut_mut #15417

wants to merge 11 commits into from

Conversation

ada4a
Copy link
Contributor

@ada4a ada4a commented Aug 5, 2025

This PR:

  • adds structured suggestions to all 3 lint scenarios
  • adds more examples of linted patterns, and recommended fixes
  • moves some test cases to _unfixable.rs, to allow running rustfix on the main file
  • stops the lint from firing multiple times on types like &mut &mut &mut T

Open questions:

  • I'd like to add a note explaining why chained &mut are useless.. but can't really come up with something consice. I very much don't like the one in the docs -- it's a bit too condescending imo.
  • see comments in the diff for more

I do realize that the PR ended up being quite wide-scoped, but that's primarily because once I added structured suggestions to one of the lint cases, ui_test started complaining about warnings remaining after the rustfix run, which of course had to with the fact that the other two lint cases didn't actually fix the code they linted, only warned. I guess ui_test could be smarter about this, by understanding that if a warning was created without a suggestion, then that warning will still remain in the fixed file. But oh well.

changelog: [mut_mut]: add structured suggestions, improve docs

fixes #13021

@rustbot
Copy link
Collaborator

rustbot commented Aug 5, 2025

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 5, 2025
//~| mut_mut
}

let mut z = inline!(&mut $(&mut 3u32));
Copy link
Contributor Author

@ada4a ada4a Aug 5, 2025

Choose a reason for hiding this comment

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

I stopped the lint from firing when inside macros, because the $(&mut 3u32) thing caused a bad suggestion:

&mut 3u32mut $(&mut u32)

should I try bringing the suggestion back, and if so, how?

"this expression mutably borrows a mutable reference. Consider reborrowing",
"this expression mutably borrows a mutable reference",
|diag| {
diag.span_suggestion(expr.span, "reborrow instead", sugg, applicability);
Copy link
Contributor Author

@ada4a ada4a Aug 5, 2025

Choose a reason for hiding this comment

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

From my experience, not many people know about reborrowing -- maybe the message for the case that recommends it should link to some page that explains it?

Admittedly this is less necessary now that we've got a structured suggestion; people can just search for the term separately if they're interested

self.cx,
MUT_MUT,
expr.hir_id,
expr.span,
"generally you want to avoid `&mut &mut _` if possible",
"an expression of form `&mut &mut _`",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this message helpful enough?

Copy link

github-actions bot commented Aug 5, 2025

Lintcheck changes for dcb123e

Lint Added Removed Changed
clippy::mut_mut 0 0 11

This comment will be updated if you push new changes

@ada4a ada4a force-pushed the mut-mut branch 2 times, most recently from a81c9a6 to c26eb82 Compare August 5, 2025 12:57
@ada4a
Copy link
Contributor Author

ada4a commented Aug 5, 2025

@cmrschwarz let me know what you think! You can see the change in the diagnostic messages in the .stderr files

@cmrschwarz
Copy link

@cmrschwarz let me know what you think! You can see the change in the diagnostic messages in the .stderr files

Just looking at the structured suggestions, this looks exactly like what I hoped for.
Thank you for working on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mut_mut complains when borrowing from a slice of mutable references
4 participants