rust-lang / rust-lang/rust-clippy

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

Open
#12,142 1 comment 0 reactions 1 assignee View on GitHub

@Jacherr is already working on this.

Since Jun 2, 2024.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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): https://github.com/rust-lang/rust-clippy/pull/12136#discussion_r1451562174.

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.