Skip to content

Commit 3df6e21

Browse files
committed
misc
1 parent 5ac9657 commit 3df6e21

File tree

2 files changed

+36
-52
lines changed

2 files changed

+36
-52
lines changed

clippy_lints/src/use_self.rs

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
99
use rustc_hir::def_id::LocalDefId;
1010
use rustc_hir::intravisit::{InferKind, Visitor, VisitorExt, walk_ty};
1111
use rustc_hir::{
12-
self as hir, AmbigArg, Expr, ExprKind, FnRetTy, FnSig, GenericArgsParentheses, GenericParam, GenericParamKind,
13-
HirId, Impl, ImplItemKind, Item, ItemKind, Pat, PatExpr, PatExprKind, PatKind, Path, QPath, Ty, TyKind,
12+
self as hir, AmbigArg, Expr, ExprKind, FnRetTy, FnSig, GenericArgsParentheses, GenericParamKind, HirId, Impl,
13+
ImplItemKind, Item, ItemKind, Pat, PatExpr, PatExprKind, PatKind, Path, QPath, Ty, TyKind,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass};
1616
use rustc_middle::ty::Ty as MiddleTy;
1717
use rustc_session::impl_lint_pass;
1818
use rustc_span::Span;
19+
use std::iter;
1920

2021
declare_clippy_lint! {
2122
/// ### What it does
@@ -101,17 +102,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
101102
let types_to_skip = generics
102103
.params
103104
.iter()
104-
.filter_map(|param| match param {
105-
GenericParam {
106-
kind:
107-
GenericParamKind::Const {
108-
ty: Ty { hir_id, .. }, ..
109-
},
110-
..
111-
} => Some(*hir_id),
105+
.filter_map(|param| match param.kind {
106+
GenericParamKind::Const { ty, .. } => Some(ty.hir_id),
112107
_ => None,
113108
})
114-
.chain(std::iter::once(self_ty.hir_id))
109+
.chain([self_ty.hir_id])
115110
.collect();
116111
StackItem::Check {
117112
impl_id: item.owner_id.def_id,
@@ -212,9 +207,9 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
212207
&& let impl_ty = cx.tcx.type_of(impl_id).instantiate_identity()
213208
&& same_type_and_consts(ty, impl_ty)
214209
// Ensure the type we encounter and the one from the impl have the same lifetime parameters. It may be that
215-
// the lifetime parameters of `ty` are elided (`impl<'a> Foo<'a> { fn new() -> Self { Foo{..} } }`, in
210+
// the lifetime parameters of `ty` are elided (`impl<'a> Foo<'a> { fn new() -> Self { Foo{..} } }`), in
216211
// which case we must still trigger the lint.
217-
&& (has_no_lifetime(ty) || same_lifetimes(ty, impl_ty))
212+
&& (!has_lifetime(ty) || same_lifetimes(ty, impl_ty))
218213
&& self.msrv.meets(cx, msrvs::TYPE_ALIAS_ENUM_VARIANTS)
219214
{
220215
span_lint(cx, hir_ty.span);
@@ -227,18 +222,16 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
227222
&& cx.typeck_results().expr_ty(expr) == cx.tcx.type_of(impl_id).instantiate_identity()
228223
&& self.msrv.meets(cx, msrvs::TYPE_ALIAS_ENUM_VARIANTS)
229224
{
230-
} else {
231-
return;
232-
}
233-
match expr.kind {
234-
ExprKind::Struct(QPath::Resolved(_, path), ..) => check_path(cx, path),
235-
ExprKind::Call(fun, _) => {
236-
if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind {
237-
check_path(cx, path);
238-
}
239-
},
240-
ExprKind::Path(QPath::Resolved(_, path)) => check_path(cx, path),
241-
_ => (),
225+
match expr.kind {
226+
ExprKind::Struct(QPath::Resolved(_, path), ..) => check_path(cx, path),
227+
ExprKind::Call(fun, _) => {
228+
if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind {
229+
check_path(cx, path);
230+
}
231+
},
232+
ExprKind::Path(QPath::Resolved(_, path)) => check_path(cx, path),
233+
_ => (),
234+
}
242235
}
243236
}
244237

@@ -308,36 +301,32 @@ fn lint_path_to_variant(cx: &LateContext<'_>, path: &Path<'_>) {
308301
}
309302
}
310303

311-
/// Returns `true` if types `a` and `b` have the same lifetime parameters, otherwise returns
312-
/// `false`.
304+
/// Checks whether types `a` and `b` have the same lifetime parameters.
313305
///
314306
/// This function does not check that types `a` and `b` are the same types.
315307
fn same_lifetimes<'tcx>(a: MiddleTy<'tcx>, b: MiddleTy<'tcx>) -> bool {
316308
use rustc_middle::ty::{Adt, GenericArgKind};
317-
match (&a.kind(), &b.kind()) {
318-
(&Adt(_, args_a), &Adt(_, args_b)) => {
319-
args_a
320-
.iter()
321-
.zip(args_b.iter())
322-
.all(|(arg_a, arg_b)| match (arg_a.kind(), arg_b.kind()) {
323-
// TODO: Handle inferred lifetimes
324-
(GenericArgKind::Lifetime(inner_a), GenericArgKind::Lifetime(inner_b)) => inner_a == inner_b,
325-
(GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => same_lifetimes(type_a, type_b),
326-
_ => true,
327-
})
309+
match (a.kind(), b.kind()) {
310+
(Adt(_, args_a), Adt(_, args_b)) => {
311+
iter::zip(*args_a, *args_b).all(|(arg_a, arg_b)| match (arg_a.kind(), arg_b.kind()) {
312+
// TODO: Handle inferred lifetimes
313+
(GenericArgKind::Lifetime(inner_a), GenericArgKind::Lifetime(inner_b)) => inner_a == inner_b,
314+
(GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => same_lifetimes(type_a, type_b),
315+
_ => true,
316+
})
328317
},
329318
_ => a == b,
330319
}
331320
}
332321

333-
/// Returns `true` if `ty` has no lifetime parameter, otherwise returns `false`.
334-
fn has_no_lifetime(ty: MiddleTy<'_>) -> bool {
322+
/// Checks whether `ty` has lifetime parameters.
323+
fn has_lifetime(ty: MiddleTy<'_>) -> bool {
335324
use rustc_middle::ty::{Adt, GenericArgKind};
336325
match ty.kind() {
337-
&Adt(_, args) => !args
326+
Adt(_, args) => args
338327
.iter()
339328
// TODO: Handle inferred lifetimes
340329
.any(|arg| matches!(arg.kind(), GenericArgKind::Lifetime(..))),
341-
_ => true,
330+
_ => false,
342331
}
343332
}

clippy_utils/src/ty/mod.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -522,16 +522,11 @@ pub fn same_type_and_consts<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
522522
return false;
523523
}
524524

525-
args_a
526-
.iter()
527-
.zip(args_b.iter())
528-
.all(|(arg_a, arg_b)| match (arg_a.kind(), arg_b.kind()) {
529-
(GenericArgKind::Const(inner_a), GenericArgKind::Const(inner_b)) => inner_a == inner_b,
530-
(GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => {
531-
same_type_and_consts(type_a, type_b)
532-
},
533-
_ => true,
534-
})
525+
iter::zip(*args_a, *args_b).all(|(arg_a, arg_b)| match (arg_a.kind(), arg_b.kind()) {
526+
(GenericArgKind::Const(inner_a), GenericArgKind::Const(inner_b)) => inner_a == inner_b,
527+
(GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => same_type_and_consts(type_a, type_b),
528+
_ => true,
529+
})
535530
},
536531
_ => a == b,
537532
}

0 commit comments

Comments
 (0)