Skip to content

Commit 334670e

Browse files
authored
fix: skip removing self in imports if they are stacked like use self::self; (#6573)
This prevents an idempotence issue where each run of rustfmt removed another `self` from the import path. For example, `use self::self::self;` -> `use self::self;` -> `use self;` -> `{import removed}`. Now `use self::self;`, `use self::self::self`, etc. are all left unchanged, and users will get a compiler error if they try to compile code with these invalid imports. This change does not impact normalizing `use foo::self;` -> `use foo;`.
1 parent 1443bba commit 334670e

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/imports.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,13 @@ impl UseTree {
566566

567567
// Normalise foo::self -> foo.
568568
if let UseSegmentKind::Slf(None) = last.kind {
569-
if !self.path.is_empty() {
570-
return self;
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+
}
571576
}
572577
}
573578

tests/source/issue_6558.rs

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

tests/target/issue_6558.rs

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

0 commit comments

Comments
 (0)