Skip to content

Errorneous suggestion with map + Arc::clone of custom expression #12560

Closed
@ProgramCrafter

Description

@ProgramCrafter

Summary

Clippy didn't recognize that .cloned() method will have behaviour different from existing map + Arc::clone. It seems that it interpreted Arc::clone(&t.track) as Arc::clone(t).

Lint Name

clippy::map_clone

Reproducer

I tried to run Clippy on webrtc-rs repository. It created a false positive result (full log).

The following errors were reported:
error[E0308]: mismatched types
   --> webrtc\src\rtp_transceiver\rtp_receiver\mod.rs:496:13
    |
496 |             tracks.first().cloned()
    |             ^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<Arc<TrackRemote>>`, found `Option<TrackStreams>`
    |
    = note: expected enum `std::option::Option<std::sync::Arc<track::track_remote::TrackRemote>>`
               found enum `std::option::Option<track::TrackStreams>`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: you are explicitly cloning with `.map()`
   --> webrtc\src\rtp_transceiver\rtp_receiver\mod.rs:496:13
    |
496 |             tracks.first().map(|t| Arc::clone(&t.track))
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `tracks.first().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

warning: you are explicitly cloning with `.map()`
   --> webrtc\src\rtp_transceiver\mod.rs:304:9
    |
304 |         self.mid.get().map(Clone::clone)
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `self.mid.get().cloned()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone

Version

rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-pc-windows-msvc
release: 1.77.0
LLVM version: 17.0.6

Additional Labels

@rustbot label +l-suggestion-causes-error

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