Skip to content

Commit 8776db9

Browse files
committed
Fix ICE in repeat_once lint
1 parent dd07860 commit 8776db9

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

clippy_lints/src/repeat_once.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ declare_lint_pass!(RepeatOnce => [REPEAT_ONCE]);
3939
impl<'tcx> LateLintPass<'tcx> for RepeatOnce {
4040
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
4141
if_chain! {
42-
if let ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind;
42+
if let ExprKind::MethodCall(path, _, [receiver, count], _) = &expr.kind;
4343
if path.ident.name == sym!(repeat);
44-
if let Some(Constant::Int(1)) = constant_context(cx, cx.typeck_results()).expr(&args[1]);
45-
if !in_macro(args[0].span);
44+
if let Some(Constant::Int(1)) = constant_context(cx, cx.typeck_results()).expr(&count);
45+
if !in_macro(receiver.span);
4646
then {
47-
let ty = walk_ptrs_ty(cx.typeck_results().expr_ty(&args[0]));
47+
let ty = walk_ptrs_ty(cx.typeck_results().expr_ty(&receiver));
4848
if ty.is_str() {
4949
span_lint_and_sugg(
5050
cx,
5151
REPEAT_ONCE,
5252
expr.span,
5353
"calling `repeat(1)` on str",
5454
"consider using `.to_string()` instead",
55-
format!("{}.to_string()", snippet(cx, args[0].span, r#""...""#)),
55+
format!("{}.to_string()", snippet(cx, receiver.span, r#""...""#)),
5656
Applicability::MachineApplicable,
5757
);
5858
} else if ty.builtin_index().is_some() {
@@ -62,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for RepeatOnce {
6262
expr.span,
6363
"calling `repeat(1)` on slice",
6464
"consider using `.to_vec()` instead",
65-
format!("{}.to_vec()", snippet(cx, args[0].span, r#""...""#)),
65+
format!("{}.to_vec()", snippet(cx, receiver.span, r#""...""#)),
6666
Applicability::MachineApplicable,
6767
);
6868
} else if is_type_diagnostic_item(cx, ty, sym!(string_type)) {
@@ -72,7 +72,7 @@ impl<'tcx> LateLintPass<'tcx> for RepeatOnce {
7272
expr.span,
7373
"calling `repeat(1)` on a string literal",
7474
"consider using `.clone()` instead",
75-
format!("{}.clone()", snippet(cx, args[0].span, r#""...""#)),
75+
format!("{}.clone()", snippet(cx, receiver.span, r#""...""#)),
7676
Applicability::MachineApplicable,
7777
);
7878
}

tests/ui/crashes/ice-5944.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![warn(clippy::repeat_once)]
2+
3+
trait Repeat {
4+
fn repeat(&self) {}
5+
}
6+
7+
impl Repeat for usize {
8+
fn repeat(&self) {}
9+
}
10+
11+
fn main() {
12+
let _ = 42.repeat();
13+
}

0 commit comments

Comments
 (0)