Skip to content

rework use_self impl based on ty::Ty comparison #3410 #5531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 56 additions & 41 deletions clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ declare_lint_pass!(UseSelf => [USE_SELF]);

const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";

fn span_lint<'tcx>(cx: &LateContext<'tcx>, span: Span) {
fn span_lint(cx: &LateContext<'_>, span: Span) {
span_lint_and_sugg(
cx,
USE_SELF,
Expand Down Expand Up @@ -99,12 +99,12 @@ fn span_lint_on_qpath_resolved<'tcx>(cx: &LateContext<'tcx>, qpath: &'tcx QPath<
}
}

struct ImplVisitor<'a, 'tcx> {
struct BodyVisitor<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
self_ty: Ty<'tcx>,
}

impl<'a, 'tcx> ImplVisitor<'a, 'tcx> {
impl<'a, 'tcx> BodyVisitor<'a, 'tcx> {
fn check_trait_method_impl_decl(
&mut self,
impl_item: &ImplItem<'tcx>,
Expand Down Expand Up @@ -151,46 +151,13 @@ impl<'a, 'tcx> ImplVisitor<'a, 'tcx> {
}
}

impl<'a, 'tcx> Visitor<'tcx> for ImplVisitor<'a, 'tcx> {
impl<'a, 'tcx> Visitor<'tcx> for BodyVisitor<'a, 'tcx> {
type Map = Map<'tcx>;

fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
}

fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'tcx>) {
if let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind {
match path.res {
def::Res::SelfTy(..) => {},
_ => {
match self.cx.tcx.hir().find(self.cx.tcx.hir().get_parent_node(hir_ty.hir_id)) {
Some(Node::Expr(Expr {
kind: ExprKind::Path(QPath::TypeRelative(_, _segment)),
..
})) => {
// The following block correctly identifies applicable lint locations
// but `hir_ty_to_ty` calls cause odd ICEs.
//
// if hir_ty_to_ty(self.cx.tcx, hir_ty) == self.self_ty {
// // FIXME: this span manipulation should not be necessary
// // @flip1995 found an ast lowering issue in
// // https://github.com/rust-lang/rust/blob/master/src/librustc_ast_lowering/path.rs#L142-L162
// span_lint_until_last_segment(self.cx, hir_ty.span, segment);
// }
},
_ => {
if hir_ty_to_ty(self.cx.tcx, hir_ty) == self.self_ty {
span_lint(self.cx, hir_ty.span)
}
},
}
},
}
}

walk_ty(self, hir_ty);
}

fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
fn expr_ty_matches<'tcx>(expr: &'tcx Expr<'tcx>, self_ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool {
let def_id = expr.hir_id.owner;
Expand Down Expand Up @@ -247,6 +214,52 @@ impl<'a, 'tcx> Visitor<'tcx> for ImplVisitor<'a, 'tcx> {
}
}

struct FnSigVisitor<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
self_ty: Ty<'tcx>,
}

impl<'a, 'tcx> Visitor<'tcx> for FnSigVisitor<'a, 'tcx> {
type Map = Map<'tcx>;

fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::None
}

fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'tcx>) {
if let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind {
match path.res {
def::Res::SelfTy(..) => {},
_ => {
match self.cx.tcx.hir().find(self.cx.tcx.hir().get_parent_node(hir_ty.hir_id)) {
Some(Node::Expr(Expr {
kind: ExprKind::Path(QPath::TypeRelative(_, segment)),
..
})) => {
// The following block correctly identifies applicable lint locations
// but `hir_ty_to_ty` calls cause odd ICEs.
//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment still applicable here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'll remove it.

if hir_ty_to_ty(self.cx.tcx, hir_ty) == self.self_ty {
// fixme: this span manipulation should not be necessary
// @flip1995 found an ast lowering issue in
// https://github.com/rust-lang/rust/blob/master/src/librustc_ast_lowering/path.rs#l142-l162
span_lint_until_last_segment(self.cx, hir_ty.span, segment);
}
},
_ => {
if hir_ty_to_ty(self.cx.tcx, hir_ty) == self.self_ty {
span_lint(self.cx, hir_ty.span)
}
},
}
},
}
}

walk_ty(self, hir_ty);
}
}

impl<'tcx> LateLintPass<'tcx> for UseSelf {
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
if in_external_macro(cx.sess(), impl_item.span) {
Expand All @@ -270,7 +283,8 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
// TODO: don't short-circuit upon lifetime parameters
if should_check {
let self_ty = hir_ty_to_ty(cx.tcx, hir_self_ty);
let visitor = &mut ImplVisitor { cx, self_ty };
let body_visitor = &mut BodyVisitor { cx, self_ty };
let fn_sig_visitor = &mut FnSigVisitor { cx, self_ty };

let tcx = cx.tcx;
let impl_def_id = tcx.hir().local_def_id(imp.hir_id);
Expand All @@ -279,11 +293,12 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
if let Some(impl_trait_ref) = impl_trait_ref;
if let ImplItemKind::Fn(FnSig { decl: impl_decl, .. }, impl_body_id) = &impl_item.kind;
then {
visitor.check_trait_method_impl_decl(impl_item, impl_decl, impl_trait_ref);
body_visitor.check_trait_method_impl_decl(impl_item, impl_decl, impl_trait_ref);
let body = tcx.hir().body(*impl_body_id);
visitor.visit_body(body);
body_visitor.visit_body(body);
} else {
walk_impl_item(visitor, impl_item)
walk_impl_item(body_visitor, impl_item);
walk_impl_item(fn_sig_visitor, impl_item);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ mod use_self {
Self {}
}
fn test() -> Self {
// FIXME: applicable here
Foo::new()
}
}

impl Default for Foo {
fn default() -> Self {
// FIXME: applicable here
fn default() -> Foo {
// FIXME: applicable here
Foo::new()
}
Expand Down Expand Up @@ -212,7 +214,9 @@ mod rustfix {
fn fun_1() {}

fn fun_2() {
// FIXME: applicable here
nested::A::fun_1();
// FIXME: applicable here
nested::A::A;

Self {};
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ mod use_self {
Foo {}
}
fn test() -> Foo {
// FIXME: applicable here
Foo::new()
}
}

impl Default for Foo {
// FIXME: applicable here
fn default() -> Foo {
// FIXME: applicable here
Foo::new()
Expand Down Expand Up @@ -212,7 +214,9 @@ mod rustfix {
fn fun_1() {}

fn fun_2() {
// FIXME: applicable here
nested::A::fun_1();
// FIXME: applicable here
nested::A::A;

nested::A {};
Expand Down
84 changes: 39 additions & 45 deletions tests/ui/use_self.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error: unnecessary structure name repetition
--> $DIR/use_self.rs:14:21
--> $DIR/use_self.rs:15:13
|
LL | fn new() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
LL | Foo {}
| ^^^ help: use the applicable keyword: `Self`
|
= note: `-D clippy::use-self` implied by `-D warnings`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:15:13
--> $DIR/use_self.rs:14:21
|
LL | Foo {}
| ^^^ help: use the applicable keyword: `Self`
LL | fn new() -> Foo {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:17:22
Expand All @@ -19,122 +19,116 @@ LL | fn test() -> Foo {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:23:25
|
LL | fn default() -> Foo {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:94:24
--> $DIR/use_self.rs:96:24
|
LL | fn bad(foos: &[Foo]) -> impl Iterator<Item = &Foo> {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:109:13
--> $DIR/use_self.rs:111:13
|
LL | TS(0)
| ^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:117:25
--> $DIR/use_self.rs:120:17
|
LL | fn new() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
LL | Foo {}
| ^^^ help: use the applicable keyword: `Self`
...
LL | use_self_expand!(); // Should lint in local macros
| ------------------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: unnecessary structure name repetition
--> $DIR/use_self.rs:118:17
--> $DIR/use_self.rs:119:25
|
LL | Foo {}
| ^^^ help: use the applicable keyword: `Self`
LL | fn new() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
...
LL | use_self_expand!(); // Should lint in local macros
| ------------------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: unnecessary structure name repetition
--> $DIR/use_self.rs:141:29
|
LL | fn bar() -> Bar {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:142:21
--> $DIR/use_self.rs:144:21
|
LL | Bar { foo: Foo {} }
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:153:21
--> $DIR/use_self.rs:143:29
|
LL | fn baz() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
LL | fn bar() -> Bar {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:154:13
--> $DIR/use_self.rs:156:13
|
LL | Foo {}
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:171:21
--> $DIR/use_self.rs:155:21
|
LL | fn baz() -> Foo {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:173:21
|
LL | let _ = Enum::B(42);
| ^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:172:21
--> $DIR/use_self.rs:174:21
|
LL | let _ = Enum::C { field: true };
| ^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:173:21
--> $DIR/use_self.rs:175:21
|
LL | let _ = Enum::A;
| ^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:218:13
--> $DIR/use_self.rs:222:13
|
LL | nested::A {};
| ^^^^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:254:13
--> $DIR/use_self.rs:258:13
|
LL | S {}
| ^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:282:29
--> $DIR/use_self.rs:287:13
|
LL | fn foo(value: T) -> Foo<T> {
| ^^^^^^ help: use the applicable keyword: `Self`
LL | Foo { value }
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:283:13
--> $DIR/use_self.rs:286:29
|
LL | Foo { value }
| ^^^ help: use the applicable keyword: `Self`
LL | fn foo(value: T) -> Foo<T> {
| ^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:320:21
--> $DIR/use_self.rs:324:21
|
LL | type From = T::From;
| ^^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:321:19
--> $DIR/use_self.rs:325:19
|
LL | type To = T::To;
| ^^^^^ help: use the applicable keyword: `Self`

error: aborting due to 21 previous errors
error: aborting due to 20 previous errors

Loading