-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
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
Thanks for your report. The @rustbot transfer rust |
@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
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
ass::SomeStruct
whileSomeStruct
is actually an unused import. When clippy attempts to correct this, it generatesuse something::self as s;
, which is incorrect rustIssue occurs on both stable and nightly. Full clippy output and reproducer below
Reproducer
To reproduce, just
cargo init
and replacemain.rs
with the following code and runcargo clippy --fix
:I expected
use something::{self as s, SomeStruct};
to be reduced touse something as s;
Version
Additional Labels
No response
Clippy Error Log
clippy --fix output
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
flagThe 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 importself
|
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 defaultwarning:
rs-bug
(bin "rs-bug") generated 1 warning (runcargo 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:
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
flagThe 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 importself
|
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.25sThe text was updated successfully, but these errors were encountered: