Skip to content

Commit b0b3dc6

Browse files
committed
Auto merge of #4776 - mikerite:fix-4727, r=flip1995
Fix crash in `use-self` lint Fixes #4727 changelog: Fix crash in `use-self` lint
2 parents 37fa1e2 + cc6e27f commit b0b3dc6

File tree

6 files changed

+159
-56
lines changed

6 files changed

+159
-56
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 {
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
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub trait Trait {
2+
fn fun(par: &str) -> &str;
3+
}
4+
5+
impl Trait for str {
6+
fn fun(par: &str) -> &str {
7+
&par[0..1]
8+
}
9+
}

tests/ui/crashes/ice-4727.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-pass
2+
3+
#![warn(clippy::use_self)]
4+
5+
#[path = "auxiliary/ice-4727-aux.rs"]
6+
mod aux;
7+
8+
fn main() {}

tests/ui/use_self.fixed

Lines changed: 30 additions & 0 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)]
@@ -332,3 +333,32 @@ mod issue3567 {
332333
}
333334
}
334335
}
336+
337+
mod paths_created_by_lowering {
338+
use std::ops::Range;
339+
340+
struct S {}
341+
342+
impl S {
343+
const A: usize = 0;
344+
const B: usize = 1;
345+
346+
async fn g() -> Self {
347+
Self {}
348+
}
349+
350+
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
351+
&p[Self::A..Self::B]
352+
}
353+
}
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+
}
364+
}

tests/ui/use_self.rs

Lines changed: 30 additions & 0 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)]
@@ -332,3 +333,32 @@ mod issue3567 {
332333
}
333334
}
334335
}
336+
337+
mod paths_created_by_lowering {
338+
use std::ops::Range;
339+
340+
struct S {}
341+
342+
impl S {
343+
const A: usize = 0;
344+
const B: usize = 1;
345+
346+
async fn g() -> S {
347+
S {}
348+
}
349+
350+
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
351+
&p[S::A..S::B]
352+
}
353+
}
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+
}
364+
}

0 commit comments

Comments
 (0)