Skip to content

Commit 935a355

Browse files
committed
fix: #6558, only skip removing self if they are stacked like self::self
1 parent 3b3fc2f commit 935a355

File tree

7 files changed

+20
-12
lines changed

7 files changed

+20
-12
lines changed

src/imports.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,25 @@ impl UseTree {
557557
self.path = vec![];
558558
return self;
559559
}
560+
UseSegmentKind::Slf(None) if self.path.is_empty() && self.visibility.is_some() => {
561+
self.path = vec![];
562+
return self;
563+
}
560564
_ => (),
561565
}
562566

567+
// Normalise foo::self -> foo.
568+
if let UseSegmentKind::Slf(None) = last.kind {
569+
if let Some(second_last) = self.path.pop() {
570+
if matches!(second_last.kind, UseSegmentKind::Slf(_)) {
571+
self.path.push(second_last);
572+
} else {
573+
self.path.push(second_last);
574+
return self;
575+
}
576+
}
577+
}
578+
563579
// Normalise foo::self as bar -> foo as bar.
564580
if let UseSegmentKind::Slf(_) = last.kind {
565581
if let Some(UseSegment {

tests/source/imports/imports.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ pub use rustc_ast::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, Expr
2323

2424
use rustc_ast::some::{};
2525

26-
// compilation error that will (and should) be ignored by rustfmt #6558
2726
use self;
2827
use std::io::{self};
29-
// compilation error that will (and should) be ignored by rustfmt #6558
3028
use std::io::self;
3129

3230
mod Foo {
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::lexer;
22
use crate::lexer::tokens::TokenData;
33
use crate::lexer::{tokens::TokenData};
4-
// compilation error that will (and should) be ignored by rustfmt #6558
54
use crate::lexer::self;
65
use crate::lexer::{self};
76
use crate::lexer::{self, tokens::TokenData};

tests/source/issue_6558.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Removing `self` breaks idempotence, leaving it causes a compilation error
1+
// Removing `self::self` breaks idempotence, leaving it causes a compilation error
22
// which the user should be made aware of #6558
33
use self;
44
use self::self;

tests/target/imports/imports.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ use rustc_ast::{self};
2727
use Foo::{Bar, Baz};
2828
use {Bar /* comment */, /* Pre-comment! */ Foo};
2929

30-
// compilation error that will (and should) be ignored by rustfmt #6558
31-
use self;
30+
use std::io;
3231
use std::io::{self};
33-
// compilation error that will (and should) be ignored by rustfmt #6558
34-
use std::io::self;
3532

3633
mod Foo {
3734
pub use rustc_ast::ast::{
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::lexer;
2+
use crate::lexer;
23
use crate::lexer::tokens::TokenData;
34
use crate::lexer::tokens::TokenData;
4-
// compilation error that will (and should) be ignored by rustfmt #6558
5-
use crate::lexer::self;
65
use crate::lexer::{self};
76
use crate::lexer::{self, tokens::TokenData};

tests/target/issue_6558.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Removing `self` breaks idempotence, leaving it causes a compilation error
1+
// Removing `self::self` breaks idempotence, leaving it causes a compilation error
22
// which the user should be made aware of #6558
3-
use self;
43
use self::self;
54
use self::self::self;

0 commit comments

Comments
 (0)