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 lint shouldn't ignore indexing to HashMap types #6266

Closed
tesuji opened this issue Oct 29, 2020 · 5 comments · Fixed by #6267
Closed

or_fun_call lint shouldn't ignore indexing to HashMap types #6266

tesuji opened this issue Oct 29, 2020 · 5 comments · Fixed by #6267
Labels
C-bug Category: Clippy is not doing the correct thing C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@tesuji
Copy link
Contributor

tesuji commented Oct 29, 2020

See also the perf improvement in rustc: rust-lang/rust#78508.

Currently, or_fun_call lint doesn't lint on indexing to HashMap types.

I tried this code:

#![warn(clippy::or_fun_call)]
use std::collections::HashMap;
pub fn foo(opt: Option<u32>, map: HashMap<u32, u32>, idx: u32) -> u32 {
    opt.unwrap_or(map[&idx])
}

I expected to see this happen: A warning to unwrap_or method call because
map[&idx] maybe expensive (indexing to a hashmap).

Instead, this happened: No warning at all.

Meta

  • cargo clippy -V: 0.0.212 (2020-10-28 31ee872) from playground
  • rustc -Vv: (2020-10-28 31ee872)

Maybe a duplicate of #5821 .

@tesuji tesuji added the C-bug Category: Clippy is not doing the correct thing label Oct 29, 2020
@camsteffen
Copy link
Contributor

map[&idx] is syntactic sugar for map.index(&idx) (a function call). The specific type of map doesn't really matter.

@ghost
Copy link

ghost commented Oct 30, 2020

Should all indexing be linted or specifically indexing HashMap types? Is it OK to index a Vec?

@tesuji
Copy link
Contributor Author

tesuji commented Oct 30, 2020

I would like to exclude Vec, slice and array types since indexing into those types
is usually just a pointer arithmetic and a dereference.

@camsteffen
Copy link
Contributor

Excluding slices seems arbitrary to me. If it is not const, why would you not defer it? Especially since the lambda will be inlined. Also indexing a slice includes a bounds check and is fallible.

However, the lint is named or_fun_call and not or_non_const. Perhaps that could be a separate lint? I think we need more input here.

@giraffate
Copy link
Contributor

@rustbot modify labels: +L-enhancement

@rustbot rustbot added the C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages label Nov 1, 2020
@bors bors closed this as completed in 2067a01 Nov 8, 2020
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 C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants