Skip to content
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

clippy::useless_asref warns in cases where map callback performs a method call before a clone #12135

Closed
alex opened this issue Jan 12, 2024 · 0 comments · Fixed by #12136
Closed
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

Comments

@alex
Copy link
Member

alex commented Jan 12, 2024

Summary

The new clippy::useless_asref warning is overly aggressive.

Lint Name

clippy::useless_asref

Reproducer

Given the following code:

pub struct Struct {
    field: Option<InnerStruct>
}

#[derive(Clone)]
pub struct Foo;

struct InnerStruct {
    x: Foo,
}

impl InnerStruct {
    fn method(&self) -> &Foo {
        &self.x
    }
}

pub fn f(x: &Struct) -> Option<Foo> {
    x.field.as_ref().map(|v| v.method().clone())
}

Clippy emits the following warning:

warning: this call to `as_ref.map(...)` does nothing
  --> src/lib.rs:19:5
   |
19 |     x.field.as_ref().map(|v| v.method().clone())
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.field.clone()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_asref
   = note: `#[warn(clippy::useless_asref)]` on by default

warning: `x` (lib) generated 1 warning (run `cargo clippy --fix --lib -p x` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.16s

However, this is not correct, x.field.clone() totally drops the call to method().

Version

rustc 1.77.0-nightly (62d7ed4a6 2024-01-11)
binary: rustc
commit-hash: 62d7ed4a6775c4490e493093ca98ef7c215b835b
commit-date: 2024-01-11
host: aarch64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6

Additional Labels

No response

@alex alex 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 Jan 12, 2024
bors added a commit that referenced this issue Jan 15, 2024
[`useless_asref`]: check that the clone receiver is the parameter

Fixes #12135

There was no check for the receiver of the `clone` call in the map closure. This makes sure that it's a path to the parameter.

changelog: [`useless_asref`]: check that the clone receiver is the closure parameter
bors added a commit that referenced this issue Jan 15, 2024
[`useless_asref`]: check that the clone receiver is the parameter

Fixes #12135

There was no check for the receiver of the `clone` call in the map closure. This makes sure that it's a path to the parameter.

changelog: [`useless_asref`]: check that the clone receiver is the closure parameter
@bors bors closed this as completed in 692f53f Jan 15, 2024
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant