Skip to content

needless_borrowed_reference does not handle mutable references #16022

@Jarcho

Description

@Jarcho

Summary

needless_borrowed_reference currently only lints on shared references (e.g. &ref x), but mutable references can be linted in some cases.

Lint Name

needless_borrowed_reference

Reproducer

This should lint:

fn f(x: Option<&mut i32>) {
    if let Some(&mut ref mut x) = x {
        *x = 0;
    }
}

And suggest:

fn f(x: Option<&mut i32>) {
    if let Some(x) = x {
        *x = 0;
    }
}

Note that this can only be done if the reference can be moved. None of these should lint:

fn f(x: Option<&mut &mut i32>) {
    if let Some(&mut &mut ref mut x) = x {
        *x = 0;
    }
}
fn f(x: Option<&mut i32>) -> Option<&mut i32> {
    if let Some(x) = x {
        *x = 0;
    }
    x
}
struct WithDrop<'a>(Option<&'a mut i32>);
impl Drop for WithDrop<'_> { fn drop(&mut self) {} }

fn f(x: WithDrop) {
    if let Some(&mut ref mut x) = x.0 {
        *x = 0;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions