Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
richardsamuels opened this issue Mar 22, 2025 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@richardsamuels
Copy link

richardsamuels commented Mar 22, 2025

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

@richardsamuels richardsamuels added the C-bug Category: Clippy is not doing the correct thing label Mar 22, 2025
@samueltardieu samueltardieu added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Mar 22, 2025
@samueltardieu
Copy link
Contributor

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
Copy link
Collaborator

rustbot commented Mar 24, 2025

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
Copy link
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.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 25, 2025
…=jieyouxu

Fix autofix for `self` and `self as …` in `unused_imports` lint

This fixes two problems with the autofixes for the `unused_imports` lint:

- `use std::collections::{HashMap, self as coll};` would suggest, when `HashMap` is unused, the incorrect `use std::collections::self as coll;` which does not compile.
- `use std::borrow::{self, Cow};` would suggest, when `self` is unused, `use std::borrow::{Cow};`, which contains unnecessary brackets.

The first problem was reported in rust-lang/rust-clippy#14450, the second found while fixing the first one.

Fix rust-lang#133750
(thanks to `@richardsamuels` for spotting the duplicate)
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 25, 2025
Rollup merge of rust-lang#138886 - samueltardieu:push-xxkzmupznoky, r=jieyouxu

Fix autofix for `self` and `self as …` in `unused_imports` lint

This fixes two problems with the autofixes for the `unused_imports` lint:

- `use std::collections::{HashMap, self as coll};` would suggest, when `HashMap` is unused, the incorrect `use std::collections::self as coll;` which does not compile.
- `use std::borrow::{self, Cow};` would suggest, when `self` is unused, `use std::borrow::{Cow};`, which contains unnecessary brackets.

The first problem was reported in rust-lang/rust-clippy#14450, the second found while fixing the first one.

Fix rust-lang#133750
(thanks to `@richardsamuels` for spotting the duplicate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

3 participants