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

or_fun_call bad output with vec![] #1667

Closed
leonardo-m opened this issue Apr 9, 2017 · 6 comments
Closed

or_fun_call bad output with vec![] #1667

leonardo-m opened this issue Apr 9, 2017 · 6 comments

Comments

@leonardo-m
Copy link

#![allow(unused_variables)]
fn main() {
    use std::collections::HashMap;
    let mut hash = HashMap::<u32, Vec<u32>>::new();
    let foo = hash.entry(1).or_insert(vec![]);
}

Gives:

warning: use of `or_insert` followed by a function call
 --> src\main.rs:5:15
  |
5 |     let foo = hash.entry(1).or_insert(vec![]);
  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(or_fun_call)] on by default
help: try this
  |     let foo = hash.entry(1).or_insert_with(|| < [ _ ] > :: into_vec ( box [ $ ( $ x ) , * ] ));
  = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#or_fun_call

A nicer looking warning should show .or_insert_with(|| vec![]);

@clarfonthey
Copy link
Contributor

This potentially could recommend or_insert_with(Vec::new) as well.

@yacoob
Copy link

yacoob commented Dec 28, 2018

A variation on this:

warning: use of `or_insert` followed by a function call
  --> src/bin/04.rs:88:60
   |
88 |         let minutes = &mut minutes_frequency.entry(*guard).or_insert(vec![0; 60]);
   |                                                            ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(|| $ crate :: vec :: from_elem ( $ elem , $ n ))`

.or_insert_with(|| vec![0; 60]) work just as well. :D I guess clippy expands the vec! macro, and then tries to advise on that.

@clarfonthey
Copy link
Contributor

in that case, you are actually allocating the Vec and should be using or_insert_with.

@yacoob
Copy link

yacoob commented Dec 28, 2018

Yup, that's what I've meant; updated the comment to be less vague.

@clarfonthey
Copy link
Contributor

Ah, gotcha!

@tesuji
Copy link
Contributor

tesuji commented May 2, 2020

Current output:

warning: use of `or_insert` followed by a function call
 --> src/main.rs:5:29
  |
5 |     let foo = hash.entry(1).or_insert(vec![0; 60]);
  |                             ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(|| vec![0; 60])`
  |
  = 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

So this issue is fixed? @phansch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants