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

Lint against x.borrow() / x.borrow_mut() in favor of &x and &mut x when fit #4598

Open
upsuper opened this issue Sep 29, 2019 · 0 comments
Open
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group T-middle Type: Probably requires verifiying types

Comments

@upsuper
Copy link
Contributor

upsuper commented Sep 29, 2019

In a discussion in our Rust group today, we saw one wrote something like

// foo is owned, and foo.x has type HashMap
let x = foo.x.borrow_mut();
// something mutating through x

and then got trouble moving ownership of items from x (which is unrelated).

This code really confused me because I don't recall that HashMap has borrow_mut, until someone brought up that there is a blanket impl of BorrowMut for all type to their mutable reference.

It doesn't feel idiomatic to use this impl in normal code. If one wants to get a mutable reference, they should just use &mut. .borrow_mut() is more lengthy and confusing. Similar for .borrow().

Thus I suggest that Clippy should lint against use of .borrow() and .borrow_mut() via that blanket impl, and suggest using & and &mut instead correspondingly.

@flip1995 flip1995 added L-style Lint: Belongs in the style lint group E-medium Call for participation: Medium difficulty level problem and requires some initial experience. A-lint Area: New lints T-middle Type: Probably requires verifiying types labels Oct 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

2 participants