Skip to content

cargo fix produces incorrect code when removing last unused dependency next to self as __ syntax #133750

Closed
@bragov4ik

Description

@bragov4ik

I encountered incorrect code being produced by cargo fix command.

Code

mod bakery_module {
    pub struct Cheesecake;

    #[derive(Debug)]
    pub struct Cupcake;
}

use bakery_module::{self as moms_spaghetti, Cheesecake};

fn main() {
    dbg!(moms_spaghetti::Cupcake);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=205e34f726214436ef250b91b4d025f2

Meta

stable rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1

nightly rustc --version --verbose:

rustc 1.85.0-nightly (5e1440ae5 2024-12-01)
binary: rustc
commit-hash: 5e1440ae514d98ddfcbf1607acb64d41e07ef616
commit-date: 2024-12-01
host: aarch64-apple-darwin
release: 1.85.0-nightly
LLVM version: 19.1.4

Error output

warning: `test-field` (bin "test-field") generated 2 warnings (run `cargo fix --bin "test-field"` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `test_field`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

<here I removed notice about making an issue into this repo>

The following errors were reported:
error[E0429]: `self` imports are only allowed within a { } list
 --> src/main.rs:8:18
  |
8 | use bakery_module::self as moms_spaghetti;
  |                  ^^^^^^
  |
help: consider importing the module directly
  |
8 - use bakery_module::self as moms_spaghetti;
8 + use bakery_module as moms_spaghetti;
  |
help: alternatively, use the multi-path `use` syntax to import `self`
  |
8 | use bakery_module::{self as moms_spaghetti};
  |                    +                      +

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0429`.
Original diagnostics will follow.

warning: `test-field` (bin "test-field" test) generated 2 warnings (2 duplicates)
Backtrace

No backtrace

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-dead_codeLint: dead_codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions