about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-05-08 23:33:24 +0200
committerGitHub <noreply@github.com>2024-05-08 23:33:24 +0200
commit2c7016776d9a2f84d73e7f028c81d897d9fe73b8 (patch)
tree0c3a9b3a1631d4bd679dfca16210d9c4a214f68c
parent27d320d1b6eaae25f8df5e2f6fe1ecc50c7da3eb (diff)
parentafa482ef2499a2501ce64c437912d197f8ab9f13 (diff)
downloadrust-2c7016776d9a2f84d73e7f028c81d897d9fe73b8.tar.gz
rust-2c7016776d9a2f84d73e7f028c81d897d9fe73b8.zip
Rollup merge of #123344 - pietroalbini:pa-unused-imports, r=Nilstrieb
Remove braces when fixing a nested use tree into a single item

[Back in 2019](https://github.com/rust-lang/rust/pull/56645) I added rustfix support for the `unused_imports` lint, to automatically remove them when running `cargo fix`. For the most part this worked great, but when removing all but one childs of a nested use tree it turned `use foo::{Unused, Used}` into `use foo::{Used}`. This is slightly annoying, because it then requires you to run `rustfmt` to get `use foo::Used`.

This PR automatically removes braces and the surrouding whitespace when all but one child of a nested use tree are unused. To get it done I had to add the span of the nested use tree to the AST, and refactor a bit the code I wrote back then.

A thing I noticed is, there doesn't seem to be any `//@ run-rustfix` test for fixing the `unused_imports` lint. I created a test in `tests/suggestions` (is that the right directory?) that for now tests just what I added in the PR. I can followup in a separate PR to add more tests for fixing `unused_lints`.

This PR is best reviewed commit-by-commit.
-rw-r--r--src/imports.rs4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/imports.rs b/src/imports.rs
index 09f6e752338..2da746295fc 100644
--- a/src/imports.rs
+++ b/src/imports.rs
@@ -458,7 +458,9 @@ impl UseTree {
                     version,
                 });
             }
-            UseTreeKind::Nested(ref list) => {
+            UseTreeKind::Nested {
+                items: ref list, ..
+            } => {
                 // Extract comments between nested use items.
                 // This needs to be done before sorting use items.
                 let items = itemize_list(