rust-lang / rust-lang/rust-clippy
Suggest a solution to confusing precedence issues related to Borrow / RefCell
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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