Skip to content

Commit 051286d

Browse files
committed
Auto merge of #7638 - xFrednet:7569-avoid-indexing-in-clippy, r=Manishearth
Avoid slice indexing in Clippy (down with the ICEs) While working on #7569 I got about 23 lint reports where we can avoid slice indexing by destructing the slice early. This is a preparation PR to avoid fixing them in the lint PR. (The implementation already takes about 300 lines without tests 😅). Either way, this should hopefully be easy to review 🙃 --- changelog: none
2 parents df7e63b + 62b4612 commit 051286d

18 files changed

+58
-58
lines changed

clippy_lints/src/blocks_in_if_conditions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
6161
// do not lint if the closure is called using an iterator (see #1141)
6262
if_chain! {
6363
if let Some(parent) = get_parent_expr(self.cx, expr);
64-
if let ExprKind::MethodCall(_, _, args, _) = parent.kind;
65-
let caller = self.cx.typeck_results().expr_ty(&args[0]);
64+
if let ExprKind::MethodCall(_, _, [self_arg, ..], _) = &parent.kind;
65+
let caller = self.cx.typeck_results().expr_ty(self_arg);
6666
if let Some(iter_id) = self.cx.tcx.get_diagnostic_item(sym::Iterator);
6767
if implements_trait(self.cx, caller, iter_id, &[]);
6868
then {

clippy_lints/src/casts/cast_ptr_alignment.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
1919
cx.typeck_results().expr_ty(expr),
2020
);
2121
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
22-
} else if let ExprKind::MethodCall(method_path, _, args, _) = expr.kind {
22+
} else if let ExprKind::MethodCall(method_path, _, [self_arg, ..], _) = &expr.kind {
2323
if_chain! {
2424
if method_path.ident.name == sym!(cast);
2525
if let Some(generic_args) = method_path.args;
@@ -28,7 +28,7 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
2828
if !is_hir_ty_cfg_dependant(cx, cast_to);
2929
then {
3030
let (cast_from, cast_to) =
31-
(cx.typeck_results().expr_ty(&args[0]), cx.typeck_results().expr_ty(expr));
31+
(cx.typeck_results().expr_ty(self_arg), cx.typeck_results().expr_ty(expr));
3232
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
3333
}
3434
}

clippy_lints/src/floating_point_arithmetic.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -362,22 +362,22 @@ fn detect_hypot(cx: &LateContext<'_>, args: &[Expr<'_>]) -> Option<String> {
362362
if_chain! {
363363
if let ExprKind::MethodCall(
364364
PathSegment { ident: lmethod_name, .. },
365-
ref _lspan,
366-
largs,
365+
_lspan,
366+
[largs_0, largs_1, ..],
367367
_
368-
) = add_lhs.kind;
368+
) = &add_lhs.kind;
369369
if let ExprKind::MethodCall(
370370
PathSegment { ident: rmethod_name, .. },
371-
ref _rspan,
372-
rargs,
371+
_rspan,
372+
[rargs_0, rargs_1, ..],
373373
_
374-
) = add_rhs.kind;
374+
) = &add_rhs.kind;
375375
if lmethod_name.as_str() == "powi" && rmethod_name.as_str() == "powi";
376-
if let Some((lvalue, _)) = constant(cx, cx.typeck_results(), &largs[1]);
377-
if let Some((rvalue, _)) = constant(cx, cx.typeck_results(), &rargs[1]);
376+
if let Some((lvalue, _)) = constant(cx, cx.typeck_results(), largs_1);
377+
if let Some((rvalue, _)) = constant(cx, cx.typeck_results(), rargs_1);
378378
if Int(2) == lvalue && Int(2) == rvalue;
379379
then {
380-
return Some(format!("{}.hypot({})", Sugg::hir(cx, &largs[0], ".."), Sugg::hir(cx, &rargs[0], "..")));
380+
return Some(format!("{}.hypot({})", Sugg::hir(cx, largs_0, ".."), Sugg::hir(cx, rargs_0, "..")));
381381
}
382382
}
383383
}
@@ -407,8 +407,8 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
407407
if cx.typeck_results().expr_ty(lhs).is_floating_point();
408408
if let Some((value, _)) = constant(cx, cx.typeck_results(), rhs);
409409
if F32(1.0) == value || F64(1.0) == value;
410-
if let ExprKind::MethodCall(path, _, method_args, _) = lhs.kind;
411-
if cx.typeck_results().expr_ty(&method_args[0]).is_floating_point();
410+
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &lhs.kind;
411+
if cx.typeck_results().expr_ty(self_arg).is_floating_point();
412412
if path.ident.name.as_str() == "exp";
413413
then {
414414
span_lint_and_sugg(
@@ -419,7 +419,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
419419
"consider using",
420420
format!(
421421
"{}.exp_m1()",
422-
Sugg::hir(cx, &method_args[0], "..")
422+
Sugg::hir(cx, self_arg, "..")
423423
),
424424
Applicability::MachineApplicable,
425425
);
@@ -617,16 +617,16 @@ fn check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>) {
617617
rhs,
618618
) = &expr.kind;
619619
if are_same_base_logs(cx, lhs, rhs);
620-
if let ExprKind::MethodCall(_, _, largs, _) = lhs.kind;
621-
if let ExprKind::MethodCall(_, _, rargs, _) = rhs.kind;
620+
if let ExprKind::MethodCall(_, _, [largs_self, ..], _) = &lhs.kind;
621+
if let ExprKind::MethodCall(_, _, [rargs_self, ..], _) = &rhs.kind;
622622
then {
623623
span_lint_and_sugg(
624624
cx,
625625
SUBOPTIMAL_FLOPS,
626626
expr.span,
627627
"log base can be expressed more clearly",
628628
"consider using",
629-
format!("{}.log({})", Sugg::hir(cx, &largs[0], ".."), Sugg::hir(cx, &rargs[0], ".."),),
629+
format!("{}.log({})", Sugg::hir(cx, largs_self, ".."), Sugg::hir(cx, rargs_self, ".."),),
630630
Applicability::MachineApplicable,
631631
);
632632
}

clippy_lints/src/if_let_mutex.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
138138

139139
fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
140140
if_chain! {
141-
if let ExprKind::MethodCall(path, _span, args, _) = &expr.kind;
141+
if let ExprKind::MethodCall(path, _span, [self_arg, ..], _) = &expr.kind;
142142
if path.ident.as_str() == "lock";
143-
let ty = cx.typeck_results().expr_ty(&args[0]);
143+
let ty = cx.typeck_results().expr_ty(self_arg);
144144
if is_type_diagnostic_item(cx, ty, sym!(mutex_type));
145145
then {
146-
Some(&args[0])
146+
Some(self_arg)
147147
} else {
148148
None
149149
}

clippy_lints/src/if_let_some_result.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ impl<'tcx> LateLintPass<'tcx> for OkIfLet {
4646
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
4747
if_chain! { //begin checking variables
4848
if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr);
49-
if let ExprKind::MethodCall(_, ok_span, result_types, _) = let_expr.kind; //check is expr.ok() has type Result<T,E>.ok(, _)
49+
if let ExprKind::MethodCall(_, ok_span, [ref result_types_0, ..], _) = let_expr.kind; //check is expr.ok() has type Result<T,E>.ok(, _)
5050
if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = let_pat.kind; //get operation
5151
if method_chain_args(let_expr, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
52-
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&result_types[0]), sym::result_type);
52+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(result_types_0), sym::result_type);
5353
if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some";
5454

