Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive with map_clone #12271

Closed
Luk-ESC opened this issue Feb 11, 2024 · 1 comment · Fixed by #12282
Closed

False positive with map_clone #12271

Luk-ESC opened this issue Feb 11, 2024 · 1 comment · Fixed by #12282
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@Luk-ESC
Copy link
Contributor

Luk-ESC commented Feb 11, 2024

Summary

calling .map(|foo| U::clone(...)) on an Option<&T> or Iterator over &T incorrectly triggers the map_clone lint.

Notes:

  • It has to be a &T
  • U can be any type that implements Clone
  • It does not have to be .map(|foo| U::clone(&foo)), the lint will trigger no matter what value is put into U::clone (foo can be unused)
  • This affects both Options and Iterators.

Lint Name

map_clone

Reproducer

I tried this code:

fn main() {
    let x: Option<&u8> = None;
    let y = x.map(|x| String::clone(loop {}));
}

I saw this happen:

...
warning: you are explicitly cloning with `.map()`
 --> src/main.rs:3:13
  |
3 |     let y = x.map(|x| String::clone(loop {}));
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `x.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
...

I expected to see this happen:
map_clone not triggering.

The same issue appears with

fn main() {
    let x: Vec<&u8> = vec![];
    let y = x.into_iter().map(|x| String::clone(loop {}));
}

Version

rustc 1.78.0-nightly (6cc484351 2024-02-10)
binary: rustc
commit-hash: 6cc4843512d613f51ec81aba689180c31b0b28b6
commit-date: 2024-02-10
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 17.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

@Luk-ESC Luk-ESC added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 11, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Feb 11, 2024
@maekawatoshiki
Copy link
Contributor

I'm working on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants