Skip to content

Fix use_self false generic arg false positive. #4242

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
62 changes: 47 additions & 15 deletions clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use if_chain::if_chain;
use rustc::hir;
use rustc::hir::def::{CtorKind, DefKind, Res};
use rustc::hir::intravisit::{walk_item, walk_path, walk_ty, NestedVisitorMap, Visitor};
use rustc::hir::intravisit::{walk_expr, walk_item, walk_ty, NestedVisitorMap, Visitor};
use rustc::hir::*;
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
use rustc::ty;
Expand All @@ -10,7 +10,7 @@ use rustc::{declare_lint_pass, declare_tool_lint};
use rustc_errors::Applicability;
use syntax_pos::symbol::kw;

use crate::utils::span_lint_and_sugg;
use crate::utils::{span_lint_and_sugg, SpanlessEq};

declare_clippy_lint! {
/// **What it does:** Checks for unnecessary repetition of structure name when a
Expand Down Expand Up @@ -52,10 +52,16 @@ declare_lint_pass!(UseSelf => [USE_SELF]);
const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";

fn span_use_self_lint(cx: &LateContext<'_, '_>, path: &Path) {
// Path segments only include actual path, no methods or fields.
let last_path_span = path.segments.last().expect(SEGMENTS_MSG).ident.span;
// Only take path up to the end of last_path_span.
let span = path.span.with_hi(last_path_span.hi());
let last_segment = path.segments.last().expect(SEGMENTS_MSG);
let no_args = last_segment.generic_args().is_empty();

let span = if no_args {
// The spans of paths with no generic args sometimes include
// extra segments that must be trimmed.
path.span.with_hi(last_segment.ident.span.hi())
} else {
path.span
};

span_lint_and_sugg(
cx,
Expand Down Expand Up @@ -218,18 +224,44 @@ struct UseSelfVisitor<'a, 'tcx> {
cx: &'a LateContext<'a, 'tcx>,
}

impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
fn visit_path(&mut self, path: &'tcx Path, _id: HirId) {
if path.segments.last().expect(SEGMENTS_MSG).ident.name != kw::SelfUpper {
if self.item_path.res == path.res {
span_use_self_lint(self.cx, path);
} else if let Res::Def(DefKind::Ctor(def::CtorOf::Struct, CtorKind::Fn), ctor_did) = path.res {
if self.item_path.res.opt_def_id() == self.cx.tcx.parent(ctor_did) {
span_use_self_lint(self.cx, path);
impl<'a, 'tcx: 'a> UseSelfVisitor<'a, 'tcx> {
fn check_path(&self, path: &hir::QPath) {
if let hir::QPath::Resolved(_, path) = path {
let last_segment = path.segments.last().expect(SEGMENTS_MSG);
if last_segment.ident.name != kw::SelfUpper {
if self.item_path.res == path.res {
if SpanlessEq::new(&self.cx).eq_path(self.item_path, path) {
span_use_self_lint(self.cx, path);
}
} else if let Res::Def(DefKind::Ctor(def::CtorOf::Struct, CtorKind::Fn), ctor_did) = path.res {
if self.item_path.res.opt_def_id() == self.cx.tcx.parent(ctor_did) {
span_use_self_lint(self.cx, path);
}
}
}
}
walk_path(self, path);
}
}

impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx Expr) {
match &expr.node {
ExprKind::Struct(path, ..) => self.check_path(&*path),
ExprKind::Call(path, ..) => {
if let ExprKind::Path(path) = &path.node {
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment here why it is necessary to only check_paths if the path is from a call and therefore it is not possible to use visit_path?

self.check_path(&*path);
}
},
_ => {},
}
walk_expr(self, expr);
}

fn visit_ty(&mut self, t: &'tcx hir::Ty) {
if let hir::TyKind::Path(path) = &t.node {
self.check_path(path);
}
walk_ty(self, t);
}

fn visit_item(&mut self, item: &'tcx Item) {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
}
}

fn eq_path(&mut self, left: &Path, right: &Path) -> bool {
pub fn eq_path(&mut self, left: &Path, right: &Path) -> bool {
left.is_global() == right.is_global()
&& over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
}
Expand Down
27 changes: 27 additions & 0 deletions tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,30 @@ mod rustfix {
}
}
}

mod issue4143 {
use std::marker::PhantomData;

struct A<T> {
t: PhantomData<T>,
}

trait T {
fn f();
fn g();
}

impl T for A<u64> {
fn f() {}
fn g() {}
}

impl T for A<u32> {
fn f() {
<A<u64>>::f();
}
fn g() {
<Self>::f();
}
}
}
27 changes: 27 additions & 0 deletions tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,30 @@ mod rustfix {
}
}
}

mod issue4143 {
use std::marker::PhantomData;

struct A<T> {
t: PhantomData<T>,
}

trait T {
fn f();
fn g();
}

impl T for A<u64> {
fn f() {}
fn g() {}
}

impl T for A<u32> {
fn f() {
<A<u64>>::f();
}
fn g() {
<A<u32>>::f();
}
}
}
8 changes: 7 additions & 1 deletion tests/ui/use_self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,11 @@ error: unnecessary structure name repetition
LL | nested::A {};
| ^^^^^^^^^ help: use the applicable keyword: `Self`

error: aborting due to 31 previous errors
error: unnecessary structure name repetition
--> $DIR/use_self.rs:334:14
|
LL | <A<u32>>::f();
| ^^^^^^ help: use the applicable keyword: `Self`

error: aborting due to 32 previous errors