Skip to content

Commit d28b8e1

Browse files
committed
New lint for zip with array length instead of enumerate()
Fixes #11.
1 parent cb729e1 commit d28b8e1

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
66
[Jump to usage instructions](#usage)
77

88
##Lints
9-
There are 70 lints included in this crate:
9+
There are 71 lints included in this crate:
1010

1111
name | default | meaning
1212
-------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -53,6 +53,7 @@ name
5353
[precedence](https://github.com/Manishearth/rust-clippy/wiki#precedence) | warn | catches operations where precedence may be unclear. See the wiki for a list of cases caught
5454
[ptr_arg](https://github.com/Manishearth/rust-clippy/wiki#ptr_arg) | warn | fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` instead, respectively
5555
[range_step_by_zero](https://github.com/Manishearth/rust-clippy/wiki#range_step_by_zero) | warn | using Range::step_by(0), which produces an infinite iterator
56+
[range_zip_with_len](https://github.com/Manishearth/rust-clippy/wiki#range_zip_with_len) | warn | zipping iterator with a range when enumerate() would do
5657
[redundant_closure](https://github.com/Manishearth/rust-clippy/wiki#redundant_closure) | warn | using redundant closures, i.e. `|a| foo(a)` (which can be written as just `foo`)
5758
[redundant_pattern](https://github.com/Manishearth/rust-clippy/wiki#redundant_pattern) | warn | using `name @ _` in a pattern
5859
[result_unwrap_used](https://github.com/Manishearth/rust-clippy/wiki#result_unwrap_used) | allow | using `Result.unwrap()`, which might be better handled

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
166166
precedence::PRECEDENCE,
167167
ptr_arg::PTR_ARG,
168168
ranges::RANGE_STEP_BY_ZERO,
169+
ranges::RANGE_ZIP_WITH_LEN,
169170
returns::LET_AND_RETURN,
170171
returns::NEEDLESS_RETURN,
171172
types::BOX_VEC,

src/ranges.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,64 @@
11
use rustc::lint::*;
22
use rustc_front::hir::*;
33
use syntax::codemap::Spanned;
4-
use utils::{match_type, is_integer_literal};
4+
use utils::{is_integer_literal, match_type, snippet};
55

66
declare_lint! {
77
pub RANGE_STEP_BY_ZERO, Warn,
88
"using Range::step_by(0), which produces an infinite iterator"
99
}
10+
declare_lint! {
11+
pub RANGE_ZIP_WITH_LEN, Warn,
12+
"zipping iterator with a range when enumerate() would do"
13+
}
1014

1115
#[derive(Copy,Clone)]
1216
pub struct StepByZero;
1317

1418
impl LintPass for StepByZero {
1519
fn get_lints(&self) -> LintArray {
16-
lint_array!(RANGE_STEP_BY_ZERO)
20+
lint_array!(RANGE_STEP_BY_ZERO, RANGE_ZIP_WITH_LEN)
1721
}
1822
}
1923

2024
impl LateLintPass for StepByZero {
2125
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
2226
if let ExprMethodCall(Spanned { node: ref name, .. }, _,
2327
ref args) = expr.node {
24-
// Only warn on literal ranges.
28+
// Range with step_by(0).
2529
if name.as_str() == "step_by" && args.len() == 2 &&
2630
is_range(cx, &args[0]) && is_integer_literal(&args[1], 0) {
2731
cx.span_lint(RANGE_STEP_BY_ZERO, expr.span,
2832
"Range::step_by(0) produces an infinite iterator. \
2933
Consider using `std::iter::repeat()` instead")
3034
}
35+
36+
// x.iter().zip(0..x.len())
37+
else if name.as_str() == "zip" && args.len() == 2 {
38+
let iter = &args[0].node;
39+
let zip_arg = &args[1].node;
40+
if_let_chain! {
41+
[
42+
// .iter() call
43+
let &ExprMethodCall( Spanned { node: ref iter_name, .. }, _, ref iter_args ) = iter,
44+
iter_name.as_str() == "iter",
45+
// range expression in .zip() call: 0..x.len()
46+
let &ExprRange(Some(ref from), Some(ref to)) = zip_arg,
47+
is_integer_literal(from, 0),
48+
// .len() call
49+
let ExprMethodCall(Spanned { node: ref len_name, .. }, _, ref len_args) = to.node,
50+
len_name.as_str() == "len" && len_args.len() == 1,
51+
// .iter() and .len() called on same Path
52+
let ExprPath(_, Path { segments: ref iter_path, .. }) = iter_args[0].node,
53+
let ExprPath(_, Path { segments: ref len_path, .. }) = len_args[0].node,
54+
iter_path == len_path
55+
], {
56+
cx.span_lint(RANGE_ZIP_WITH_LEN, expr.span,
57+
&format!("It is more idiomatic to use {}.iter().enumerate()",
58+
snippet(cx, iter_args[0].span, "_")));
59+
}
60+
}
61+
}
3162
}
3263
}
3364
}

tests/compile-fail/range.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ impl NotARange {
77
fn step_by(&self, _: u32) {}
88
}
99

10-
#[deny(range_step_by_zero)]
10+
#[deny(range_step_by_zero, range_zip_with_len)]
1111
fn main() {
1212
(0..1).step_by(0); //~ERROR Range::step_by(0) produces an infinite iterator
1313
// No warning for non-zero step
@@ -21,4 +21,9 @@ fn main() {
2121
// No error, not a range.
2222
let y = NotARange;
2323
y.step_by(0);
24+
25+
let _v1 = vec![1,2,3];
26+
let _v2 = vec![4,5];
27+
let _x = _v1.iter().zip(0.._v1.len()); //~ERROR It is more idiomatic to use _v1.iter().enumerate()
28+
let _y = _v1.iter().zip(0.._v2.len()); // No error
2429
}

0 commit comments

Comments
 (0)