Skip to content

Incorrect suggestion when contains_key+insert is used in a while loop #10331

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

Closed
aditya-r-m opened this issue Feb 12, 2023 · 0 comments · Fixed by #10346
Closed

Incorrect suggestion when contains_key+insert is used in a while loop #10331

aditya-r-m opened this issue Feb 12, 2023 · 0 comments · Fixed by #10346
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

Comments

@aditya-r-m
Copy link

aditya-r-m commented Feb 12, 2023

Summary

When contains_key+insert is used in a while loop, Clippy suggestion is an unnecessary unrolling of the code.

Lint Name

map_entry

Reproducer

Original Code (Expected):

    while !fi.contains_key(&x) {
        fi.insert(x, i);
        i += 1;
        x = fx(x);
    }

Clippy Suggestion:

     if let std::collections::hash_map::Entry::Vacant(e) = fi.entry(x) {
         e.insert(i);
         i += 1;
         x = fx(x);
     } else while !fi.contains_key(&x) {
         fi.insert(x, i);
         i += 1;
         x = fx(x);
     }

Version

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: x86_64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.6

Additional Labels

No response

@aditya-r-m aditya-r-m 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 12, 2023
@bors bors closed this as completed in 0e40f94 Feb 14, 2023
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant