diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index f96e3b120a14..56274d2b6f59 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -16,7 +16,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{BytePos, Span}; use rustc_typeck::hir_ty_to_ty; -use crate::utils::{differing_macro_contexts, span_lint_and_sugg}; +use crate::utils::span_lint_and_sugg; declare_clippy_lint! { /// **What it does:** Checks for unnecessary repetition of structure name when a @@ -79,6 +79,19 @@ fn span_lint_ignore_last_segment<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, path: &'t } } +fn span_lint_on_qpath_resolved<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, qpath: &'tcx QPath<'tcx>, enum_variant: bool) { + match qpath { + QPath::Resolved(_, path) => { + if enum_variant { + span_lint_ignore_last_segment(cx, path); + } else { + span_lint(cx, path.span); + } + }, + _ => (), + }; +} + struct ImplVisitor<'a, 'tcx> { cx: &'a LateContext<'a, 'tcx>, item: &'tcx Item<'tcx>, @@ -185,29 +198,34 @@ impl<'a, 'tcx> Visitor<'tcx> for ImplVisitor<'a, 'tcx> { } } }, + // type-relative fn calls (`Foo::new()`) and tuple-like instantiation (`Foo(arg)` or `Enum::Foo(arg)`) ExprKind::Call( - Expr { - kind: - ExprKind::Path(QPath::Resolved( - _, - path - @ - Path { - res: def::Res::Def(def::DefKind::Ctor(ctor_of, _), _), - .. - }, - )), + fun + @ Expr { + kind: ExprKind::Path(ref qpath), .. }, _, ) => { if expr_ty_matches(expr, self.self_ty, self.cx) { - match ctor_of { - def::CtorOf::Struct => span_lint(self.cx, path.span), - def::CtorOf::Variant => span_lint_ignore_last_segment(self.cx, path), + let res = self.cx.tables.qpath_res(qpath, fun.hir_id); + + if let def::Res::Def(DefKind::Ctor(ctor_of, _), ..) = res { + match ctor_of { + def::CtorOf::Variant => span_lint_on_qpath_resolved(self.cx, qpath, true), + def::CtorOf::Struct => span_lint_on_qpath_resolved(self.cx, qpath, false), + } + } else if let def::Res::Def(DefKind::Variant, ..) = res { + span_lint_on_qpath_resolved(self.cx, qpath, true); } } }, + // unit enum variants (`Enum::A`) + ExprKind::Path(ref qpath) => { + if expr_ty_matches(expr, self.self_ty, self.cx) { + span_lint_on_qpath_resolved(self.cx, qpath, true); + } + }, _ => (), } walk_expr(self, expr); diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed index f8b3bb3309f1..4ce003a05c41 100644 --- a/tests/ui/use_self.fixed +++ b/tests/ui/use_self.fixed @@ -167,7 +167,7 @@ mod nesting { let _ = Self::B(42); let _ = Self::C { field: true }; // TODO: support unit struct - let _ = Enum::A; + let _ = Self::A; } } } @@ -249,9 +249,9 @@ mod paths_created_by_lowering { const A: usize = 0; const B: usize = 1; - async fn g() -> S { - Self {} - } +/* async fn g() -> S { + S {} + } */ fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] { &p[Self::A..Self::B] diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs index 1802d1e2472b..ea15c19bb9f4 100644 --- a/tests/ui/use_self.rs +++ b/tests/ui/use_self.rs @@ -249,9 +249,9 @@ mod paths_created_by_lowering { const A: usize = 0; const B: usize = 1; - async fn g() -> S { +/* async fn g() -> S { S {} - } + } */ fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] { &p[S::A..S::B] diff --git a/tests/ui/use_self.stderr b/tests/ui/use_self.stderr index a02aaf37e731..e681b30cd155 100644 --- a/tests/ui/use_self.stderr +++ b/tests/ui/use_self.stderr @@ -100,6 +100,12 @@ error: unnecessary structure name repetition LL | let _ = Enum::C { field: true }; | ^^^^ help: use the applicable keyword: `Self` +error: unnecessary structure name repetition + --> $DIR/use_self.rs:170:21 + | +LL | let _ = Enum::A; + | ^^^^ help: use the applicable keyword: `Self` + error: unnecessary structure name repetition --> $DIR/use_self.rs:216:13 | @@ -124,12 +130,6 @@ error: unnecessary structure name repetition LL | TestStruct::from_something() | ^^^^^^^^^^ help: use the applicable keyword: `Self` -error: unnecessary structure name repetition - --> $DIR/use_self.rs:253:13 - | -LL | S {} - | ^ help: use the applicable keyword: `Self` - error: unnecessary structure name repetition --> $DIR/use_self.rs:257:16 |