-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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,75 @@ | ||
use super::UNUSED_ENUMERATE_INDEX; | ||
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then}; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::sugg; | ||
use clippy_utils::visitors::is_local_used; | ||
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 pat_span = pat.span; | ||
|
||
let PatKind::Tuple(pat, _) = pat.kind else { | ||
return; | ||
}; | ||
|
||
if pat.len() != 2 { | ||
return; | ||
} | ||
|
||
let arg_span = arg.span; | ||
|
||
let ExprKind::MethodCall(method, self_arg, [], _) = arg.kind else { | ||
return; | ||
}; | ||
|
||
if method.ident.as_str() != "enumerate" { | ||
return; | ||
} | ||
|
||
let ty = cx.typeck_results().expr_ty(arg); | ||
|
||
if !pat_is_wild(cx, &pat[0].kind, body) { | ||
return; | ||
} | ||
|
||
let new_pat_span = pat[1].span; | ||
|
||
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; | ||
} | ||
|
||
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, new_pat_span, "value").into_owned()), | ||
(arg_span, base_iter.to_string()), | ||
], | ||
); | ||
}, | ||
); | ||
} | ||
|
||
/// Returns `true` if the pattern is a `PatWild` or an ident prefixed with `_`. | ||
fn pat_is_wild<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx PatKind<'_>, body: &'tcx Expr<'_>) -> bool { | ||
match *pat { | ||
PatKind::Wild => true, | ||
PatKind::Binding(_, id, ident, None) if ident.as_str().starts_with('_') => !is_local_used(cx, body, id), | ||
_ => false, | ||
} | ||
} |
This file contains 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,10 @@ | ||
// run-rustfix | ||
#![allow(unused)] | ||
#![warn(clippy::unused_enumerate_index)] | ||
|
||
fn main() { | ||
let v = [1, 2, 3]; | ||
for x in v.iter() { | ||
print!("{x}"); | ||
} | ||
} |
This file contains 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,10 @@ | ||
// run-rustfix | ||
#![allow(unused)] | ||
#![warn(clippy::unused_enumerate_index)] | ||
|
||
fn main() { | ||
let v = [1, 2, 3]; | ||
for (_, x) in v.iter().enumerate() { | ||
print!("{x}"); | ||
} | ||
} |
This file contains 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,15 @@ | ||
error: you seem to use `.enumerate()` and immediately discard the index | ||
--> $DIR/unused_enumerate_index.rs:7: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: aborting due to previous error | ||
|