Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
antangelo opened this issue Jul 23, 2023 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@antangelo
Copy link

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

@antangelo antangelo added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 23, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 23, 2023
@y21
Copy link
Member

y21 commented Jul 23, 2023

I believe this is the same issue as #11179 (comment), which was fixed in #11184.
I can't reproduce this on the master branch, so that's probably it.

@Centri3
Copy link
Member

Centri3 commented Jul 23, 2023

Yep, damn that was fast @y21!

I'm closing as it was fixed there. It should be nightly in a few days if all goes well :)

@Centri3 Centri3 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

4 participants