suggest first() instead of get(0)
This commit is contained in:
parent
1dd026698d
commit
b531eb1a7a
@ -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,
|
||||
);
|
||||
}
|
||||
@ -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,
|
||||
|
@ -6,8 +6,8 @@ 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)
|
||||
@ -15,8 +15,8 @@ fn main() {
|
||||
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();
|
||||
|
@ -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)
|
||||
@ -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();
|
||||
|
@ -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`
|
||||
|
||||
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user