Skip to content

Commit

Permalink
suggest first() instead of get(0)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoto7250 committed May 25, 2022
1 parent 1dd0266 commit b531eb1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
9 changes: 7 additions & 2 deletions clippy_lints/src/methods/iter_next_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, cal
if let ast::LitKind::Int(start_idx, _) = start_lit.node;
then {
let mut applicability = Applicability::MachineApplicable;
let suggest = if start_idx == 0 {
format!("{}.first()", snippet_with_applicability(cx, caller_var.span, "..", &mut applicability))
} else {
format!("{}.get({})", snippet_with_applicability(cx, caller_var.span, "..", &mut applicability), start_idx)
};
span_lint_and_sugg(
cx,
ITER_NEXT_SLICE,
expr.span,
"using `.iter().next()` on a Slice without end index",
"try calling",
format!("{}.get({})", snippet_with_applicability(cx, caller_var.span, "..", &mut applicability), start_idx),
suggest,
applicability,
);
}
Expand All @@ -55,7 +60,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, cal
"using `.iter().next()` on an array",
"try calling",
format!(
"{}.get(0)",
"{}.first()",
snippet_with_applicability(cx, caller_expr.span, "..", &mut applicability)
),
applicability,
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/iter_next_slice.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ fn main() {
let s = [1, 2, 3];
let v = vec![1, 2, 3];

let _ = s.get(0);
// Should be replaced by s.get(0)
let _ = s.first();
// Should be replaced by s.first()

let _ = s.get(2);
// Should be replaced by s.get(2)

let _ = v.get(5);
// Should be replaced by v.get(5)

let _ = v.get(0);
// Should be replaced by v.get(0)
let _ = v.first();
// Should be replaced by v.first()

let o = Some(5);
o.iter().next();
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/iter_next_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {
let v = vec![1, 2, 3];

let _ = s.iter().next();
// Should be replaced by s.get(0)
// Should be replaced by s.first()

let _ = s[2..].iter().next();
// Should be replaced by s.get(2)
Expand All @@ -16,7 +16,7 @@ fn main() {
// Should be replaced by v.get(5)

let _ = v.iter().next();
// Should be replaced by v.get(0)
// Should be replaced by v.first()

let o = Some(5);
o.iter().next();
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/iter_next_slice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: using `.iter().next()` on an array
--> $DIR/iter_next_slice.rs:9:13
|
LL | let _ = s.iter().next();
| ^^^^^^^^^^^^^^^ help: try calling: `s.get(0)`
| ^^^^^^^^^^^^^^^ help: try calling: `s.first()`
|
= note: `-D clippy::iter-next-slice` implied by `-D warnings`

Expand All @@ -22,7 +22,7 @@ error: using `.iter().next()` on an array
--> $DIR/iter_next_slice.rs:18:13
|
LL | let _ = v.iter().next();
| ^^^^^^^^^^^^^^^ help: try calling: `v.get(0)`
| ^^^^^^^^^^^^^^^ help: try calling: `v.first()`

error: aborting due to 4 previous errors

0 comments on commit b531eb1

Please sign in to comment.