Skip to content

Consider not warning for map_clone with Arc::clone #12528

Closed
@stevenengler

Description

@stevenengler

Summary

Similar to how some people prefer to use the fully qualified Arc::clone(&foo) rather than foo.clone() for readability, people may also prefer foo.map(Arc::clone) rather than foo.cloned() to be more explicit that the Arc is being cloned. I think that the map_clone lint should not trigger for foo.map(Arc::clone) or for foo.map(Rc::clone).

This is of course subjective, but it would be a subjective choice to not show a warning, which I think is nicer.

Lint Name

map_clone

Reproducer

I tried this code:

use std::sync::Arc;

struct Foo;

fn main() {
    let x = Arc::new(Foo);
    let y = Some(&x);
    let _z = y.map(Arc::clone);
}

I saw this happen:

warning: you are explicitly cloning with `.map()`
 --> src/main.rs:8:14
  |
8 |     let _z = y.map(Arc::clone);
  |              ^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `y.cloned()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
  = note: `#[warn(clippy::map_clone)]` on by default

I expected to see this happen:

No lint.

Version

rustc 1.79.0-nightly (1388d7a06 2024-03-20)
binary: rustc
commit-hash: 1388d7a069d872bcfe5e5dd97ef61fa0a586fac0
commit-date: 2024-03-20
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2

Additional Labels

No response

Metadata

Metadata

Assignees

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