-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
A-documentationArea: Adding or improving documentationArea: Adding or improving documentationL-suggestionLint: Improving, adding or fixing lint suggestionsLint: Improving, adding or fixing lint suggestionsgood first issueThese issues are a good way to get started with ClippyThese issues are a good way to get started with Clippy
Description
needless_range_loop suggests changing:
pub fn foo_a(out: &mut Vec<usize>, inx: &[usize], base: usize) {
for i in 1 .. base {
out[i] = *inx.get(i).unwrap_or(&0);
}
}
to
pub fn foo_b(out: &mut Vec<usize>, inx: &[usize], base: usize) {
for (i, item) in out.iter_mut().enumerate().take(base).skip(1) {
*item = *inx.get(i).unwrap_or(&0);
}
}
However, these are only equivalent is out has at least base elements. If not, then this changes behaviour (the first version panics with out of bounds, the second silently truncates the loop).
See this example at https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6f00aec168a98758df574f0734a2233e (edit: this link was previously broken, thanks)
(I noticed this will discussing needless_range_loop in #6075 )
peter-lyons-kehl, epidemian and zachs18
Metadata
Metadata
Assignees
Labels
A-documentationArea: Adding or improving documentationArea: Adding or improving documentationL-suggestionLint: Improving, adding or fixing lint suggestionsLint: Improving, adding or fixing lint suggestionsgood first issueThese issues are a good way to get started with ClippyThese issues are a good way to get started with Clippy