def::{CtorOf, DefKind, Res},
def_id::LocalDefId,
intravisit::{walk_inf, walk_ty, Visitor},
- Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Path, QPath, TyKind,
+ Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Pat, PatKind, Path, QPath,
+ TyKind,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_semver::RustcVersion;
///
/// ### Known problems
/// - Unaddressed false negative in fn bodies of trait implementations
- /// - False positive with assotiated types in traits (#4140)
+ /// - False positive with associated types in traits (#4140)
///
/// ### Example
/// ```rust
- /// struct Foo {}
+ /// struct Foo;
/// impl Foo {
/// fn new() -> Foo {
/// Foo {}
/// ```
/// could be
/// ```rust
- /// struct Foo {}
+ /// struct Foo;
/// impl Foo {
/// fn new() -> Self {
/// Self {}
fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
if_chain! {
if !hir_ty.span.from_expansion();
- if meets_msrv(self.msrv.as_ref(), &msrvs::TYPE_ALIAS_ENUM_VARIANTS);
+ if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS);
if let Some(&StackItem::Check {
impl_id,
in_body,
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if_chain! {
if !expr.span.from_expansion();
- if meets_msrv(self.msrv.as_ref(), &msrvs::TYPE_ALIAS_ENUM_VARIANTS);
+ if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS);
if let Some(&StackItem::Check { impl_id, .. }) = self.stack.last();
if cx.typeck_results().expr_ty(expr) == cx.tcx.type_of(impl_id);
then {} else { return; }
}
}
+ fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
+ if_chain! {
+ if !pat.span.from_expansion();
+ if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS);
+ if let Some(&StackItem::Check { impl_id, .. }) = self.stack.last();
+ if let PatKind::Path(QPath::Resolved(_, path)) = pat.kind;
+ if !matches!(path.res, Res::SelfTy { .. } | Res::Def(DefKind::TyParam, _));
+ if cx.typeck_results().pat_ty(pat) == cx.tcx.type_of(impl_id);
+ if let [first, ..] = path.segments;
+ if let Some(hir_id) = first.hir_id;
+ then {
+ span_lint(cx, cx.tcx.hir().span(hir_id));
+ }
+ }
+ }
+
extract_msrv_attr!(LateContext);
}