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

FP implicit-clone: &&Path.to_path_buf() #8227

Closed
matthiaskrgr opened this issue Jan 5, 2022 · 0 comments · Fixed by #8231
Closed

FP implicit-clone: &&Path.to_path_buf() #8227

matthiaskrgr opened this issue Jan 5, 2022 · 0 comments · Fixed by #8231
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

@matthiaskrgr
Copy link
Member

Summary

clippy suggestes invalid code when cloning &&Paths

Lint Name

clippy::implicit-clone

Reproducer

I tried this code:

use std::path::PathBuf;

fn main() {
    let a: Vec<&PathBuf> = Vec::new();
    let mut c: Vec<PathBuf> = Vec::new();

    for path /* &&PathBuf */  in a.iter() {
        c.push(path.to_path_buf());
    }
}

I saw this happen:
cargo clippy -- -Wclippy::implicit_clone

    Checking clippy_bug v0.1.0 (/tmp/clippy_bug)
warning: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenced type
 --> src/main.rs:8:21
  |
8 |         c.push(path.to_path_buf());
  |                     ^^^^^^^^^^^ help: consider using: `clone`
  |
  = note: requested on the command line with `-W clippy::implicit-clone`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implicit_clone

I expected to see this happen:

I expected the suggested code to compile, but it didn't 😅

error[E0308]: mismatched types
 --> src/main.rs:8:16
  |
8 |         c.push(path.clone());
  |                ^^^^^^^^^^^^- help: try using a conversion method: `.to_path_buf()`
  |                |
  |                expected struct `std::path::PathBuf`, found `&std::path::PathBuf`

Maybe clippy does not understand that we have a double-reference here?

Version

clippy 0.1.59 (7d6f948 2022-01-04)

rustc 1.59.0-nightly (7d6f94817 2022-01-04)
binary: rustc
commit-hash: 7d6f948173ccb18822bab13d548c65632db5f0aa
commit-date: 2022-01-04
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@matthiaskrgr matthiaskrgr 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 Jan 5, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jan 5, 2022
@bors bors closed this as completed in 7a4acf9 Jan 14, 2022
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.

2 participants