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_entry causes error: unable to infer None type #9973

Open
matthiaskrgr opened this issue Nov 28, 2022 · 0 comments
Open

map_entry causes error: unable to infer None type #9973

matthiaskrgr opened this issue Nov 28, 2022 · 0 comments
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_entry

Reproducer

I tried this code:
apparently this was an official test at some point 🙃

#![allow(unused, clippy::needless_pass_by_value)]
use std::collections::{BTreeMap, HashMap};
use std::hash::Hash;

fn insert_if_absent2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
    if !m.contains_key(&k) { m.insert(k, v) } else { None };
}

fn main() {}

I saw this happen:
cargo clippy --fix -- -Aclippy::all -Wclippy::map_entry

warning: failed to automatically apply fixes suggested by rustc to crate `f`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0282]: type annotations needed
 --> src/main.rs:7:5
  |
7 |     None } else { None };
  |     ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
  |
help: consider specifying the generic argument
  |
7 |     None::<T> } else { None };
  |         +++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
Original diagnostics will follow.

warning: usage of `contains_key` followed by `insert` on a `HashMap`
 --> src/main.rs:6:5
  |
6 |     if !m.contains_key(&k) { m.insert(k, v) } else { None };
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_entry
  = note: requested on the command line with `-W clippy::map-entry`
help: try this
  |
6 ~     if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) { e.insert(v);
7 ~     None } else { None };
  |

warning: `f` (bin "f") generated 1 warning (run `cargo fix --bin "f"` to apply 1 suggestion)
warning: `f` (bin "f" test) generated 1 warning (1 duplicate)

I expected to see this happen:

Version

rustc 1.67.0-nightly (1eb62b123 2022-11-27)
binary: rustc
commit-hash: 1eb62b1235fd77200e6bd967d70e83c0f2497233
commit-date: 2022-11-27
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

@matthiaskrgr matthiaskrgr added 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 I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 28, 2022
@matthiaskrgr matthiaskrgr changed the title map_entry causes unable to infer None type map_entry causes error: unable to infer None type Nov 28, 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

No branches or pull requests

1 participant