-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add unused_enumerate_index
lint
#10404
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
use super::UNUSED_ENUMERATE_INDEX; | ||
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then}; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::{pat_is_wild, sugg}; | ||
use rustc_hir::def::DefKind; | ||
use rustc_hir::{Expr, ExprKind, Pat, PatKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
|
||
/// Checks for the `UNUSED_ENUMERATE_INDEX` lint. | ||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx Expr<'_>, body: &'tcx Expr<'_>) { | ||
let PatKind::Tuple(tuple, _) = pat.kind else { | ||
return; | ||
}; | ||
|
||
let ExprKind::MethodCall(_method, self_arg, [], _) = arg.kind else { | ||
return; | ||
}; | ||
|
||
let ty = cx.typeck_results().expr_ty(arg); | ||
|
||
if !pat_is_wild(cx, &tuple[0].kind, body) { | ||
return; | ||
} | ||
|
||
let name = match *ty.kind() { | ||
ty::Adt(base, _substs) => cx.tcx.def_path_str(base.did()), | ||
_ => return, | ||
}; | ||
|
||
if name != "std::iter::Enumerate" && name != "core::iter::Enumerate" { | ||
return; | ||
} | ||
|
||
let Some((DefKind::AssocFn, call_id)) = cx.typeck_results().type_dependent_def(arg.hir_id) else { | ||
return; | ||
}; | ||
|
||
let call_name = cx.tcx.def_path_str(call_id); | ||
|
||
if call_name != "std::iter::Iterator::enumerate" && call_name != "core::iter::Iterator::enumerate" { | ||
dnbln marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
|
||
span_lint_and_then( | ||
cx, | ||
UNUSED_ENUMERATE_INDEX, | ||
arg.span, | ||
"you seem to use `.enumerate()` and immediately discard the index", | ||
|diag| { | ||
let base_iter = sugg::Sugg::hir(cx, self_arg, "base iter"); | ||
multispan_sugg( | ||
diag, | ||
"remove the `.enumerate()` call", | ||
vec![ | ||
(pat.span, snippet(cx, tuple[1].span, "..").into_owned()), | ||
(arg.span, base_iter.to_string()), | ||
], | ||
); | ||
}, | ||
); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::unused_enumerate_index)] | ||
|
||
use std::iter::Enumerate; | ||
|
||
fn main() { | ||
let v = [1, 2, 3]; | ||
for x in v.iter() { | ||
println!("{x}"); | ||
} | ||
|
||
struct Dummy1; | ||
impl Dummy1 { | ||
fn enumerate(self) -> Vec<usize> { | ||
vec![] | ||
} | ||
} | ||
let dummy = Dummy1; | ||
for x in dummy.enumerate() { | ||
println!("{x}"); | ||
} | ||
|
||
struct Dummy2; | ||
impl Dummy2 { | ||
fn enumerate(self) -> Enumerate<std::vec::IntoIter<usize>> { | ||
vec![1, 2].into_iter().enumerate() | ||
} | ||
} | ||
let dummy = Dummy2; | ||
for (_, x) in dummy.enumerate() { | ||
println!("{x}"); | ||
} | ||
|
||
let mut with_used_iterator = [1, 2, 3].into_iter().enumerate(); | ||
with_used_iterator.next(); | ||
for (_, x) in with_used_iterator { | ||
println!("{x}"); | ||
} | ||
|
||
struct Dummy3(std::vec::IntoIter<usize>); | ||
|
||
impl Iterator for Dummy3 { | ||
type Item = usize; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.0.next() | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
} | ||
|
||
let dummy = Dummy3(vec![1, 2, 3].into_iter()); | ||
for x in dummy { | ||
println!("{x}"); | ||
} | ||
} |
This file contains hidden or 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,58 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::unused_enumerate_index)] | ||
|
||
dnbln marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use std::iter::Enumerate; | ||
|
||
fn main() { | ||
let v = [1, 2, 3]; | ||
for (_, x) in v.iter().enumerate() { | ||
println!("{x}"); | ||
} | ||
|
||
struct Dummy1; | ||
impl Dummy1 { | ||
fn enumerate(self) -> Vec<usize> { | ||
vec![] | ||
} | ||
} | ||
let dummy = Dummy1; | ||
for x in dummy.enumerate() { | ||
println!("{x}"); | ||
} | ||
|
||
struct Dummy2; | ||
impl Dummy2 { | ||
fn enumerate(self) -> Enumerate<std::vec::IntoIter<usize>> { | ||
vec![1, 2].into_iter().enumerate() | ||
} | ||
} | ||
let dummy = Dummy2; | ||
for (_, x) in dummy.enumerate() { | ||
println!("{x}"); | ||
} | ||
|
||
let mut with_used_iterator = [1, 2, 3].into_iter().enumerate(); | ||
with_used_iterator.next(); | ||
for (_, x) in with_used_iterator { | ||
println!("{x}"); | ||
} | ||
|
||
struct Dummy3(std::vec::IntoIter<usize>); | ||
|
||
impl Iterator for Dummy3 { | ||
type Item = usize; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.0.next() | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
} | ||
|
||
let dummy = Dummy3(vec![1, 2, 3].into_iter()); | ||
for (_, x) in dummy.enumerate() { | ||
println!("{x}"); | ||
} | ||
dnbln marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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,26 @@ | ||
error: you seem to use `.enumerate()` and immediately discard the index | ||
--> $DIR/unused_enumerate_index.rs:8:19 | ||
| | ||
LL | for (_, x) in v.iter().enumerate() { | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::unused-enumerate-index` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unused_enumerate_index)]` | ||
help: remove the `.enumerate()` call | ||
| | ||
LL | for x in v.iter() { | ||
| ~ ~~~~~~~~ | ||
|
||
error: you seem to use `.enumerate()` and immediately discard the index | ||
--> $DIR/unused_enumerate_index.rs:55:19 | ||
| | ||
LL | for (_, x) in dummy.enumerate() { | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
help: remove the `.enumerate()` call | ||
| | ||
LL | for x in dummy { | ||
| ~ ~~~~~ | ||
|
||
error: aborting due to 2 previous errors | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.