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 map_identity: fix requires mutability #13904

Open
matthiaskrgr opened this issue Dec 29, 2024 · 0 comments · May be fixed by #13905
Open

FP map_identity: fix requires mutability #13904

matthiaskrgr opened this issue Dec 29, 2024 · 0 comments · May be fixed by #13905
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

.

Lint Name

map_identity

Reproducer

I tried this code:

// This is a reduction of a concrete test illustrating a case that was
// annoying to Rust developer stephaneyfx (see issue #46413).
//
// With resolving issue #54556, pnkfelix hopes that the new diagnostic
// output produced by NLL helps to *explain* the semantic significance
// of temp drop order, and thus why storing the result in `x` and then
// returning `x` works.

pub struct Statement;

pub struct Rows<'stmt>(&'stmt Statement);

impl<'stmt> Drop for Rows<'stmt> {
    fn drop(&mut self) {}
}

impl<'stmt> Iterator for Rows<'stmt> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        None
    }
}

fn get_names() -> Option<String> {
    let stmt = Statement;
    let rows = Rows(&stmt); 
    rows.map(|row| row).next() // boom

}

fn main() {}

I saw this happen:

warning: unnecessary map of the identity function
  --> src/main.rs:28:9
   |
28 |     rows.map(|row| row).next()
   |         ^^^^^^^^^^^^^^^ help: remove the call to `map`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_identity
   = note: `#[warn(clippy::map_identity)]` on by default

Applying the suggestion breaks the build

error[E0596]: cannot borrow `rows` as mutable, as it is not declared as mutable
  --> src/main.rs:28:5
   |
28 |     rows.next()
   |     ^^^^ cannot borrow as mutable
   |
help: consider changing this to be mutable
   |
27 |     let mut rows = Rows(&stmt);
   |         +++

For more information about this error, try `rustc --explain E0596`.

Version

rustc 1.85.0-nightly (8742e0556 2024-12-28)
binary: rustc
commit-hash: 8742e0556dee3c64f7144de2fb2e88936418865a
commit-date: 2024-12-28
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Additional Labels

No response

@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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Dec 29, 2024
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.

1 participant