rust-lang / rust-lang/rust-clippy

Clippy doesnt see that value has been moved before suggesting

Open
#9,057 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Basically it didn't see that a value is moved when it suggested and the suggested code doesn't work

Lint Name

map_entry

Reproducer

I tried this code:

        if self.labels.contains_key(&label) {
            return Err(Box::new(SyntaxError(
                format!(
                    "Label '{}' has already been defined and cannot be redefined",
                    label
                ),
                self.current_token().position.clone(),
            )));
        } else {
            if label == "main" {
                self.has_main = true;
            }
            self.labels.insert(label, 0);
        }

I saw this happen:

warning: usage of `contains_key` followed by `insert` on a `HashMap`
   --> src/parser.rs:291:9
    |
291 | /         if self.labels.contains_key(&label) {
292 | |             return Err(Box::new(SyntaxError(
293 | |                 format!(
294 | |                     "Label '{}' has already been defined and cannot be redefined",
...   |
303 | |             self.labels.insert(label, 0);
304 | |         }
    | |_________^
    |
    = note: `#[warn(clippy::map_entry)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_entry
help: try this
    |
291 ~         if let std::collections::hash_map::Entry::Vacant(e) = self.labels.entry(label) {
292 +             if label == "main" {
293 +                 self.has_main = true;
294 +             }
295 +             e.insert(0);
296 +         } else {
  ...

I expected to see this happen:
I didn't expect it to give me wrong code, but I made some changes and it now no longer complains:

        match self.labels.entry(label) {
            Entry::Vacant(e) => {
                if e.key() == "main" {
                    self.has_main = true;
                }
                e.insert(0);
            }
            Entry::Occupied(e) => {
                return Err(Box::new(SyntaxError(
                    format!(
                        "Label '{}' has already been defined and cannot be redefined",
                        e.key()
                    ),
                    self.current_token().position.clone(),
                )));
            }
        }
Version
rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-unknown-linux-gnu
release: 1.61.0
LLVM version: 14.0.0
Additional Labels

@rustbot label +map_entry-suggestion-causes-error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the map_entry lint and reproduce the warning using the Rust example in the issue. Compare the generated suggestion with the working Entry::Vacant/Occupied rewrite shown, then verify that the suggested code compiles and preserves the moved value. The issue provides no file or test entry point.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.