Skip to content

Commit e3c1aea

Browse files
author
Michael Wright
committed
use-self: correctly ignore dummy paths
1 parent a952708 commit e3c1aea

File tree

4 files changed

+108
-62
lines changed

4 files changed

+108
-62
lines changed

clippy_lints/src/use_self.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -223,30 +223,32 @@ struct UseSelfVisitor<'a, 'tcx> {
223223

224224
impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
225225
fn visit_path(&mut self, path: &'tcx Path, _id: HirId) {
226-
if path.segments.len() >= 2 && !path.segments.iter().any(|p| p.ident.span.is_dummy()) {
227-
let last_but_one = &path.segments[path.segments.len() - 2];
228-
if last_but_one.ident.name != kw::SelfUpper {
229-
let enum_def_id = match path.res {
230-
Res::Def(DefKind::Variant, variant_def_id) => self.cx.tcx.parent(variant_def_id),
231-
Res::Def(DefKind::Ctor(def::CtorOf::Variant, _), ctor_def_id) => {
232-
let variant_def_id = self.cx.tcx.parent(ctor_def_id);
233-
variant_def_id.and_then(|def_id| self.cx.tcx.parent(def_id))
234-
},
235-
_ => None,
236-
};
226+
if !path.segments.iter().any(|p| p.ident.span.is_dummy()) {
227+
if path.segments.len() >= 2 {
228+
let last_but_one = &path.segments[path.segments.len() - 2];
229+
if last_but_one.ident.name != kw::SelfUpper {
230+
let enum_def_id = match path.res {
231+
Res::Def(DefKind::Variant, variant_def_id) => self.cx.tcx.parent(variant_def_id),
232+
Res::Def(DefKind::Ctor(def::CtorOf::Variant, _), ctor_def_id) => {
233+
let variant_def_id = self.cx.tcx.parent(ctor_def_id);
234+
variant_def_id.and_then(|def_id| self.cx.tcx.parent(def_id))
235+
},
236+
_ => None,
237+
};
237238

238-
if self.item_path.res.opt_def_id() == enum_def_id {
239-
span_use_self_lint(self.cx, path, Some(last_but_one));
239+
if self.item_path.res.opt_def_id() == enum_def_id {
240+
span_use_self_lint(self.cx, path, Some(last_but_one));
241+
}
240242
}
241243
}
242-
}
243244

244-
if path.segments.last().expect(SEGMENTS_MSG).ident.name != kw::SelfUpper {
245-
if self.item_path.res == path.res {
246-
span_use_self_lint(self.cx, path, None);
247-
} else if let Res::Def(DefKind::Ctor(def::CtorOf::Struct, _), ctor_def_id) = path.res {
248-
if self.item_path.res.opt_def_id() == self.cx.tcx.parent(ctor_def_id) {
245+
if path.segments.last().expect(SEGMENTS_MSG).ident.name != kw::SelfUpper {
246+
if self.item_path.res == path.res {
249247
span_use_self_lint(self.cx, path, None);
248+
} else if let Res::Def(DefKind::Ctor(def::CtorOf::Struct, _), ctor_def_id) = path.res {
249+
if self.item_path.res.opt_def_id() == self.cx.tcx.parent(ctor_def_id) {
250+
span_use_self_lint(self.cx, path, None);
251+
}
250252
}
251253
}
252254
}

tests/ui/use_self.fixed

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// compile-flags: --edition 2018
23

34
#![warn(clippy::use_self)]
45
#![allow(dead_code)]
@@ -333,16 +334,31 @@ mod issue3567 {
333334
}
334335
}
335336

336-
// Test with paths in ranges
337-
mod paths_in_ranges {
337+
mod paths_created_by_lowering {
338+
use std::ops::Range;
339+
338340
struct S {}
339341

340342
impl S {
341343
const A: usize = 0;
342344
const B: usize = 1;
343345

346+
async fn g() -> Self {
347+
Self {}
348+
}
349+
344350
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
345351
&p[Self::A..Self::B]
346352
}
347353
}
354+
355+
trait T {
356+
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
357+
}
358+
359+
impl T for Range<u8> {
360+
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
361+
&p[0..1]
362+
}
363+
}
348364
}

tests/ui/use_self.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// compile-flags: --edition 2018
23

34
#![warn(clippy::use_self)]
45
#![allow(dead_code)]
@@ -333,16 +334,31 @@ mod issue3567 {
333334
}
334335
}
335336

336-
// Test with paths in ranges
337-
mod paths_in_ranges {
337+
mod paths_created_by_lowering {
338+
use std::ops::Range;
339+
338340
struct S {}
339341

340342
impl S {
341343
const A: usize = 0;
342344
const B: usize = 1;
343345

346+
async fn g() -> S {
347+
S {}
348+
}
349+
344350
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
345351
&p[S::A..S::B]
346352
}
347353
}
354+
355+
trait T {
356+
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
357+
}
358+
359+
impl T for Range<u8> {
360+
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
361+
&p[0..1]
362+
}
363+
}
348364
}

0 commit comments

Comments
 (0)