Skip to content

Lint when .take(x) or .skip(x) is out of bounds for an array #11345

@Centri3

Description

@Centri3

What it does

.take() and .skip() (gracefully) don't panic when doing something like [1, 2, 3].iter().skip(4). We should lint this and say that it makes no sense since it'll either return the full iterator or an empty iterator.

Advantage

  • The code is useless in both scenarios

Drawbacks

No response

Example

for _ in [1, 2, 3].iter().skip(4) {
    unreachable!();
}
for (i, _) in [1, 2, 3].iter().take(4).enumerate() {
    assert!(i <= 2);
}

Could be written as:

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions