Skip to content

needless_pass_by_ref_mut false positive for required mut reference in async function #11216

Closed as not planned
@antangelo

Description

@antangelo

Summary

When passing a mut reference into an async function, the needless_pass_by_ref_mut warning is always triggered, even if the reference is used to call a function that requires it to be mutable. If the suggestion is taken, the resulting code does not compile.

Removing the async modifier from the requires_mut function in the reproducer (and replacing block_on with a direct function call) will produce correct output. In practice I discovered this bug with a trait parameter for an async_trait, but it presents on struct methods as well.

Lint Name

needless_pass_by_ref_mut

Reproducer

I tried this code:

struct S;

impl S {
    fn mut_fn(&mut self) {
        todo!();
    }
}

async fn uses_mut(s: &mut S) {
    s.mut_fn();
}

fn main() {
    let mut s = S;
    futures::executor::block_on(uses_mut(&mut s));
}

I saw this happen:

warning: this argument is a mutable reference, but not used mutably
 --> src/main.rs:9:22
  |
9 | async fn uses_mut(s: &mut S) {
  |                      ^^^^^^ help: consider changing to: `&S`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
  = note: `#[warn(clippy::needless_pass_by_ref_mut)]` on by default

I expected to see this happen:

No warning generated, the mut reference is required.

Version

rustc 1.73.0-nightly (1d56e3a6d 2023-07-22)
binary: rustc
commit-hash: 1d56e3a6d943062e41165bf07fea5ea8324ae011
commit-date: 2023-07-22
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions