Skip to content

Commit a952708

Browse files
author
Michael Wright
committed
Fix crash in use-self lint
Fixes #4727
1 parent eae7b99 commit a952708

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

clippy_lints/src/use_self.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ 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 {
226+
if path.segments.len() >= 2 && !path.segments.iter().any(|p| p.ident.span.is_dummy()) {
227227
let last_but_one = &path.segments[path.segments.len() - 2];
228228
if last_but_one.ident.name != kw::SelfUpper {
229229
let enum_def_id = match path.res {

tests/ui/crashes/ice-4727-aux.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//ignore-test
2+
3+
// This file is used by the ice-4727 test but isn't itself a test.
4+
//
5+
pub trait Trait {
6+
fn fun(par: &str) -> &str;
7+
}
8+
9+
impl Trait for str {
10+
fn fun(par: &str) -> &str {
11+
&par[0..1]
12+
}
13+
}

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 = "ice-4727-aux.rs"]
6+
mod aux;
7+
8+
fn main() {}

tests/ui/use_self.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,17 @@ mod issue3567 {
332332
}
333333
}
334334
}
335+
336+
// Test with paths in ranges
337+
mod paths_in_ranges {
338+
struct S {}
339+
340+
impl S {
341+
const A: usize = 0;
342+
const B: usize = 1;
343+
344+
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
345+
&p[Self::A..Self::B]
346+
}
347+
}
348+
}

tests/ui/use_self.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,17 @@ mod issue3567 {
332332
}
333333
}
334334
}
335+
336+
// Test with paths in ranges
337+
mod paths_in_ranges {
338+
struct S {}
339+
340+
impl S {
341+
const A: usize = 0;
342+
const B: usize = 1;
343+
344+
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
345+
&p[S::A..S::B]
346+
}
347+
}
348+
}

tests/ui/use_self.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,17 @@ error: unnecessary structure name repetition
222222
LL | TestStruct::from_something()
223223
| ^^^^^^^^^^ help: use the applicable keyword: `Self`
224224

225-
error: aborting due to 36 previous errors
225+
error: unnecessary structure name repetition
226+
--> $DIR/use_self.rs:345:16
227+
|
228+
LL | &p[S::A..S::B]
229+
| ^ help: use the applicable keyword: `Self`
230+
231+
error: unnecessary structure name repetition
232+
--> $DIR/use_self.rs:345:22
233+
|
234+
LL | &p[S::A..S::B]
235+
| ^ help: use the applicable keyword: `Self`
236+
237+
error: aborting due to 38 previous errors
226238

0 commit comments

Comments
 (0)