-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
The following code:
#![warn(noop_method_call)]
struct Board;
fn do_part1(boards: &Vec<Board>) {
let boards = boards.clone();
boards[0] = Board;
}
produces the following error:
error[E0596]: cannot borrow `*boards` as mutable, as it is behind a `&` reference
--> src/lib.rs:7:5
|
6 | let boards = boards.clone();
| ------ help: consider changing this to be a mutable reference: `&mut Vec<Board>`
7 | boards[0] = Board;
| ^^^^^^ `boards` is a `&` reference, so the data it refers to cannot be borrowed as mutable
For more information about this error, try `rustc --explain E0596`.
If we comment out boards[0] = Board;
, then the noop_method_call
lint will fire as expected. However, when that line is present, the lint never gets a chance to run, since the mutable borrow error occurs during MIR borrowchecking. Displaying the lint would help the user to resolve the problem, since the root cause is the fact that the .clone()
call produces a &Vec<Board>
, not a Vec<Board>
.
Originally identified in #91532
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.