rust-lang / rust-lang/rust-clippy

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

Open
#4,598 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-style T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no source file, test, or entry point. Start by locating Clippy’s lint area and related borrow lints, then add coverage showing when .borrow() or .borrow_mut() should be replaced by references and verify the suggested replacements are accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.