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

new lint: warn if we match on index item and use wildcard pattern #5500

Closed
matthiaskrgr opened this issue Apr 20, 2020 · 1 comment · Fixed by #5522
Closed

new lint: warn if we match on index item and use wildcard pattern #5500

matthiaskrgr opened this issue Apr 20, 2020 · 1 comment · Fixed by #5522
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-style Lint: Belongs in the style lint group T-AST Type: Requires working with the AST

Comments

@matthiaskrgr
Copy link
Member

Inspired by #5499

pub fn main() {
    let x = vec![1,2,3];
    match_vec_idx(4 /*boom*/, x);
}

fn match_vec_idx(idx: usize, vec: Vec<u32>) -> bool {
    match vec[idx] {
        1 => return true,
        2 => return false,
        _ => {}
    }
return false
}

In this code we can see that we match on an item in a certain vector position, we only care about a limited number of cases and ignore the rest.

This code will panic if the idx is out of bounds, as it is the case here.
If we could lint the

match x[y] => {
   something => something_else,
   _  => {}
}

pattern where nothing happens anyway if we don't find any of the matched patterns in the vector position, we could suggest using .get() and avoid the panic:

pub fn main() {
    let x = vec![1,2,3];
    match_vec_idx(4/*no longer boom*/, x);
}

fn match_vec_idx(idx: usize, vec: Vec<u32>) -> bool {
    match vec.get(idx) { // use .get()
        Some(1) => return true, // wrapped in Some()
        Some(2) => return false, // wrapped in Some()
        _ => {}
    }
return false
}
@matthiaskrgr matthiaskrgr added the A-lint Area: New lints label Apr 20, 2020
@flip1995 flip1995 added L-style Lint: Belongs in the style lint group good-first-issue These issues are a good way to get started with Clippy T-AST Type: Requires working with the AST labels Apr 21, 2020
@CrazyRoka
Copy link
Contributor

I will try to do it as my first attempt to contribute to a project

bors added a commit that referenced this issue Apr 27, 2020
New  lint `match_vec_item`

Added new lint to warn a match on index item which can panic. It's always better to use `get(..)` instead.
Closes #5500
changelog: New lint `match_on_vec_items`
@bors bors closed this as completed in d13ffbe Apr 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-style Lint: Belongs in the style lint group T-AST Type: Requires working with the AST
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants