Skip to content

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

Closed
@eric-seppanen

Description

@eric-seppanen

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

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