You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mmstick opened this issue
Jun 12, 2016
· 6 comments
Labels
A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-styleLint: Belongs in the style lint groupT-middleType: Probably requires verifiying types
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);
The text was updated successfully, but these errors were encountered:
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
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);
A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-styleLint: Belongs in the style lint groupT-middleType: Probably requires verifiying types
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:
However, it would be better to detect this behaviour and recommend the following:
Or even the following for more complicated mapping.
The text was updated successfully, but these errors were encountered: