Skip to content

.as_ref().map(|x| *x) is no longer warned #12142

Open
@y21

Description

@y21

Summary

We used to lint on Some(1).as_ref().map(|x| *x), but no longer do. The interaction between the two lints useless_asref and map_clone is important here.
Some more context (where this was found and probably for why this is): #12136 (comment).

Lint Name

map_clone / useless_asref

Reproducer

I tried this code:

fn main() {
  Some(1).as_ref().map(|x| *x);
  Some(1).as_ref().map(|&x| x);
}

I expected to see this happen:

warning: you are using an explicit closure for copying elements
 --> src/main.rs:2:3
  |
2 |   Some(1).as_ref().map(|x| *x);
  |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `Some(1).as_ref().copied()`
  |
  = 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 using an explicit closure for copying elements
 --> src/main.rs:3:3
  |
3 |   Some(1).as_ref().map(|&x| x);
  |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `Some(1).as_ref().copied()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone

Instead, this happened:
no warnings

Version

commit a71211d0b52c01f1b37fe544e0e13fd1bdc31979 (Jan 12)

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions