Skip to content

Commit 4181ff5

Browse files
authored
Do not attempt to use nth with non-usize argument (rust-lang#16272)
Fixes rust-lang#16270 ---- changelog: [`needless_collect`]: do not attempt to suggest `nth` when argument is not a `usize`
2 parents a55845c + 354adbc commit 4181ff5

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

clippy_lints/src/methods/needless_collect.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ pub(super) fn check<'tcx>(
8181
},
8282
_ => return,
8383
};
84-
} else if let ExprKind::Index(_, index, _) = parent.kind {
84+
} else if let ExprKind::Index(_, index, _) = parent.kind
85+
&& cx.typeck_results().expr_ty(index).is_usize()
86+
{
8587
app = Applicability::MaybeIncorrect;
8688
let snip = snippet_with_applicability(cx, index.span, "_", &mut app);
8789
sugg = format!("nth({snip}).unwrap()");

tests/ui/needless_collect.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,8 @@ mod issue8055_regression {
214214
.len();
215215
}
216216
}
217+
218+
fn issue16270() {
219+
// Do not lint, `..` implements `Index` but is not `usize`
220+
_ = &(1..3).collect::<Vec<i32>>()[..];
221+
}

tests/ui/needless_collect.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,8 @@ mod issue8055_regression {
214214
.len();
215215
}
216216
}
217+
218+
fn issue16270() {
219+
// Do not lint, `..` implements `Index` but is not `usize`
220+
_ = &(1..3).collect::<Vec<i32>>()[..];
221+
}

0 commit comments

Comments
 (0)