Skip to content

Commit 097ef51

Browse files
committed
refactor: remove unnecessary variables
1 parent 4bcadde commit 097ef51

File tree

8 files changed

+67
-76
lines changed

8 files changed

+67
-76
lines changed

clippy_lints/src/dereference.rs

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -798,57 +798,55 @@ fn walk_parents<'tcx>(
798798
}),
799799
ExprKind::MethodCall(_, receiver, args, _) => {
800800
let id = cx.typeck_results().type_dependent_def_id(parent.hir_id).unwrap();
801-
std::iter::once(receiver)
802-
.chain(args.iter())
803-
.position(|arg| arg.hir_id == child_id)
804-
.map(|i| {
805-
if i == 0 {
806-
// Check for calls to trait methods where the trait is implemented on a reference.
807-
// Two cases need to be handled:
808-
// * `self` methods on `&T` will never have auto-borrow
809-
// * `&self` methods on `&T` can have auto-borrow, but `&self` methods on `T` will take
810-
// priority.
811-
if e.hir_id != child_id {
812-
Position::ReborrowStable(precedence)
813-
} else if let Some(trait_id) = cx.tcx.trait_of_item(id)
814-
&& let arg_ty = cx.tcx.erase_regions(cx.typeck_results().expr_ty_adjusted(e))
815-
&& let ty::Ref(_, sub_ty, _) = *arg_ty.kind()
816-
&& let subs = match cx
817-
.typeck_results()
818-
.node_substs_opt(parent.hir_id)
819-
.and_then(|subs| subs.get(1..))
820-
{
821-
Some(subs) => cx.tcx.mk_substs(subs.iter().copied()),
822-
None => cx.tcx.mk_substs(std::iter::empty::<ty::subst::GenericArg<'_>>()),
823-
} && let impl_ty = if cx.tcx.fn_sig(id).skip_binder().inputs()[0].is_ref() {
824-
// Trait methods taking `&self`
825-
sub_ty
826-
} else {
827-
// Trait methods taking `self`
828-
arg_ty
829-
} && impl_ty.is_ref()
830-
&& cx.tcx.infer_ctxt().enter(|infcx|
831-
infcx
832-
.type_implements_trait(trait_id, impl_ty, subs, cx.param_env)
833-
.must_apply_modulo_regions()
834-
)
801+
if receiver.hir_id == child_id {
802+
// Check for calls to trait methods where the trait is implemented on a reference.
803+
// Two cases need to be handled:
804+
// * `self` methods on `&T` will never have auto-borrow
805+
// * `&self` methods on `&T` can have auto-borrow, but `&self` methods on `T` will take
806+
// priority.
807+
if e.hir_id != child_id {
808+
return Some(Position::ReborrowStable(precedence))
809+
} else if let Some(trait_id) = cx.tcx.trait_of_item(id)
810+
&& let arg_ty = cx.tcx.erase_regions(cx.typeck_results().expr_ty_adjusted(e))
811+
&& let ty::Ref(_, sub_ty, _) = *arg_ty.kind()
812+
&& let subs = match cx
813+
.typeck_results()
814+
.node_substs_opt(parent.hir_id)
815+
.and_then(|subs| subs.get(1..))
835816
{
836-
Position::MethodReceiverRefImpl
817+
Some(subs) => cx.tcx.mk_substs(subs.iter().copied()),
818+
None => cx.tcx.mk_substs(std::iter::empty::<ty::subst::GenericArg<'_>>()),
819+
} && let impl_ty = if cx.tcx.fn_sig(id).skip_binder().inputs()[0].is_ref() {
820+
// Trait methods taking `&self`
821+
sub_ty
837822
} else {
838-
Position::MethodReceiver
839-
}
823+
// Trait methods taking `self`
824+
arg_ty
825+
} && impl_ty.is_ref()
826+
&& cx.tcx.infer_ctxt().enter(|infcx|
827+
infcx
828+
.type_implements_trait(trait_id, impl_ty, subs, cx.param_env)
829+
.must_apply_modulo_regions()
830+
)
831+
{
832+
return Some(Position::MethodReceiverRefImpl)
833+
} else {
834+
return Some(Position::MethodReceiver)
835+
}
836+
}
837+
args.iter()
838+
.position(|arg| arg.hir_id == child_id)
839+
.map(|i| {
840+
let ty = cx.tcx.fn_sig(id).skip_binder().inputs()[i + 1];
841+
if let ty::Param(param_ty) = ty.kind() {
842+
needless_borrow_impl_arg_position(cx, parent, i + 1, *param_ty, e, precedence, msrv)
840843
} else {
841-
let ty = cx.tcx.fn_sig(id).skip_binder().inputs()[i];
842-
if let ty::Param(param_ty) = ty.kind() {
843-
needless_borrow_impl_arg_position(cx, parent, i, *param_ty, e, precedence, msrv)
844-
} else {
845-
ty_auto_deref_stability(
846-
cx,
847-
cx.tcx.erase_late_bound_regions(cx.tcx.fn_sig(id).input(i)),
848-
precedence,
849-
)
850-
.position_for_arg()
851-
}
844+
ty_auto_deref_stability(
845+
cx,
846+
cx.tcx.erase_late_bound_regions(cx.tcx.fn_sig(id).input(i + 1)),
847+
precedence,
848+
)
849+
.position_for_arg()
852850
}
853851
})
854852
},

clippy_lints/src/infinite_iter.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for InfiniteIter {
5959
MaybeInfinite => (MAYBE_INFINITE_ITER, "possible infinite iteration detected"),
6060
Finite => {
6161
return;
62-
},
62+
}
6363
};
6464
span_lint(cx, lint, expr.span, msg);
6565
}
@@ -229,11 +229,9 @@ fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
229229
return MaybeInfinite.and(is_infinite(cx, receiver));
230230
}
231231
}
232-
if method.ident.name == sym!(last) {
233-
let not_double_ended = cx
234-
.tcx
235-
.get_diagnostic_item(sym::DoubleEndedIterator)
236-
.map_or(false, |id| {
232+
if method.ident.name == sym!(last) && args.is_empty() {
233+
let not_double_ended =
234+
cx.tcx.get_diagnostic_item(sym::DoubleEndedIterator).map_or(false, |id| {
237235
!implements_trait(cx, cx.typeck_results().expr_ty(receiver), id, &[])
238236
});
239237
if not_double_ended {

clippy_lints/src/len_zero.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,15 @@ fn check_for_is_empty<'tcx>(
370370
}
371371

372372
fn check_cmp(cx: &LateContext<'_>, span: Span, method: &Expr<'_>, lit: &Expr<'_>, op: &str, compare_to: u32) {
373-
if let (&ExprKind::MethodCall(method_path, receiver, ..), &ExprKind::Lit(ref lit)) = (&method.kind, &lit.kind) {
373+
if let (&ExprKind::MethodCall(method_path, receiver, args, _), &ExprKind::Lit(ref lit)) = (&method.kind, &lit.kind) {
374374
// check if we are in an is_empty() method
375375
if let Some(name) = get_item_name(cx, method) {
376376
if name.as_str() == "is_empty" {
377377
return;
378378
}
379379
}
380380

381-
check_len(cx, span, method_path.ident.name, receiver, &lit.node, op, compare_to);
381+
check_len(cx, span, method_path.ident.name, receiver, args, &lit.node, op, compare_to);
382382
} else {
383383
check_empty_expr(cx, span, method, lit, op);
384384
}
@@ -389,6 +389,7 @@ fn check_len(
389389
span: Span,
390390
method_name: Symbol,
391391
receiver: &Expr<'_>,
392+
args: &[Expr<'_>],
392393
lit: &LitKind,
393394
op: &str,
394395
compare_to: u32,
@@ -399,7 +400,7 @@ fn check_len(
399400
return;
400401
}
401402

402-
if method_name == sym::len && has_is_empty(cx, receiver) {
403+
if method_name == sym::len && args.is_empty() && has_is_empty(cx, receiver) {
403404
let mut applicability = Applicability::MachineApplicable;
404405
span_lint_and_sugg(
405406
cx,

clippy_lints/src/methods/clone_on_copy.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ pub(super) fn check(
2121
receiver: &Expr<'_>,
2222
args: &[Expr<'_>],
2323
) {
24-
let arg = match args {
25-
[] if method_name == sym::clone => receiver,
26-
_ => return,
27-
};
24+
let arg = if method_name == sym::clone && args.is_empty() { receiver } else { return };
2825
if cx
2926
.typeck_results()
3027
.type_dependent_def_id(expr.hir_id)

clippy_lints/src/methods/clone_on_ref_ptr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ pub(super) fn check(
2020
if !(args.is_empty() && method_name == sym::clone) {
2121
return;
2222
}
23-
let arg = receiver;
24-
let obj_ty = cx.typeck_results().expr_ty(arg).peel_refs();
23+
let obj_ty = cx.typeck_results().expr_ty(receiver).peel_refs();
2524

2625
if let ty::Adt(_, subst) = obj_ty.kind() {
2726
let caller_type = if is_type_diagnostic_item(cx, obj_ty, sym::Rc) {
@@ -34,7 +33,7 @@ pub(super) fn check(
3433
return;
3534
};
3635

37-
let snippet = snippet_with_macro_callsite(cx, arg.span, "..");
36+
let snippet = snippet_with_macro_callsite(cx, receiver.span, "..");
3837

3938
span_lint_and_sugg(
4039
cx,

clippy_lints/src/methods/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3381,7 +3381,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
33813381
impl Methods {
33823382
#[allow(clippy::too_many_lines)]
33833383
fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
3384-
if let Some((name, recv, [args @ ..], span)) = method_call(expr) {
3384+
if let Some((name, recv, args, span)) = method_call(expr) {
33853385
match (name, args) {
33863386
("add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub", [_arg]) => {
33873387
zst_offset::check(cx, expr, recv);
@@ -3485,7 +3485,7 @@ impl Methods {
34853485
}
34863486
},
34873487
("last", []) | ("skip", [_]) => {
3488-
if let Some((name2, recv2, [args2 @ ..], _span2)) = method_call(recv) {
3488+
if let Some((name2, recv2, args2, _span2)) = method_call(recv) {
34893489
if let ("cloned", []) = (name2, args2) {
34903490
iter_overeager_cloned::check(cx, expr, recv, recv2, false, false);
34913491
}
@@ -3500,7 +3500,7 @@ impl Methods {
35003500
} else {
35013501
map_err_ignore::check(cx, expr, m_arg);
35023502
}
3503-
if let Some((name, recv2, [args @ ..], span2)) = method_call(recv) {
3503+
if let Some((name, recv2, args, span2)) = method_call(recv) {
35043504
match (name, args) {
35053505
("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, self.msrv),
35063506
("as_ref", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, false, self.msrv),
@@ -3520,7 +3520,7 @@ impl Methods {
35203520
manual_ok_or::check(cx, expr, recv, def, map);
35213521
},
35223522
("next", []) => {
3523-
if let Some((name2, recv2, [args2 @ ..], _)) = method_call(recv) {
3523+
if let Some((name2, recv2, args2, _)) = method_call(recv) {
35243524
match (name2, args2) {
35253525
("cloned", []) => iter_overeager_cloned::check(cx, expr, recv, recv2, false, false),
35263526
("filter", [arg]) => filter_next::check(cx, expr, recv2, arg),
@@ -3593,7 +3593,7 @@ impl Methods {
35933593
},
35943594
("step_by", [arg]) => iterator_step_by_zero::check(cx, expr, arg),
35953595
("take", [_arg]) => {
3596-
if let Some((name2, recv2, [args2 @ ..], _span2)) = method_call(recv) {
3596+
if let Some((name2, recv2, args2, _span2)) = method_call(recv) {
35973597
if let ("cloned", []) = (name2, args2) {
35983598
iter_overeager_cloned::check(cx, expr, recv, recv2, false, false);
35993599
}

clippy_lints/src/minmax.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,12 @@ fn fetch_const<'a>(
109109
args: &'a [Expr<'a>],
110110
m: MinMax,
111111
) -> Option<(MinMax, Constant, &'a Expr<'a>)> {
112-
if (receiver.is_some() && args.len() != 1) || (receiver.is_none() && args.len() != 2) {
112+
let mut args = receiver.into_iter().chain(args.into_iter());
113+
let arg0 = args.next()?;
114+
let arg1 = args.next()?;
115+
if args.next().is_some() {
113116
return None;
114117
}
115-
let (arg0, arg1) = if let Some(receiver) = receiver {
116-
(receiver, &args[0])
117-
} else {
118-
(&args[0], &args[1])
119-
};
120118
constant_simple(cx, cx.typeck_results(), arg0).map_or_else(
121119
|| constant_simple(cx, cx.typeck_results(), arg1).map(|c| (m, c, arg0)),
122120
|c| {

clippy_lints/src/unused_peekable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'tcx> Visitor<'_> for PeekableVisitor<'_, 'tcx> {
150150
..
151151
},
152152
self_arg,
153-
[remaining_args @ ..],
153+
remaining_args,
154154
_,
155155
) => {
156156
let method_name = method_name_ident.name.as_str();

0 commit comments

Comments
 (0)