Skip to content

clippy::useless_asref suggestion doesn't compile in 1.77.0 #12357

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
eric-seppanen opened this issue Feb 26, 2024 · 6 comments · Fixed by #14174
Closed

clippy::useless_asref suggestion doesn't compile in 1.77.0 #12357

eric-seppanen opened this issue Feb 26, 2024 · 6 comments · Fixed by #14174
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

@eric-seppanen
Copy link

eric-seppanen commented Feb 26, 2024

Summary

The useless_asref lint (in rust 1.77) suggests a fix that does not compile.

Perhaps related to #12148?

Lint Name

useless_asref

Reproducer

I tried this code:

use std::ops::Deref;
use std::sync::Arc;

pub struct Wrap<T> {
    inner: T,
}

impl<T> Deref for Wrap<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

pub struct Thing {
    current: Option<Wrap<Arc<u32>>>,
}

impl Thing {
    pub fn fetch(&self) -> Option<Arc<u32>> {
        self.current.as_ref().map(|p| Arc::clone(p))
    }
}

I saw this happen:

warning: this call to `as_ref.map(...)` does nothing
  --> src/lib.rs:22:9
   |
22 |         self.current.as_ref().map(|p| Arc::clone(p))
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `self.current.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

And if I apply the suggestion:

error[E0599]: the method `clone` exists for enum `Option<Wrap<Arc<u32>>>`, but its trait bounds were not satisfied
   --> src/lib.rs:22:22
    |
4   | pub struct Wrap<T> {
    | ------------------ doesn't satisfy `Wrap<Arc<u32>>: Clone`
...
22  |         self.current.clone()
    |                      ^^^^^ method cannot be called on `Option<Wrap<Arc<u32>>>` due to unsatisfied trait bounds
    |
    |
570 | pub enum Option<T> {
    | ------------------ doesn't satisfy `Option<Wrap<Arc<u32>>>: Clone`
    |
    = note: the following trait bounds were not satisfied:
            `Wrap<Arc<u32>>: Clone`
            which is required by `Option<Wrap<Arc<u32>>>: Clone`

I expected to see this happen:

Either no lint should fire, or the suggested fix should compile.

Version

rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6
@eric-seppanen eric-seppanen 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 Feb 26, 2024
@eric-seppanen
Copy link
Author

@rustbot label +I-suggestion-causes-error

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Feb 26, 2024
@eric-seppanen eric-seppanen changed the title clippy::useless_asref suggestion doesn't compile in 1.77.0-beta.5 clippy::useless_asref suggestion doesn't compile in 1.77.0 Mar 27, 2024
@eric-seppanen
Copy link
Author

Verified that the issue is still present in stable 1.77.0.

@eric-seppanen
Copy link
Author

Looks like my example would be covered by #12535; I'm leaving this open though in case this is a general problem that would affect a wider range of types (besides Arc, Rc, etc.)

@ctron
Copy link

ctron commented Apr 11, 2024

A pattern I see this showing up is Results where the T: Clone, but not for E. And it's pretty annoying.

Still shows up in 1.77.2.

@eric-seppanen
Copy link
Author

Still broken in 1.79; 1.80-beta.4; and nightly-2024-06-25.

@eric-seppanen
Copy link
Author

Still broken in 1.82; 1.83.0-beta.4; and nightly-2024-11-01.

github-merge-queue bot pushed a commit that referenced this issue Feb 22, 2025
… `.clone()` (#14174)

fixes #12357

changelog: [`useless_asref`]: don't suggest to use `.clone()` if the
target type doesn't implement the `Clone` trait
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

Successfully merging a pull request may close this issue.

3 participants