rust-lang / rust-lang/rust-clippy

Suggest a solution to confusing precedence issues related to Borrow / RefCell

Open
#11,040 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

This lint would catch a common mistake with RefCell / Borrow, where Borrow::borrow can confusingly take precedence over RefCell::borrow, resulting in incredibly unhelpful diagnostics that are unrelated to the problem (see: https://github.com/rust-lang/rust/issues/94858).

Ideally this should be fixed in the compiler itself, but it would also be nice to have a lint to catch this issue, since it's a fairly common issue that tends to bite new users.

Advantage

No response

Drawbacks

No response

Example
use std::{borrow::BorrowMut, cell::RefCell, rc::Rc};

pub struct Foo {
    bar: (),
}

fn foo() {
    let foo = Rc::new(RefCell::new(Foo { bar: () }));
    // lint here: consider explicitly specifying either RefCell::borrow_mut or BorrowMut::borrow_mut
    let bar = foo.borrow_mut().bar;
}

Could be written as:

use std::{borrow::BorrowMut, cell::RefCell, rc::Rc};

pub struct Foo {
    bar: (),
}

fn foo() {
    let foo = Rc::new(RefCell::new(Foo { bar: () }));
    let bar = RefCell::borrow_mut(&foo).bar;
}

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

Start with the RefCell/Borrow example in the issue and read the referenced rust-lang/rust#94858 discussion to understand the precedence problem. The work is done when Clippy can identify this confusing call pattern and provide guidance to explicitly select RefCell::borrow_mut or BorrowMut::borrow_mut.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.