Skip to content

Commit fea1c5f

Browse files
committed
refactor: remove unnecessary variables
1 parent 3955dc3 commit fea1c5f

File tree

17 files changed

+93
-108
lines changed

17 files changed

+93
-108
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
906906
if let hir::ExprKind::Closure(hir::Closure {
907907
capture_clause: hir::CaptureBy::Ref,
908908
..
909-
}) = arg.kind {
909+
}) = arg.kind
910+
{
910911
closure_span = Some(arg.span.shrink_to_lo());
911912
break;
912913
}

compiler/rustc_hir/src/hir.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1881,11 +1881,11 @@ pub enum ExprKind<'hir> {
18811881
///
18821882
/// The `PathSegment` represents the method name and its generic arguments
18831883
/// (within the angle brackets).
1884-
/// The first element of the `&[Expr]` is the expression that evaluates
1884+
/// The `&Expr` is the expression that evaluates
18851885
/// to the object on which the method is being called on (the receiver),
1886-
/// and the remaining elements are the rest of the arguments.
1886+
/// and the `&[Expr]` is the rest of the arguments.
18871887
/// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
1888-
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d], span)`.
1888+
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, x, [a, b, c, d], span)`.
18891889
/// The final `Span` represents the span of the function and arguments
18901890
/// (e.g. `foo::<Bar, Baz>(a, b, c, d)` in `x.foo::<Bar, Baz>(a, b, c, d)`
18911891
///

compiler/rustc_lint/src/array_into_iter.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
6161
}
6262

