Skip to content

match_on_vec_items: false positive on match vec[..] #5551

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
goffrie opened this issue May 1, 2020 · 3 comments · Fixed by #5560
Closed

match_on_vec_items: false positive on match vec[..] #5551

goffrie opened this issue May 1, 2020 · 3 comments · Fixed by #5560
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 good first issue These issues are a good way to get started with Clippy

Comments

@goffrie
Copy link

goffrie commented May 1, 2020

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)

@flip1995 flip1995 added good first issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels May 1, 2020
@flip1995
Copy link
Member

flip1995 commented May 1, 2020

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.

@flip1995 flip1995 added the C-bug Category: Clippy is not doing the correct thing label May 1, 2020
@goffrie
Copy link
Author

goffrie commented May 1, 2020 via email

@flip1995
Copy link
Member

flip1995 commented May 1, 2020

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

bors added a commit that referenced this issue May 2, 2020
Move match_on_vec_items to pedantic

Addresses #5551 (comment)

Fixes #5553

changelog: Move [`match_on_vec_items`] to pedantic

r? @phansch
bors added a commit that referenced this issue May 2, 2020
Move match_on_vec_items to pedantic

Addresses #5551 (comment)

Fixes #5553

changelog: Move [`match_on_vec_items`] to pedantic

r? @phansch
@bors bors closed this as completed in 76ddac5 May 2, 2020
@bors bors closed this as completed in #5560 May 2, 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 good first issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants