about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkyoto7250 <50972773+kyoto7250@users.noreply.github.com>2022-05-24 18:55:39 +0900
committerkyoto7250 <50972773+kyoto7250@users.noreply.github.com>2022-05-25 09:08:25 +0900
commitb531eb1a7aca3357d2d5fce97f3f07d2a6956bfe (patch)
treeb6590cf21beb366f5c89b09f7756e03c77594ee7
parent1dd026698d4f6c5848fb88d48b2d4248846bb41c (diff)
downloadrust-b531eb1a7aca3357d2d5fce97f3f07d2a6956bfe.tar.gz
rust-b531eb1a7aca3357d2d5fce97f3f07d2a6956bfe.zip
suggest first() instead of get(0)
-rw-r--r--clippy_lints/src/methods/iter_next_slice.rs9
-rw-r--r--tests/ui/iter_next_slice.fixed8
-rw-r--r--tests/ui/iter_next_slice.rs4
-rw-r--r--tests/ui/iter_next_slice.stderr4
4 files changed, 15 insertions, 10 deletions
diff --git a/clippy_lints/src/methods/iter_next_slice.rs b/clippy_lints/src/methods/iter_next_slice.rs
index d053ff56756..b8d1dabe007 100644
--- a/clippy_lints/src/methods/iter_next_slice.rs
+++ b/clippy_lints/src/methods/iter_next_slice.rs
@@ -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,
diff --git a/tests/ui/iter_next_slice.fixed b/tests/ui/iter_next_slice.fixed
index 11ffc8edb14..f612d26aaab 100644
--- a/tests/ui/iter_next_slice.fixed
+++ b/tests/ui/iter_next_slice.fixed
@@ -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();
diff --git a/tests/ui/iter_next_slice.rs b/tests/ui/iter_next_slice.rs
index e0d3aabd54a..5195f1c8667 100644
--- a/tests/ui/iter_next_slice.rs
+++ b/tests/ui/iter_next_slice.rs
@@ -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();
diff --git a/tests/ui/iter_next_slice.stderr b/tests/ui/iter_next_slice.stderr
index a78d2c2d5e8..d8b89061ff8 100644
--- a/tests/ui/iter_next_slice.stderr
+++ b/tests/ui/iter_next_slice.stderr
@@ -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