Skip to content

Commit acc521b

Browse files
committed
suggest first() instead of get(0)
1 parent c634cec commit acc521b

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

clippy_lints/src/methods/iter_next_slice.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, cal
3434
if let ast::LitKind::Int(start_idx, _) = start_lit.node;
3535
then {
3636
let mut applicability = Applicability::MachineApplicable;
37+
let suggest = if start_idx == 0 {
38+
format!("{}.first()", snippet_with_applicability(cx, caller_var.span, "..", &mut applicability))
39+
} else {
40+
format!("{}.get({})", snippet_with_applicability(cx, caller_var.span, "..", &mut applicability), start_idx)
41+
};
3742
span_lint_and_sugg(
3843
cx,
3944
ITER_NEXT_SLICE,
4045
expr.span,
4146
"using `.iter().next()` on a Slice without end index",
4247
"try calling",
43-
format!("{}.get({})", snippet_with_applicability(cx, caller_var.span, "..", &mut applicability), start_idx),
48+
suggest,
4449
applicability,
4550
);
4651
}
@@ -55,7 +60,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, cal
5560
"using `.iter().next()` on an array",
5661
"try calling",
5762
format!(
58-
"{}.get(0)",
63+
"{}.first()",
5964
snippet_with_applicability(cx, caller_expr.span, "..", &mut applicability)
6065
),
6166
applicability,

tests/ui/iter_next_slice.fixed

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ fn main() {
66
let s = [1, 2, 3];
77
let v = vec![1, 2, 3];
88

9-
let _ = s.get(0);
10-
// Should be replaced by s.get(0)
9+
let _ = s.first();
10+
// Should be replaced by s.first()
1111

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

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

18-
let _ = v.get(0);
19-
// Should be replaced by v.get(0)
18+
let _ = v.first();
19+
// Should be replaced by v.first()
2020

2121
let o = Some(5);
2222
o.iter().next();

tests/ui/iter_next_slice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
let v = vec![1, 2, 3];
88

99
let _ = s.iter().next();
10-
// Should be replaced by s.get(0)
10+
// Should be replaced by s.first()
1111

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

1818
let _ = v.iter().next();
19-
// Should be replaced by v.get(0)
19+
// Should be replaced by v.first()
2020

2121
let o = Some(5);
2222
o.iter().next();

tests/ui/iter_next_slice.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: using `.iter().next()` on an array
22
--> $DIR/iter_next_slice.rs:9:13
33
|
44
LL | let _ = s.iter().next();
5-
| ^^^^^^^^^^^^^^^ help: try calling: `s.get(0)`
5+
| ^^^^^^^^^^^^^^^ help: try calling: `s.first()`
66
|
77
= note: `-D clippy::iter-next-slice` implied by `-D warnings`
88

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

2727
error: aborting due to 4 previous errors
2828

0 commit comments

Comments
 (0)