6363
// We only care about method call expressions.
64-
if let hir::ExprKind::MethodCall(call, receiver, ..) = &expr.kind {
64+
if let hir::ExprKind::MethodCall(call, receiver_arg, ..) = &expr.kind {
6565
if call.ident.name != sym::into_iter {
6666
return;
6767
}
@@ -75,7 +75,6 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
7575
};
7676

7777
// As this is a method call expression, we have at least one argument.
78-
let receiver_arg = receiver;
7978
let receiver_ty = cx.typeck_results().expr_ty(receiver_arg);
8079
let adjustments = cx.typeck_results().expr_adjustments(receiver_arg);
8180

compiler/rustc_lint/src/methods.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,13 @@ impl<'tcx> LateLintPass<'tcx> for TemporaryCStringAsPtr {
6363
}
6464

6565
match first_method_call(expr) {
66-
Some((path, receiver)) if path.ident.name == sym::as_ptr => {
67-
let unwrap_arg = receiver;
66+
Some((path, unwrap_arg)) if path.ident.name == sym::as_ptr => {
6867
let as_ptr_span = path.ident.span;
6968
match first_method_call(unwrap_arg) {
7069
Some((path, receiver))
7170
if path.ident.name == sym::unwrap || path.ident.name == sym::expect =>
7271
{
73-
let source_arg = receiver;
74-
lint_cstring_as_ptr(cx, as_ptr_span, source_arg, unwrap_arg);
72+
lint_cstring_as_ptr(cx, as_ptr_span, receiver, unwrap_arg);
7573
}
7674
_ => return,
7775
}

compiler/rustc_mir_build/src/thir/cx/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ impl<'tcx> Cx<'tcx> {
267267
// When we apply adjustments to the receiver, use the span of
268268
// the overall method call for better diagnostics. args[0]
269269
// is guaranteed to exist, since a method call always has a receiver.
270-
let old_adjustment_span = self.adjustment_span.replace((receiver.hir_id, expr_span));
270+
let old_adjustment_span =
271+
self.adjustment_span.replace((receiver.hir_id, expr_span));
271272
info!("Using method span: {:?}", expr.span);
272273
let args = std::iter::once(receiver)
273274
.chain(args.iter())

compiler/rustc_passes/src/liveness.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1041,10 +1041,11 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10411041

10421042
hir::ExprKind::MethodCall(.., receiver, ref args, _) => {
10431043
let succ = self.check_is_ty_uninhabited(expr, succ);
1044-
std::iter::once(receiver)
1045-
.chain(args.iter())
1044+
let succ = args
1045+
.iter()
10461046
.rev()
1047-
.fold(succ, |succ, expr| self.propagate_through_expr(expr, succ))
1047+
.fold(succ, |succ, expr| self.propagate_through_expr(expr, succ));
1048+
self.propagate_through_expr(receiver, succ)
10481049
}
10491050

10501051
hir::ExprKind::Tup(ref exprs) => self.propagate_through_exprs(exprs, succ),

compiler/rustc_typeck/src/check/demand.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -770,16 +770,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
770770
if let hir::ExprKind::MethodCall(ref segment, receiver, args, _) = expr.kind {
771771
let clone_trait =
772772
self.tcx.require_lang_item(LangItem::Clone, Some(segment.ident.span));
773-
if let (true, Some(true), sym::clone) = (
774-
args.is_empty(),
775-
self.typeck_results.borrow().type_dependent_def_id(expr.hir_id).map(
773+
if args.is_empty()
774+
&& self.typeck_results.borrow().type_dependent_def_id(expr.hir_id).map(
776775
|did| {
777776
let ai = self.tcx.associated_item(did);
778777
ai.trait_container(self.tcx) == Some(clone_trait)
779778
},
780-
),
781-
segment.ident.name,
782-
) {
779+
) == Some(true)
780+
&& segment.ident.name == sym::clone
781+
{
783782
// If this expression had a clone call when suggesting borrowing
784783
// we want to suggest removing it because it'd now be unnecessary.
785784
sugg_sp = receiver.span;

compiler/rustc_typeck/src/check/expr.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1195,14 +1195,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11951195
&self,
11961196
expr: &'tcx hir::Expr<'tcx>,
11971197
segment: &hir::PathSegment<'_>,
1198-
receiver: &'tcx hir::Expr<'tcx>,
1198+
rcvr: &'tcx hir::Expr<'tcx>,
11991199
args: &'tcx [hir::Expr<'tcx>],
12001200
expected: Expectation<'tcx>,
12011201
) -> Ty<'tcx> {
1202-
let rcvr = &receiver;
12031202
let rcvr_t = self.check_expr(&rcvr);
12041203
// no need to check for bot/err -- callee does that
1205-
let rcvr_t = self.structurally_resolved_type(receiver.span, rcvr_t);
1204+
let rcvr_t = self.structurally_resolved_type(rcvr.span, rcvr_t);
12061205
let span = segment.ident.span;
12071206

12081207
let method = match self.lookup_method(rcvr_t, segment, span, expr, rcvr, args) {
@@ -1219,9 +1218,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12191218
span,
12201219
rcvr_t,
12211220
segment.ident,
1222-
SelfSource::MethodCall(receiver),
1221+
SelfSource::MethodCall(rcvr),
12231222
error,
1224-
Some((receiver, args)),
1223+
Some((rcvr, args)),
12251224
) {
12261225
err.emit();
12271226
}

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1913,14 +1913,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19131913
.enumerate()
19141914
.filter(|(_, ty)| find_param_in_ty(**ty, param_to_point_at))
19151915
.collect();
1916-
let args: Vec<&'tcx hir::Expr<'tcx>> = if let Some(receiver) = receiver {
1917-
std::iter::once(receiver).chain(args.iter()).collect()
1918-
} else {
1919-
args.iter().collect()
1920-
};
1921-
19221916
// If there's one field that references the given generic, great!
1923-
if let [(idx, _)] = args_referencing_param.as_slice() && let Some(arg) = args.get(*idx) {
1917+
if let [(idx, _)] = args_referencing_param.as_slice()
1918+
&& let Some(arg) = receiver
1919+
.map_or(args.get(*idx), |rcvr| if *idx == 0 { Some(rcvr) } else { args.get(*idx - 1) }) {
19241920
error.obligation.cause.span = arg.span.find_ancestor_in_same_ctxt(error.obligation.cause.span).unwrap_or(arg.span);
19251921
error.obligation.cause.map_code(|parent_code| {
19261922
ObligationCauseCode::FunctionArgumentObligation {

src/tools/clippy/clippy_lints/src/dereference.rs

+46-48
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
},

src/tools/clippy/clippy_lints/src/infinite_iter.rs

+4-6
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 {

src/tools/clippy/clippy_lints/src/len_zero.rs

+4-3
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,

src/tools/clippy/clippy_lints/src/methods/clone_on_copy.rs

+1-4
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)

src/tools/clippy/clippy_lints/src/methods/clone_on_ref_ptr.rs

+2-3
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,

0 commit comments

Comments
 (0)