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

Lint for Unnecessary Manual Bounds Checking #1008

Open
mmstick opened this issue Jun 12, 2016 · 6 comments
Open

Lint for Unnecessary Manual Bounds Checking #1008

mmstick opened this issue Jun 12, 2016 · 6 comments
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group T-middle Type: Probably requires verifiying types

Comments

@mmstick
Copy link

mmstick commented Jun 12, 2016

I ran across a section of translated C code that was written as such to manually avoid accessing a value out of bounds and returning a default value if it is out of bounds:

let hash_value = if pos + ZOPFLI_MIN_MATCH <= array.len() {
    array[pos + ZOPFLI_MIN_MATCH - 1]
} else {
    0
};

However, it would be better to detect this behaviour and recommend the following:

let hash_value = array.iter().nth(pos + ZOPFLI_MIN_MATCH - 1).cloned().unwrap_or(0);

Or even the following for more complicated mapping.

let hash_value = array.iter().nth(pos + ZOPFLI_MIN_MATCH - 1).map_or(0, |value| *value);
@mcarton mcarton added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types A-lint Area: New lints L-style Lint: Belongs in the style lint group labels Jun 12, 2016
@mcarton
Copy link
Member

mcarton commented Jun 12, 2016

Decide between cloned and map_or might be hard (cc #443, #498), but otherwise 👍

@mmstick
Copy link
Author

mmstick commented Jun 12, 2016

I would think that cloned would be ideal for simply copying the value in the case of |value| *value whereas map_or would be ideal for more complicated actions where you want to do more than just copying the value, like so:

let result_is_even = array.iter().nth(index).map_or(false, |value| *value % 2 == 0);

@mcarton
Copy link
Member

mcarton commented Jun 12, 2016

Yes but it's hard to check whether cloned can be called because it has a weird bound.

@mcarton
Copy link
Member

mcarton commented Jun 12, 2016

Oh actually this is Option::cloned which only needs T: Clone. It shouldn't be a problem to check then.

@Manishearth
Copy link
Member

Its better to use .get() over iterators for arrays, no? That iterator might get optimized, but not in debug, and get is cleaner anyway.

@mcarton
Copy link
Member

mcarton commented Jun 12, 2016

I guess we also need a lint for s/iter().nth/get/ then 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

3 participants