Skip to content

Invalid suggestion of or_fun_call causes a compiler error error[E0277]: expected a FnOnce<()> closure, found Vec<_>`` #6748

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
mgacek8 opened this issue Feb 16, 2021 · 4 comments · Fixed by #6790
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@mgacek8
Copy link
Contributor

mgacek8 commented Feb 16, 2021

Lint name: or_fun_call

I tried this code:

use std::collections::HashMap;
fn main() {
    let mut hash_map: HashMap<_, Vec<usize>> = HashMap::new();
    let _ = hash_map.entry("test".to_owned()).or_insert(vec![]);

    println!("hash_map: {:?}", hash_map);
}

I expected to see this happen: suggestion to use or_insert_with(|| vec![]) instead of or_insert(vec![])

Instead, this happened: clippy suggested me to use or_insert_with(vec![])

warning: use of `or_insert` followed by a function call
 --> src/main.rs:4:47
  |
4 |     let _ = hash_map.entry("test".to_owned()).or_insert(vec![]);
  |                                               ^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(vec![])`
  |
  = note: `#[warn(clippy::or_fun_call)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call

warning: 1 warning emitted

and that suggestion causes the compiler error.

error[E0277]: expected a `FnOnce<()>` closure, found `Vec<_>`
 --> src/main.rs:4:62
  |
4 |     let _ = hash_map.entry("test".to_owned()).or_insert_with(vec![]);
  |                                                              ^^^^^^ expected an `FnOnce<()>` closure, found `Vec<_>`
  |
  = help: the trait `FnOnce<()>` is not implemented for `Vec<_>`
  = note: wrap the `Vec<_>` in a closure with no arguments: `|| { /* code */ }`
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

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

Playground link

I found that this only happens when the vector is empty.
When it is not, the suggestion is correct:

4 |     let _ = hash_map.entry("test".to_owned()).or_insert(vec![123]);
  |                                               ^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(|| vec![123])`

Meta

  • clippy 0.1.52 (5fa22fe 2021-02-14)
  • rustc stable 1.50.0
@mgacek8 mgacek8 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 16, 2021
@camsteffen
Copy link
Contributor

Could be fixed with #3812.

@camsteffen camsteffen added the good-first-issue These issues are a good way to get started with Clippy label Feb 16, 2021
@mgacek8
Copy link
Contributor Author

mgacek8 commented Feb 22, 2021

Shouldn't this lint for .or_insert(vec![]) suggest using .or_insert_with(Vec::new) (instead of .or_insert_with(vec![]) that causes a compiler error)?
And then, as in #3812, there could be another link that suggest std::collections::btree_map::Entry::or_default instead of .or_insert_with(Vec::new).
Please let me know if it makes sense to fix that (.or_insert(vec![]) -> .or_insert_with(Vec::new)) . If it does I'd like to do it.

@camsteffen
Copy link
Contributor

We should have both fixed actually, since vec![] could be any macro that calls any function. When #3812 is fixed, it should just suggest or_default() instead of vec![].

So @mgacek8 if you would like to just fix the this issue that would be great!

@mgacek8
Copy link
Contributor Author

mgacek8 commented Feb 22, 2021

@rustbot claim

bors added a commit that referenced this issue Feb 25, 2021
or_fun_call: fix suggestion for `or_insert(vec![])`

fixes #6748
changelog: or_fun_call: fix suggestion for `or_insert(vec![])` on `std::collections::hash_map::Entry` or `std::collections::btree_map::Entry`
@bors bors closed this as completed in 5c6cd87 Feb 25, 2021
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 good-first-issue These issues are a good way to get started with Clippy 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.

2 participants