Rust-clippy: match_on_vec_items: false positive on `match vec[..]`

Created on 1 May 2020  Â·  3Comments  Â·  Source: rust-lang/rust-clippy

Sample program:

fn main() {
    let x = vec![1,2,3];
    match x[..] {
        [1, 2, 3] => println!("123"),
        _ => println!("not 123"),
    }
}

Clippy output:

error: indexing into a vector may panic
 --> src/main.rs:3:11
  |
3 |     match x[..] {
  |           ^^^^^ help: try this: `x.get(..)`
  |
  = note: `#[deny(clippy::match_on_vec_items)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_on_vec_items

error: aborting due to previous error

However, this use of indexing is infallible.

(note that I'm silencing this lint globally anyway because I don't consider it useful)

L-bug L-enhancement good-first-issue

All 3 comments

cc @CrazyRoka This lint shouldn't trigger on wild [..] patterns.

@goffrie Why do you think this lint isn't useful? x.get(/*...*/) can never panic, while x[/*...*/] can. IMO using get is a clear improvement, since you get a compile time guarantee.

When I updated clippy a number of callsites lit up with the lint where it
was impossible for the indexing to panic (usually because the length was
just checked in some way). Using get would have made the the code less
clear.

Though I feel that this is more of a "teaching" lint for people writing new
code.

On Fri., May 1, 2020, 05:24 Philipp Krones, notifications@github.com
wrote:

cc @CrazyRoka https://github.com/CrazyRoka This lint shouldn't trigger
on wild [..] patterns.

@goffrie https://github.com/goffrie Why do you think this lint is not
useful? x.get(/.../) can never panic, while x[/.../] can. IMO using
get is a clear improvement.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/rust-lang/rust-clippy/issues/5551#issuecomment-622366954,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAJO2TXUKOP6UEDS6XO6MR3RPK5OXANCNFSM4MW2ODTQ
.

Alright, that is a fair point. I'll move the lint to pedantic (allow-by-default)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matthiaskrgr picture matthiaskrgr  Â·  3Comments

icefoxen picture icefoxen  Â·  3Comments

jyn514 picture jyn514  Â·  3Comments

matthiaskrgr picture matthiaskrgr  Â·  3Comments

taiki-e picture taiki-e  Â·  3Comments