5555
then {

clippy_lints/src/loops/needless_range_loop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,10 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
301301
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
302302
if_chain! {
303303
// a range index op
304-
if let ExprKind::MethodCall(meth, _, args, _) = expr.kind;
304+
if let ExprKind::MethodCall(meth, _, [args_0, args_1, ..], _) = &expr.kind;
305305
if (meth.ident.name == sym::index && match_trait_method(self.cx, expr, &paths::INDEX))
306306
|| (meth.ident.name == sym::index_mut && match_trait_method(self.cx, expr, &paths::INDEX_MUT));
307-
if !self.check(&args[1], &args[0], expr);
307+
if !self.check(args_1, args_0, expr);
308308
then { return }
309309
}
310310

clippy_lints/src/mem_forget.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ declare_lint_pass!(MemForget => [MEM_FORGET]);
2828

2929
impl<'tcx> LateLintPass<'tcx> for MemForget {
3030
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
31-
if let ExprKind::Call(path_expr, args) = e.kind {
31+
if let ExprKind::Call(path_expr, [ref first_arg, ..]) = e.kind {
3232
if let ExprKind::Path(ref qpath) = path_expr.kind {
3333
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
3434
if match_def_path(cx, def_id, &paths::MEM_FORGET) {
35-
let forgot_ty = cx.typeck_results().expr_ty(&args[0]);
35+
let forgot_ty = cx.typeck_results().expr_ty(first_arg);
3636

3737
if forgot_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {
3838
span_lint(cx, MEM_FORGET, e.span, "usage of `mem::forget` on `Drop` type");

clippy_lints/src/methods/or_fun_call.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ pub(super) fn check<'tcx>(
9696
(&paths::RESULT, true, &["or", "unwrap_or"], "else"),
9797
];
9898

99-
if let hir::ExprKind::MethodCall(path, _, args, _) = &arg.kind {
99+
if let hir::ExprKind::MethodCall(path, _, [self_arg, ..], _) = &arg.kind {
100100
if path.ident.name == sym::len {
101-
let ty = cx.typeck_results().expr_ty(&args[0]).peel_refs();
101+
let ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
102102

103103
match ty.kind() {
104104
ty::Slice(_) | ty::Array(_, _) | ty::Str => return,

clippy_lints/src/methods/utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ pub(super) fn derefs_to_slice<'tcx>(
2424
}
2525
}
2626

27-
if let hir::ExprKind::MethodCall(path, _, args, _) = expr.kind {
28-
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
29-
Some(&args[0])
27+
if let hir::ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind {
28+
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(self_arg)) {
29+
Some(self_arg)
3030
} else {
3131
None
3232
}

clippy_lints/src/misc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,12 @@ fn is_signum(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
513513
}
514514

515515
if_chain! {
516-
if let ExprKind::MethodCall(method_name, _, expressions, _) = expr.kind;
516+
if let ExprKind::MethodCall(method_name, _, [ref self_arg, ..], _) = expr.kind;
517517
if sym!(signum) == method_name.ident.name;
518518
// Check that the receiver of the signum() is a float (expressions[0] is the receiver of
519519
// the method call)
520520
then {
521-
return is_float(cx, &expressions[0]);
521+
return is_float(cx, self_arg);
522522
}
523523
}
524524
false

clippy_lints/src/mut_mutex_lock.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ declare_lint_pass!(MutMutexLock => [MUT_MUTEX_LOCK]);
4747
impl<'tcx> LateLintPass<'tcx> for MutMutexLock {
4848
fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) {
4949
if_chain! {
50-
if let ExprKind::MethodCall(path, method_span, args, _) = &ex.kind;
50+
if let ExprKind::MethodCall(path, method_span, [self_arg, ..], _) = &ex.kind;
5151
if path.ident.name == sym!(lock);
52-
let ty = cx.typeck_results().expr_ty(&args[0]);
52+
let ty = cx.typeck_results().expr_ty(self_arg);
5353
if let ty::Ref(_, inner_ty, Mutability::Mut) = ty.kind();
5454
if is_type_diagnostic_item(cx, inner_ty, sym!(mutex_type));
5555
then {

clippy_lints/src/open_options.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
3131

3232
impl<'tcx> LateLintPass<'tcx> for OpenOptions {
3333
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
34-
if let ExprKind::MethodCall(path, _, arguments, _) = e.kind {
35-
let obj_ty = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
34+
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &e.kind {
35+
let obj_ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
3636
if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
3737
let mut options = Vec::new();
38-
get_open_options(cx, &arguments[0], &mut options);
38+
get_open_options(cx, self_arg, &mut options);
3939
check_open_options(cx, &options, e.span);
4040
}
4141
}

clippy_lints/src/ptr_offset_with_cast.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ fn expr_as_ptr_offset_call<'tcx>(
9292
cx: &LateContext<'tcx>,
9393
expr: &'tcx Expr<'_>,
9494
) -> Option<(&'tcx Expr<'tcx>, &'tcx Expr<'tcx>, Method)> {
95-
if let ExprKind::MethodCall(path_segment, _, args, _) = expr.kind {
96-
if is_expr_ty_raw_ptr(cx, &args[0]) {
95+
if let ExprKind::MethodCall(path_segment, _, [arg_0, arg_1, ..], _) = &expr.kind {
96+
if is_expr_ty_raw_ptr(cx, arg_0) {
9797
if path_segment.ident.name == sym::offset {
98-
return Some((&args[0], &args[1], Method::Offset));
98+
return Some((arg_0, arg_1, Method::Offset));
9999
}
100100
if path_segment.ident.name == sym!(wrapping_offset) {
101-
return Some((&args[0], &args[1], Method::WrappingOffset));
101+
return Some((arg_0, arg_1, Method::WrappingOffset));
102102
}
103103
}
104104
}

clippy_lints/src/strings.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ declare_lint_pass!(StrToString => [STR_TO_STRING]);
345345
impl LateLintPass<'_> for StrToString {
346346
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
347347
if_chain! {
348-
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind;
348+
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind;
349349
if path.ident.name == sym!(to_string);
350-
let ty = cx.typeck_results().expr_ty(&args[0]);
350+
let ty = cx.typeck_results().expr_ty(self_arg);
351351
if let ty::Ref(_, ty, ..) = ty.kind();
352352
if *ty.kind() == ty::Str;
353353
then {
@@ -394,9 +394,9 @@ declare_lint_pass!(StringToString => [STRING_TO_STRING]);
394394
impl LateLintPass<'_> for StringToString {
395395
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
396396
if_chain! {
397-
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind;
397+
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind;
398398
if path.ident.name == sym!(to_string);
399-
let ty = cx.typeck_results().expr_ty(&args[0]);
399+
let ty = cx.typeck_results().expr_ty(self_arg);
400400
if is_type_diagnostic_item(cx, ty, sym::string_type);
401401
then {
402402
span_lint_and_help(

clippy_lints/src/to_string_in_display.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ impl LateLintPass<'_> for ToStringInDisplay {
9292
if_chain! {
9393
if self.in_display_impl;
9494
if let Some(self_hir_id) = self.self_hir_id;
95-
if let ExprKind::MethodCall(path, _, args, _) = expr.kind;
95+
if let ExprKind::MethodCall(path, _, [ref self_arg, ..], _) = expr.kind;
9696
if path.ident.name == sym!(to_string);
9797
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
9898
if is_diag_trait_item(cx, expr_def_id, sym::ToString);
99-
if path_to_local_id(&args[0], self_hir_id);
99+
if path_to_local_id(self_arg, self_hir_id);
100100
then {
101101
span_lint(
102102
cx,

clippy_lints/src/undropped_manually_drops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ declare_lint_pass!(UndroppedManuallyDrops => [UNDROPPED_MANUALLY_DROPS]);
3737

3838
impl LateLintPass<'tcx> for UndroppedManuallyDrops {
3939
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
40-
if let Some(args) = match_function_call(cx, expr, &paths::DROP) {
41-
let ty = cx.typeck_results().expr_ty(&args[0]);
40+
if let Some([arg_0, ..]) = match_function_call(cx, expr, &paths::DROP) {
41+
let ty = cx.typeck_results().expr_ty(arg_0);
4242
if is_type_lang_item(cx, ty, lang_items::LangItem::ManuallyDrop) {
4343
span_lint_and_help(
4444
cx,

clippy_lints/src/unused_io_amount.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
4545

4646
match expr.kind {
4747
hir::ExprKind::Match(res, _, _) if is_try(cx, expr).is_some() => {
48-
if let hir::ExprKind::Call(func, args) = res.kind {
48+
if let hir::ExprKind::Call(func, [ref arg_0, ..]) = res.kind {
4949
if matches!(
5050
func.kind,
5151
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryTraitBranch, _))
5252
) {
53-
check_map_error(cx, &args[0], expr);
53+
check_map_error(cx, arg_0, expr);
5454
}
5555
} else {
5656
check_map_error(cx, res, expr);
5757
}
5858
},
59-
hir::ExprKind::MethodCall(path, _, args, _) => match &*path.ident.as_str() {
59+
hir::ExprKind::MethodCall(path, _, [ref arg_0, ..], _) => match &*path.ident.as_str() {
6060
"expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
61-
check_map_error(cx, &args[0], expr);
61+
check_map_error(cx, arg_0, expr);
6262
},
6363
_ => (),
6464
},

clippy_lints/src/utils/internal_lints.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,10 @@ impl<'tcx> LateLintPass<'tcx> for CompilerLintFunctions {
504504
}
505505

506506
if_chain! {
507-
if let ExprKind::MethodCall(path, _, args, _) = expr.kind;
507+
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind;
508508
let fn_name = path.ident;
509509
if let Some(sugg) = self.map.get(&*fn_name.as_str());
510-
let ty = cx.typeck_results().expr_ty(&args[0]).peel_refs();
510+
let ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
511511
if match_type(cx, ty, &paths::EARLY_CONTEXT)
512512
|| match_type(cx, ty, &paths::LATE_CONTEXT);
513513
then {

0 commit comments

Comments
 (0)