Detect need for partial `self` borrows in methods and suggest alternatives
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
When encountering methods where getters on self are used which cause borrow errors like the following:
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/lib.rs:20:17
|
19 | let a = self.get_a();
| ---- immutable borrow occurs here
20 | let b = self.get_mut_b();
| ^^^^^^^^^^^^^^^^ mutable borrow occurs here
21 | println!("{a:?} {b:?}");
| - immutable borrow later used here
error[E0502]: cannot borrow `self.b` as mutable because it is also borrowed as immutable
--> src/lib.rs:29:9
|
29 | self.b.bar(self.get_a());
| ^^^^^^^---^----^^^^^^^^^
| | | |
| | | immutable borrow occurs here
| | immutable borrow later used by call
| mutable borrow occurs here
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/lib.rs:35:9
|
35 | self.bar(self.get_a());
| ^^^^^---^----^^^^^^^^^
| | | |
| | | immutable borrow occurs here
| | immutable borrow later used by call
| mutable borrow occurs here
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/lib.rs:37:9
|
37 | self.bar(&self.a);
| ^^^^^---^-------^
| | | |
| | | immutable borrow occurs here
| | immutable borrow later used by call
| mutable borrow occurs here
we should explain that get_a and get_mut_b borrow the entirety of Self making accessing any other method or field simultaneously will never work. If we ever get partial self borrows, then we should suggest it here.
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 linked Rust Playground reproducer and the E0502 diagnostics shown in the issue. Trace where these borrow errors are diagnosed and determine how the diagnostic should explain whole-self borrows; done means the current examples receive that explanation and partial-self-borrow support produces an appropriate suggestion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100