Skip to content

Commit d42cc8c

Browse files
committed
Add lint for iter.nth(0)
- Encourage iter.next() rather than iter.nth(0), which is less readable
1 parent 72a2e0e commit d42cc8c

File tree

8 files changed

+151
-6
lines changed

8 files changed

+151
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ Released 2018-09-13
11411141
[`iter_cloned_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_cloned_collect
11421142
[`iter_next_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_loop
11431143
[`iter_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth
1144+
[`iter_nth_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth_zero
11441145
[`iter_skip_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_skip_next
11451146
[`iterator_step_by_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#iterator_step_by_zero
11461147
[`just_underscores_and_digits`]: https://rust-lang.github.io/rust-clippy/master/index.html#just_underscores_and_digits

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 342 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 343 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
617617
&methods::ITERATOR_STEP_BY_ZERO,
618618
&methods::ITER_CLONED_COLLECT,
619619
&methods::ITER_NTH,
620+
&methods::ITER_NTH_ZERO,
620621
&methods::ITER_SKIP_NEXT,
621622
&methods::MANUAL_SATURATING_ARITHMETIC,
622623
&methods::MAP_FLATTEN,
@@ -1195,6 +1196,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
11951196
LintId::of(&methods::ITERATOR_STEP_BY_ZERO),
11961197
LintId::of(&methods::ITER_CLONED_COLLECT),
11971198
LintId::of(&methods::ITER_NTH),
1199+
LintId::of(&methods::ITER_NTH_ZERO),
11981200
LintId::of(&methods::ITER_SKIP_NEXT),
11991201
LintId::of(&methods::MANUAL_SATURATING_ARITHMETIC),
12001202
LintId::of(&methods::NEW_RET_NO_SELF),
@@ -1372,6 +1374,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
13721374
LintId::of(&methods::CHARS_LAST_CMP),
13731375
LintId::of(&methods::INTO_ITER_ON_REF),
13741376
LintId::of(&methods::ITER_CLONED_COLLECT),
1377+
LintId::of(&methods::ITER_NTH_ZERO),
13751378
LintId::of(&methods::ITER_SKIP_NEXT),
13761379
LintId::of(&methods::MANUAL_SATURATING_ARITHMETIC),
13771380
LintId::of(&methods::NEW_RET_NO_SELF),

clippy_lints/src/methods/mod.rs

+54-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use syntax::ast;
2020
use syntax::source_map::Span;
2121
use syntax::symbol::{sym, Symbol, SymbolStr};
2222

23+
use crate::consts::{constant, Constant};
2324
use crate::utils::usage::mutated_variables;
2425
use crate::utils::{
2526
get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy,
@@ -756,6 +757,32 @@ declare_clippy_lint! {
756757
"using `Iterator::step_by(0)`, which will panic at runtime"
757758
}
758759

760+
declare_clippy_lint! {
761+
/// **What it does:** Checks for the use of `iter.nth(0)`.
762+
///
763+
/// **Why is this bad?** `iter.nth(0)` is unnecessary, and `iter.next()`
764+
/// is more readable.
765+
///
766+
/// **Known problems:** None.
767+
///
768+
/// **Example:**
769+
///
770+
/// ```rust,ignore
771+
/// // Bad
772+
/// let mut s = HashSet::new();
773+
/// s.insert(1);
774+
/// let x = s.iter().nth(0);
775+
///
776+
/// // Good
777+
/// let mut s = HashSet::new();
778+
/// s.insert(1);
779+
/// let x = s.iter().next();
780+
/// ```
781+
pub ITER_NTH_ZERO,
782+
style,
783+
"replace `iter.nth(0)` with `iter.next()`"
784+
}
785+
759786
declare_clippy_lint! {
760787
/// **What it does:** Checks for use of `.iter().nth()` (and the related
761788
/// `.iter_mut().nth()`) on standard library types with O(1) element access.
@@ -1136,6 +1163,7 @@ declare_lint_pass!(Methods => [
11361163
MAP_FLATTEN,
11371164
ITERATOR_STEP_BY_ZERO,
11381165
ITER_NTH,
1166+
ITER_NTH_ZERO,
11391167
ITER_SKIP_NEXT,
11401168
GET_UNWRAP,
11411169
STRING_EXTEND_CHARS,
@@ -1191,8 +1219,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
11911219
["as_ptr", "unwrap"] | ["as_ptr", "expect"] => {
11921220
lint_cstring_as_ptr(cx, expr, &arg_lists[1][0], &arg_lists[0][0])
11931221
},
1194-
["nth", "iter"] => lint_iter_nth(cx, expr, arg_lists[1], false),
1195-
["nth", "iter_mut"] => lint_iter_nth(cx, expr, arg_lists[1], true),
1222+
["nth", "iter"] => lint_iter_nth(cx, expr, &arg_lists, false),
1223+
["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true),
1224+
["nth", ..] => lint_iter_nth_zero(cx, expr, arg_lists[0]),
11961225
["step_by", ..] => lint_step_by(cx, expr, arg_lists[0]),
11971226
["next", "skip"] => lint_iter_skip_next(cx, expr),
11981227
["collect", "cloned"] => lint_iter_cloned_collect(cx, expr, arg_lists[1]),
@@ -1983,7 +2012,6 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, fold_ar
19832012

19842013
fn lint_step_by<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr<'_>, args: &'tcx [hir::Expr<'_>]) {
19852014
if match_trait_method(cx, expr, &paths::ITERATOR) {
1986-
use crate::consts::{constant, Constant};
19872015
if let Some((Constant::Int(0), _)) = constant(cx, cx.tables, &args[1]) {
19882016
span_lint(
19892017
cx,
@@ -1998,9 +2026,10 @@ fn lint_step_by<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr<'_>, args
19982026
fn lint_iter_nth<'a, 'tcx>(
19992027
cx: &LateContext<'a, 'tcx>,
20002028
expr: &hir::Expr<'_>,
2001-
iter_args: &'tcx [hir::Expr<'_>],
2029+
nth_and_iter_args: &[&'tcx [hir::Expr<'tcx>]],
20022030
is_mut: bool,
20032031
) {
2032+
let iter_args = nth_and_iter_args[1];
20042033
let mut_str = if is_mut { "_mut" } else { "" };
20052034
let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])).is_some() {
20062035
"slice"
@@ -2009,6 +2038,8 @@ fn lint_iter_nth<'a, 'tcx>(
20092038
} else if match_type(cx, cx.tables.expr_ty(&iter_args[0]), &paths::VEC_DEQUE) {
20102039
"VecDeque"
20112040
} else {
2041+
let nth_args = nth_and_iter_args[0];
2042+
lint_iter_nth_zero(cx, expr, &nth_args);
20122043
return; // caller is not a type that we want to lint
20132044
};
20142045

@@ -2023,6 +2054,25 @@ fn lint_iter_nth<'a, 'tcx>(
20232054
);
20242055
}
20252056

2057+
fn lint_iter_nth_zero<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr<'_>, nth_args: &'tcx [hir::Expr<'_>]) {
2058+
if_chain! {
2059+
if match_trait_method(cx, expr, &paths::ITERATOR);
2060+
if let Some((Constant::Int(0), _)) = constant(cx, cx.tables, &nth_args[1]);
2061+
then {
2062+
let mut applicability = Applicability::MachineApplicable;
2063+
span_lint_and_sugg(
2064+
cx,
2065+
ITER_NTH_ZERO,
2066+
expr.span,
2067+
"called `.nth(0)` on a `std::iter::Iterator`",
2068+
"try calling",
2069+
format!("{}.next()", snippet_with_applicability(cx, nth_args[0].span, "..", &mut applicability)),
2070+
applicability,
2071+
);
2072+
}
2073+
}
2074+
}
2075+
20262076
fn lint_get_unwrap<'a, 'tcx>(
20272077
cx: &LateContext<'a, 'tcx>,
20282078
expr: &hir::Expr<'_>,

src/lintlist/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 342] = [
9+
pub const ALL_LINTS: [Lint; 343] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -875,6 +875,13 @@ pub const ALL_LINTS: [Lint; 342] = [
875875
deprecation: None,
876876
module: "methods",
877877
},
878+
Lint {
879+
name: "iter_nth_zero",
880+
group: "style",
881+
desc: "replace `iter.nth(0)` with `iter.next()`",
882+
deprecation: None,
883+
module: "methods",
884+
},
878885
Lint {
879886
name: "iter_skip_next",
880887
group: "style",

tests/ui/iter_nth_zero.fixed

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::iter_nth_zero)]
4+
use std::collections::HashSet;
5+
6+
struct Foo {}
7+
8+
impl Foo {
9+
fn nth(&self, index: usize) -> usize {
10+
index + 1
11+
}
12+
}
13+
14+
fn main() {
15+
let f = Foo {};
16+
f.nth(0); // lint does not apply here
17+
18+
let mut s = HashSet::new();
19+
s.insert(1);
20+
let _x = s.iter().next();
21+
22+
let mut s2 = HashSet::new();
23+
s2.insert(2);
24+
let mut iter = s2.iter();
25+
let _y = iter.next();
26+
27+
let mut s3 = HashSet::new();
28+
s3.insert(3);
29+
let mut iter2 = s3.iter();
30+
let _unwrapped = iter2.next().unwrap();
31+
}

tests/ui/iter_nth_zero.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::iter_nth_zero)]
4+
use std::collections::HashSet;
5+
6+
struct Foo {}
7+
8+
impl Foo {
9+
fn nth(&self, index: usize) -> usize {
10+
index + 1
11+
}
12+
}
13+
14+
fn main() {
15+
let f = Foo {};
16+
f.nth(0); // lint does not apply here
17+
18+
let mut s = HashSet::new();
19+
s.insert(1);
20+
let _x = s.iter().nth(0);
21+
22+
let mut s2 = HashSet::new();
23+
s2.insert(2);
24+
let mut iter = s2.iter();
25+
let _y = iter.nth(0);
26+
27+
let mut s3 = HashSet::new();
28+
s3.insert(3);
29+
let mut iter2 = s3.iter();
30+
let _unwrapped = iter2.nth(0).unwrap();
31+
}

tests/ui/iter_nth_zero.stderr

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: called `.nth(0)` on a `std::iter::Iterator`
2+
--> $DIR/iter_nth_zero.rs:20:14
3+
|
4+
LL | let _x = s.iter().nth(0);
5+
| ^^^^^^^^^^^^^^^ help: try calling: `s.iter().next()`
6+
|
7+
= note: `-D clippy::iter-nth-zero` implied by `-D warnings`
8+
9+
error: called `.nth(0)` on a `std::iter::Iterator`
10+
--> $DIR/iter_nth_zero.rs:25:14
11+
|
12+
LL | let _y = iter.nth(0);
13+
| ^^^^^^^^^^^ help: try calling: `iter.next()`
14+
15+
error: called `.nth(0)` on a `std::iter::Iterator`
16+
--> $DIR/iter_nth_zero.rs:30:22
17+
|
18+
LL | let _unwrapped = iter2.nth(0).unwrap();
19+
| ^^^^^^^^^^^^ help: try calling: `iter2.next()`
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)