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

map_clone suggests removing clone altogether #6239

Closed
jyn514 opened this issue Oct 27, 2020 · 5 comments · Fixed by #6269
Closed

map_clone suggests removing clone altogether #6239

jyn514 opened this issue Oct 27, 2020 · 5 comments · Fixed by #6269
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@jyn514
Copy link
Member

jyn514 commented Oct 27, 2020

I tried this code (https://github.com/rust-lang/rust/blob/a6ff925f8b5598a1f6d84964525baa1d4a08fd63/compiler/rustc_codegen_ssa/src/back/write.rs#L1026):

    let cgcx = CodegenContext::<B> {
        incr_comp_session_dir: sess.incr_comp_session_dir_opt().map(|r| r.clone()),
    };

I expected to see this happen: Not sure this lint should have fired in the first place? This is a Ref, not an iterator.

Instead, this happened: Clippy suggests removing the map altogether, giving a type error.

error[E0308]: mismatched types
    --> compiler/rustc_codegen_ssa/src/back/write.rs:1014:32
     |
1014 |         incr_comp_session_dir: sess.incr_comp_session_dir_opt(),
     |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::path::PathBuf`, found struct `std::cell::Ref`
     |
     = note: expected enum `std::option::Option<std::path::PathBuf>`
                found enum `std::option::Option<std::cell::Ref<'_, std::path::PathBuf, >>`

Original diagnostics will follow.

warning: you are needlessly cloning iterator elements
    --> compiler/rustc_codegen_ssa/src/back/write.rs:1014:64
     |
1014 |         incr_comp_session_dir: sess.incr_comp_session_dir_opt().map(|r| r.clone()),
     |                                                                ^^^^^^^^^^^^^^^^^^^ help: remove the `map` call
     |
     = note: `#[warn(clippy::map_clone)]` on by default
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone

Meta

  • cargo clippy -V: clippy 0.0.212 (ffa2e7a 2020-10-24)
@jyn514 jyn514 added the C-bug Category: Clippy is not doing the correct thing label Oct 27, 2020
@giraffate
Copy link
Contributor

map_clone works with not only Iterator but Option although not written in doc.

@jyn514
Copy link
Member Author

jyn514 commented Oct 30, 2020

Gotcha, thanks. It should still suggest cloned() instead of removing the clone altogether, though.

@jyn514
Copy link
Member Author

jyn514 commented Oct 30, 2020

@rustbot modify labels: +A-suggestion +L-suggestion-causes-error

@rustbot rustbot added L-suggestion Lint: Improving, adding or fixing lint suggestions I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Oct 30, 2020
@jyn514
Copy link
Member Author

jyn514 commented Oct 30, 2020

Actually I think this is still wrong - r.clone() clones the path, not the Ref. So .cloned() wouldn't do anything here - it would leave you with an Option<Ref<PathBuf>> still. I think clippy isn't taking deref coercion into account.

@camsteffen
Copy link
Contributor

I can fix this.

@bors bors closed this as completed in c4fc076 Nov 11, 2020
bors added a commit that referenced this issue Nov 18, 2020
Improve doc about `map_clone`

A follow up of #6239 (comment).

`map_clone` works with not only `Iterator` but `Option` although not written in [doc](https://rust-lang.github.io/rust-clippy/master/#map_clone). Also, an example in the doc shows  a usage of dereferencing, but this isn't also written.

changelog: Improve doc about `map_clone`
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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants