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

cargo clippy --fix leaves blank lines when removing entire use statements #121751

Closed
akonradi-signal opened this issue Feb 28, 2024 · 2 comments
Closed
Labels
C-bug Category: This is a bug.

Comments

@akonradi-signal
Copy link

Summary

cargo clippy --fix can remove entire use statements if all of the imported names are unused, but it leaves a blank line behind. With a large number of use lines, this can result in imports being fragmented into multiple blocks that won't ever be joined back together. If clippy removes an entire use statement, it should remove the trailing newline at the end, too.

Reproducer

I tried this code:

use std::any::Any;
use std::option::Option;
use std::result::Result;
use std::sync::Mutex;

pub fn use_some_imports() {
    let _ = Mutex::new(());
    let _ = Option::Some(32);
}

I expected to see this happen:
Running cargo clippy --fix should have removed the use lines for Any and Result and kept the block of imports together, like this:

use std::option::Option;
use std::sync::Mutex;

pub fn use_some_imports() {
    let _ = Mutex::new(());
    let _ = Option::Some(32);
}

Instead, this happened:
cargo clippy --fix left blank lines, splitting the block of imports:

use std::option::Option;

use std::sync::Mutex;

pub fn use_some_imports() {
    let _ = Mutex::new(());
    let _ = Option::Some(32);
}

Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

Additional Labels

No response

@akonradi-signal akonradi-signal added the C-bug Category: This is a bug. label Feb 28, 2024
@y21
Copy link
Member

y21 commented Feb 28, 2024

This looks like an issue with the unused_imports lint specifically, which is implemented in rustc.
It can also be repro'd with cargo fix, so clippy is not needed for this.

Moving to rust-lang/rust since this is unactionable in the clippy repo

@y21 y21 transferred this issue from rust-lang/rust-clippy Feb 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 28, 2024
@ehuss
Copy link
Contributor

ehuss commented Feb 28, 2024

Thanks for the report! I believe this is a duplicate of #120296, so closing in favor of that.

@ehuss ehuss closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2024
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

5 participants