Skip to content

The NOOP_METHOD_CALL lint fails to run if a MIR error occurs #91536

Open
@Aaron1011

Description

@Aaron1011

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

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions