Skip to content

Clippy cannot --fix self imports with unused imports in the same import list #14450

@richardsamuels

Description

@richardsamuels

Summary

As a part of a larger project, I encountered an issue with clippy --fix

My Project had an import that looks like this:
use something::{self as s, SomeStruct};

My code accesses SomeStruct as s::SomeStruct while SomeStruct is actually an unused import. When clippy attempts to correct this, it generates use something::self as s;, which is incorrect rust

Issue occurs on both stable and nightly. Full clippy output and reproducer below

Reproducer

To reproduce, just cargo init and replace main.rs with the following code and run cargo clippy --fix:

mod something {
    pub struct SomeStruct {}

    impl SomeStruct {
        pub fn new() -> Self {
            SomeStruct {}
        }
        pub fn some_func(&self) {
            println!("Hello, world!");
        }
    }
}

use something::{self as s, SomeStruct};

fn main() {
    let s = s::SomeStruct::new();

    s.some_func();
}

I expected use something::{self as s, SomeStruct}; to be reduced to use something as s;

Version

Stable:
rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: aarch64-apple-darwin
release: 1.85.1
LLVM version: 19.1.7

Stable Clippy: clippy 0.1.85 (4eb161250e 2025-03-15)

Nightly:
rustc 1.87.0-nightly (be73c1f46 2025-03-21)
binary: rustc
commit-hash: be73c1f4617c97bce81b2694a767353300a75072
commit-date: 2025-03-21
host: aarch64-apple-darwin
release: 1.87.0-nightly
LLVM version: 20.1.1

Nightly clippy: clippy 0.1.87 (be73c1f461 2025-03-21)

Additional Labels

No response

Clippy Error Log

clippy --fix output
    Checking rs-bug v0.1.0 (/Users/rsamuels/code/rs-bug)
warning: failed to automatically apply fixes suggested by rustc to crate `rs_bug`

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

  • src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the --broken-code flag

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

error: aborting due to 1 previous error

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

warning: unused import: SomeStruct
--> src/main.rs:14:28
|
14 | use something::{self as s, SomeStruct};
| ^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

warning: rs-bug (bin "rs-bug") generated 1 warning (run cargo clippy --fix --bin "rs-bug" to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate rs_bug

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

  • src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the --broken-code flag

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

| + +

error: aborting due to 1 previous error

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

warning: rs-bug (bin "rs-bug" test) generated 1 warning (1 duplicate)
Finished dev profile [unoptimized + debuginfo] target(s) in 0.25s

Activity

added
C-bugCategory: Clippy is not doing the correct thing
on Mar 22, 2025
samueltardieu

samueltardieu commented on Mar 24, 2025

@samueltardieu
Member

Thanks for your report. The unused_imports is a lint from the Rust compiler, not from Clippy. cargo check will detect the issue, and cargo fix will propose the same wrong fix.

@rustbot transfer rust

rustbot

rustbot commented on Mar 24, 2025

@rustbot
Collaborator

Error: The feature transfer is not enabled in this repository.
To enable it add its section in the triagebot.toml in the root of the repository.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

richardsamuels

richardsamuels commented on Mar 24, 2025

@richardsamuels
Author

@samueltardieu Looks like there is an issue in rustbot's config for this repo.

Additionally, I found rust-lang/rust#133750, which is the same issue, so I'm closing this as duplicate.

added a commit that references this issue on Mar 25, 2025
946192b
added a commit that references this issue on Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @samueltardieu@richardsamuels@rustbot

        Issue actions

          Clippy cannot --fix self imports with unused imports in the same import list · Issue #14450 · rust-lang/rust-clippy