-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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: iter_out_of_bounds
#11396
Merged
Merged
new lint: iter_out_of_bounds
#11396
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use clippy_utils::diagnostics::span_lint_and_note; | ||
use clippy_utils::higher::VecArgs; | ||
use clippy_utils::{expr_or_init, is_trait_method, match_def_path, paths}; | ||
use rustc_ast::LitKind; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self}; | ||
use rustc_span::sym; | ||
|
||
use super::ITER_OUT_OF_BOUNDS; | ||
|
||
fn expr_as_u128(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<u128> { | ||
if let ExprKind::Lit(lit) = expr_or_init(cx, e).kind | ||
&& let LitKind::Int(n, _) = lit.node | ||
{ | ||
Some(n) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Attempts to extract the length out of an iterator expression. | ||
fn get_iterator_length<'tcx>(cx: &LateContext<'tcx>, iter: &'tcx Expr<'tcx>) -> Option<u128> { | ||
let ty::Adt(adt, substs) = cx.typeck_results().expr_ty(iter).kind() else { | ||
return None; | ||
}; | ||
let did = adt.did(); | ||
|
||
if match_def_path(cx, did, &paths::ARRAY_INTO_ITER) { | ||
// For array::IntoIter<T, const N: usize>, the length is the second generic | ||
// parameter. | ||
substs | ||
.const_at(1) | ||
.try_eval_target_usize(cx.tcx, cx.param_env) | ||
.map(u128::from) | ||
} else if match_def_path(cx, did, &paths::SLICE_ITER) | ||
&& let ExprKind::MethodCall(_, recv, ..) = iter.kind | ||
{ | ||
if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() { | ||
// For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..) | ||
len.try_eval_target_usize(cx.tcx, cx.param_env).map(u128::from) | ||
} else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) { | ||
match args { | ||
VecArgs::Vec(vec) => vec.len().try_into().ok(), | ||
VecArgs::Repeat(_, len) => expr_as_u128(cx, len), | ||
} | ||
} else { | ||
None | ||
} | ||
} else if match_def_path(cx, did, &paths::ITER_EMPTY) { | ||
Some(0) | ||
} else if match_def_path(cx, did, &paths::ITER_ONCE) { | ||
Some(1) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
recv: &'tcx Expr<'tcx>, | ||
arg: &'tcx Expr<'tcx>, | ||
message: &'static str, | ||
note: &'static str, | ||
) { | ||
if is_trait_method(cx, expr, sym::Iterator) | ||
&& let Some(len) = get_iterator_length(cx, recv) | ||
&& let Some(skipped) = expr_as_u128(cx, arg) | ||
&& skipped > len | ||
{ | ||
span_lint_and_note(cx, ITER_OUT_OF_BOUNDS, expr.span, message, None, note); | ||
} | ||
} | ||
|
||
pub(super) fn check_skip<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
recv: &'tcx Expr<'tcx>, | ||
arg: &'tcx Expr<'tcx>, | ||
) { | ||
check( | ||
cx, | ||
expr, | ||
recv, | ||
arg, | ||
"this `.skip()` call skips more items than the iterator will produce", | ||
"this operation is useless and will create an empty iterator", | ||
); | ||
} | ||
|
||
pub(super) fn check_take<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
recv: &'tcx Expr<'tcx>, | ||
arg: &'tcx Expr<'tcx>, | ||
) { | ||
check( | ||
cx, | ||
expr, | ||
recv, | ||
arg, | ||
"this `.take()` call takes more items than the iterator will produce", | ||
"this operation is useless and the returned iterator will simply yield the same items", | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//@no-rustfix | ||
|
||
#![deny(clippy::iter_out_of_bounds)] | ||
#![allow(clippy::useless_vec)] | ||
|
||
fn opaque_empty_iter() -> impl Iterator<Item = ()> { | ||
std::iter::empty() | ||
} | ||
|
||
fn main() { | ||
for _ in [1, 2, 3].iter().skip(4) { | ||
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce | ||
unreachable!(); | ||
} | ||
for (i, _) in [1, 2, 3].iter().take(4).enumerate() { | ||
//~^ ERROR: this `.take()` call takes more items than the iterator will produce | ||
assert!(i <= 2); | ||
} | ||
|
||
#[allow(clippy::needless_borrow)] | ||
for _ in (&&&&&&[1, 2, 3]).iter().take(4) {} | ||
//~^ ERROR: this `.take()` call takes more items than the iterator will produce | ||
|
||
for _ in [1, 2, 3].iter().skip(4) {} | ||
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce | ||
|
||
for _ in [1; 3].iter().skip(4) {} | ||
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce | ||
|
||
// Should not lint | ||
for _ in opaque_empty_iter().skip(1) {} | ||
|
||
for _ in vec![1, 2, 3].iter().skip(4) {} | ||
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce | ||
|
||
for _ in vec![1; 3].iter().skip(4) {} | ||
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce | ||
|
||
let x = [1, 2, 3]; | ||
for _ in x.iter().skip(4) {} | ||
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce | ||
|
||
let n = 4; | ||
for _ in x.iter().skip(n) {} | ||
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce | ||
|
||
let empty = std::iter::empty::<i8>; | ||
|
||
for _ in empty().skip(1) {} | ||
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce | ||
|
||
for _ in empty().take(1) {} | ||
//~^ ERROR: this `.take()` call takes more items than the iterator will produce | ||
|
||
for _ in std::iter::once(1).skip(2) {} | ||
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce | ||
|
||
for _ in std::iter::once(1).take(2) {} | ||
//~^ ERROR: this `.take()` call takes more items than the iterator will produce | ||
|
||
for x in [].iter().take(1) { | ||
//~^ ERROR: this `.take()` call takes more items than the iterator will produce | ||
let _: &i32 = x; | ||
} | ||
|
||
// ok, not out of bounds | ||
for _ in [1].iter().take(1) {} | ||
for _ in [1, 2, 3].iter().take(2) {} | ||
for _ in [1, 2, 3].iter().skip(2) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
error: this `.skip()` call skips more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:11:14 | ||
| | ||
LL | for _ in [1, 2, 3].iter().skip(4) { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and will create an empty iterator | ||
note: the lint level is defined here | ||
--> $DIR/iter_out_of_bounds.rs:3:9 | ||
| | ||
LL | #![deny(clippy::iter_out_of_bounds)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: this `.take()` call takes more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:15:19 | ||
| | ||
LL | for (i, _) in [1, 2, 3].iter().take(4).enumerate() { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and the returned iterator will simply yield the same items | ||
|
||
error: this `.take()` call takes more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:21:14 | ||
| | ||
LL | for _ in (&&&&&&[1, 2, 3]).iter().take(4) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and the returned iterator will simply yield the same items | ||
|
||
error: this `.skip()` call skips more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:24:14 | ||
| | ||
LL | for _ in [1, 2, 3].iter().skip(4) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and will create an empty iterator | ||
|
||
error: this `.skip()` call skips more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:27:14 | ||
| | ||
LL | for _ in [1; 3].iter().skip(4) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and will create an empty iterator | ||
|
||
error: this `.skip()` call skips more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:33:14 | ||
| | ||
LL | for _ in vec![1, 2, 3].iter().skip(4) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and will create an empty iterator | ||
|
||
error: this `.skip()` call skips more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:36:14 | ||
| | ||
LL | for _ in vec![1; 3].iter().skip(4) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and will create an empty iterator | ||
|
||
error: this `.skip()` call skips more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:40:14 | ||
| | ||
LL | for _ in x.iter().skip(4) {} | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and will create an empty iterator | ||
|
||
error: this `.skip()` call skips more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:44:14 | ||
| | ||
LL | for _ in x.iter().skip(n) {} | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and will create an empty iterator | ||
|
||
error: this `.skip()` call skips more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:49:14 | ||
| | ||
LL | for _ in empty().skip(1) {} | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and will create an empty iterator | ||
|
||
error: this `.take()` call takes more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:52:14 | ||
| | ||
LL | for _ in empty().take(1) {} | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and the returned iterator will simply yield the same items | ||
|
||
error: this `.skip()` call skips more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:55:14 | ||
| | ||
LL | for _ in std::iter::once(1).skip(2) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and will create an empty iterator | ||
|
||
error: this `.take()` call takes more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:58:14 | ||
| | ||
LL | for _ in std::iter::once(1).take(2) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and the returned iterator will simply yield the same items | ||
|
||
error: this `.take()` call takes more items than the iterator will produce | ||
--> $DIR/iter_out_of_bounds.rs:61:14 | ||
| | ||
LL | for x in [].iter().take(1) { | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this operation is useless and the returned iterator will simply yield the same items | ||
|
||
error: aborting due to 14 previous errors | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This really should have a
MaybeIncorrect
suggestion added to it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the suggestion should be or if it makes sense to have one. At least for something like
[1,2,3].iter().take(4)
it could probably suggest removing the.take()
call as that'll still yield the same items with or without it, but I don't know what the user could have meant with[1,2,3].iter().skip(4)
. Should it suggest and empty iterator? 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was only considering
take
here. Given the suggestion isn't going to have a lot of value (decent chance of being the wrong fix), I guess it's fine to not have one.