-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
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
samueltardieu commentedon Mar 24, 2025
Thanks for your report. The
unused_imports
is a lint from the Rust compiler, not from Clippy.cargo check
will detect the issue, andcargo fix
will propose the same wrong fix.@rustbot transfer rust
rustbot commentedon 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.
self
andself as …
inunused_imports
lint rust-lang/rust#138886richardsamuels commentedon Mar 24, 2025
@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.
cargo fix
produces incorrect code when removing last unused dependency next toself as __
syntax rust-lang/rust#133750Rollup merge of rust-lang#138886 - samueltardieu:push-xxkzmupznoky, r…
Unrolled build for rust-lang#138886