Skip to content

Commit 14b8290

Browse files
dnblnblyxyas
andcommitted
Apply suggestions from code review
Co-authored-by: Alejandra González <[email protected]>
1 parent 0b90f72 commit 14b8290

File tree

5 files changed

+135
-31
lines changed

5 files changed

+135
-31
lines changed

clippy_lints/src/loops/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ declare_clippy_lint! {
580580

581581
declare_clippy_lint! {
582582
/// ### What it does
583-
/// Checks for `for (_, v) in a.iter().enumerate()`
583+
/// Checks for uses of the `enumerate` method where the index is unused (`_`)
584584
///
585585
/// ### Why is this bad?
586586
/// The index from `.enumerate()` is immediately dropped.
@@ -589,20 +589,20 @@ declare_clippy_lint! {
589589
/// ```rust
590590
/// let v = vec![1, 2, 3, 4];
591591
/// for (_, x) in v.iter().enumerate() {
592-
/// print!("{x}")
592+
/// println!("{x}");
593593
/// }
594594
/// ```
595595
/// Use instead:
596596
/// ```rust
597597
/// let v = vec![1, 2, 3, 4];
598598
/// for x in v.iter() {
599-
/// print!("{x}")
599+
/// println!("{x}");
600600
/// }
601601
/// ```
602-
#[clippy::version = "1.69.0"]
602+
#[clippy::version = "1.75.0"]
603603
pub UNUSED_ENUMERATE_INDEX,
604604
style,
605-
"using .enumerate() and immediately dropping the index"
605+
"using `.enumerate()` and immediately dropping the index"
606606
}
607607

608608
declare_clippy_lint! {

clippy_lints/src/loops/unused_enumerate_index.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,27 @@ use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
33
use clippy_utils::source::snippet;
44
use clippy_utils::sugg;
55
use clippy_utils::visitors::is_local_used;
6+
use rustc_hir::def::DefKind;
67
use rustc_hir::{Expr, ExprKind, Pat, PatKind};
78
use rustc_lint::LateContext;
89
use rustc_middle::ty;
910

1011
/// Checks for the `UNUSED_ENUMERATE_INDEX` lint.
1112
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx Expr<'_>, body: &'tcx Expr<'_>) {
12-
let pat_span = pat.span;
13-
14-
let PatKind::Tuple(pat, _) = pat.kind else {
13+
let PatKind::Tuple(tuple, _) = pat.kind else {
1514
return;
1615
};
1716

18-
if pat.len() != 2 {
19-
return;
20-
}
21-
22-
let arg_span = arg.span;
23-
24-
let ExprKind::MethodCall(method, self_arg, [], _) = arg.kind else {
17+
let ExprKind::MethodCall(_method, self_arg, [], _) = arg.kind else {
2518
return;
2619
};
2720

28-
if method.ident.as_str() != "enumerate" {
29-
return;
30-
}
31-
3221
let ty = cx.typeck_results().expr_ty(arg);
3322

34-
if !pat_is_wild(cx, &pat[0].kind, body) {
23+
if !pat_is_wild(cx, &tuple[0].kind, body) {
3524
return;
3625
}
3726

38-
let new_pat_span = pat[1].span;
39-
4027
let name = match *ty.kind() {
4128
ty::Adt(base, _substs) => cx.tcx.def_path_str(base.did()),
4229
_ => return,
@@ -46,19 +33,29 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx
4633
return;
4734
}
4835

36+
let Some((DefKind::AssocFn, call_id)) = cx.typeck_results().type_dependent_def(arg.hir_id) else {
37+
return;
38+
};
39+
40+
let call_name = cx.tcx.def_path_str(call_id);
41+
42+
if call_name != "std::iter::Iterator::enumerate" && call_name != "core::iter::Iterator::enumerate" {
43+
return;
44+
}
45+
4946
span_lint_and_then(
5047
cx,
5148
UNUSED_ENUMERATE_INDEX,
52-
arg_span,
49+
arg.span,
5350
"you seem to use `.enumerate()` and immediately discard the index",
5451
|diag| {
5552
let base_iter = sugg::Sugg::hir(cx, self_arg, "base iter");
5653
multispan_sugg(
5754
diag,
5855
"remove the `.enumerate()` call",
5956
vec![
60-
(pat_span, snippet(cx, new_pat_span, "value").into_owned()),
61-
(arg_span, base_iter.to_string()),
57+
(pat.span, snippet(cx, tuple[1].span, "..").into_owned()),
58+
(arg.span, base_iter.to_string()),
6259
],
6360
);
6461
},

tests/ui/unused_enumerate_index.fixed

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,58 @@
1-
// run-rustfix
21
#![allow(unused)]
32
#![warn(clippy::unused_enumerate_index)]
43

4+
use std::iter::Enumerate;
5+
56
fn main() {
67
let v = [1, 2, 3];
78
for x in v.iter() {
8-
print!("{x}");
9+
println!("{x}");
10+
}
11+
12+
struct Dummy1;
13+
impl Dummy1 {
14+
fn enumerate(self) -> Vec<usize> {
15+
vec![]
16+
}
17+
}
18+
let dummy = Dummy1;
19+
for x in dummy.enumerate() {
20+
println!("{x}");
21+
}
22+
23+
struct Dummy2;
24+
impl Dummy2 {
25+
fn enumerate(self) -> Enumerate<std::vec::IntoIter<usize>> {
26+
vec![1, 2].into_iter().enumerate()
27+
}
28+
}
29+
let dummy = Dummy2;
30+
for (_, x) in dummy.enumerate() {
31+
println!("{x}");
32+
}
33+
34+
let mut with_used_iterator = [1, 2, 3].into_iter().enumerate();
35+
with_used_iterator.next();
36+
for (_, x) in with_used_iterator {
37+
println!("{x}");
38+
}
39+
40+
struct Dummy3(std::vec::IntoIter<usize>);
41+
42+
impl Iterator for Dummy3 {
43+
type Item = usize;
44+
45+
fn next(&mut self) -> Option<Self::Item> {
46+
self.0.next()
47+
}
48+
49+
fn size_hint(&self) -> (usize, Option<usize>) {
50+
self.0.size_hint()
51+
}
52+
}
53+
54+
let dummy = Dummy3(vec![1, 2, 3].into_iter());
55+
for x in dummy {
56+
println!("{x}");
957
}
1058
}

tests/ui/unused_enumerate_index.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,58 @@
1-
// run-rustfix
21
#![allow(unused)]
32
#![warn(clippy::unused_enumerate_index)]
43

4+
use std::iter::Enumerate;
5+
56
fn main() {
67
let v = [1, 2, 3];
78
for (_, x) in v.iter().enumerate() {
8-
print!("{x}");
9+
println!("{x}");
10+
}
11+
12+
struct Dummy1;
13+
impl Dummy1 {
14+
fn enumerate(self) -> Vec<usize> {
15+
vec![]
16+
}
17+
}
18+
let dummy = Dummy1;
19+
for x in dummy.enumerate() {
20+
println!("{x}");
21+
}
22+
23+
struct Dummy2;
24+
impl Dummy2 {
25+
fn enumerate(self) -> Enumerate<std::vec::IntoIter<usize>> {
26+
vec![1, 2].into_iter().enumerate()
27+
}
28+
}
29+
let dummy = Dummy2;
30+
for (_, x) in dummy.enumerate() {
31+
println!("{x}");
32+
}
33+
34+
let mut with_used_iterator = [1, 2, 3].into_iter().enumerate();
35+
with_used_iterator.next();
36+
for (_, x) in with_used_iterator {
37+
println!("{x}");
38+
}
39+
40+
struct Dummy3(std::vec::IntoIter<usize>);
41+
42+
impl Iterator for Dummy3 {
43+
type Item = usize;
44+
45+
fn next(&mut self) -> Option<Self::Item> {
46+
self.0.next()
47+
}
48+
49+
fn size_hint(&self) -> (usize, Option<usize>) {
50+
self.0.size_hint()
51+
}
52+
}
53+
54+
let dummy = Dummy3(vec![1, 2, 3].into_iter());
55+
for (_, x) in dummy.enumerate() {
56+
println!("{x}");
957
}
1058
}

tests/ui/unused_enumerate_index.stderr

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you seem to use `.enumerate()` and immediately discard the index
2-
--> $DIR/unused_enumerate_index.rs:7:19
2+
--> $DIR/unused_enumerate_index.rs:8:19
33
|
44
LL | for (_, x) in v.iter().enumerate() {
55
| ^^^^^^^^^^^^^^^^^^^^
@@ -11,5 +11,16 @@ help: remove the `.enumerate()` call
1111
LL | for x in v.iter() {
1212
| ~ ~~~~~~~~
1313

14-
error: aborting due to previous error
14+
error: you seem to use `.enumerate()` and immediately discard the index
15+
--> $DIR/unused_enumerate_index.rs:55:19
16+
|
17+
LL | for (_, x) in dummy.enumerate() {
18+
| ^^^^^^^^^^^^^^^^^
19+
|
20+
help: remove the `.enumerate()` call
21+
|
22+
LL | for x in dummy {
23+
| ~ ~~~~~
24+
25+
error: aborting due to 2 previous errors
1526

0 commit comments

Comments
 (0)