Skip to content

False positive  #12828

@bazhenov

Description

@bazhenov

Summary

Right now clippy complains about cloning Option<Rc<_>> with as_ref().map(Rc::clone).

In this particular case I believe this is justified approach to use explicit Rc::clone. It helps to harden code to make sure no deep clone would happen in case of future changes. I believe the same logic applies to Arc.

By using Rc::clone for reference counting, we can visually distinguish between the deep-copy kinds of clones and the kinds of clones that increase the reference count. When looking for performance problems in the code, we only need to consider the deep-copy clones and can disregard calls to Rc::clone.

https://doc.rust-lang.org/book/ch15-04-rc.html#using-rct-to-share-data

Lint Name

clippy::useless_asref

Reproducer

I tried this code:

use std::rc::Rc;

fn main() {
    let value: Option<Rc<Vec<u8>>> = Some(Rc::new(vec![]));
    let _rc_clone = value.as_ref().map(Rc::clone);
}

I saw this happen:

warning: this call to `as_ref.map(...)` does nothing
 --> src/main.rs:5:21
  |
5 |     let _rc_clone = value.as_ref().map(Rc::clone);
  |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `value.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

I expected to see this happen:
No warnings

Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: aarch64-apple-darwin
release: 1.78.0
LLVM version: 18.1.2

Additional Labels

No response

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 have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions