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

Remove braces when fixing a nested use tree into a single item #123344

Merged
merged 5 commits into from
May 8, 2024

Conversation

pietroalbini
Copy link
Member

@pietroalbini pietroalbini commented Apr 1, 2024

Back in 2019 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.

@rustbot
Copy link
Collaborator

rustbot commented Apr 1, 2024

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 1, 2024
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Apr 1, 2024

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Apr 2, 2024

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@pietroalbini pietroalbini added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 2, 2024
@pietroalbini
Copy link
Member Author

All tests pass, this is ready for review.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 14, 2024
@@ -2703,7 +2703,7 @@ pub enum UseTreeKind {
/// `use prefix` or `use prefix as rename`
Simple(Option<Ident>),
/// `use prefix::{...}`
Nested(ThinVec<(UseTree, NodeId)>),
Nested { items: ThinVec<(UseTree, NodeId)>, span: Span },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could use a comment explaining what the span is for (the parentheses, I was told)

@Noratrieb
Copy link
Member

@pietroalbini told me he does feel like adding the comment later so I will approve this now. if adding the comment never happens, that's sad but ok too.

@bors r+
PXL_20240508_163955194

@bors
Copy link
Contributor

bors commented May 8, 2024

📌 Commit 2d3a9a5 has been approved by Nilstrieb

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 8, 2024
}

let mut unused_spans = Vec::new();
let mut to_remove = Vec::new();
let mut all_nested_unused = true;
let mut used_childs = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we agreed to not talk about this one

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 8, 2024
…Nilstrieb

Remove braces when fixing a nested use tree into a single item

[Back in 2019](rust-lang#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.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 8, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#123344 (Remove braces when fixing a nested use tree into a single item)
 - rust-lang#124587 (Generic `NonZero` post-stabilization changes.)
 - rust-lang#124775 (crashes: add lastest batch of crash tests)
 - rust-lang#124869 (Make sure we don't deny macro vars w keyword names)
 - rust-lang#124876 (Simplify `use crate::rustc_foo::bar` occurrences.)
 - rust-lang#124892 (Update cc crate to v1.0.97)
 - rust-lang#124903 (Ignore empty RUSTC_WRAPPER in bootstrap)
 - rust-lang#124909 (Reapply the part of rust-lang#124548 that bors forgot)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit d30af5e into rust-lang:master May 8, 2024
11 checks passed
@rustbot rustbot added this to the 1.80.0 milestone May 8, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 8, 2024
Rollup merge of rust-lang#123344 - pietroalbini:pa-unused-imports, r=Nilstrieb

Remove braces when fixing a nested use tree into a single item

[Back in 2019](rust-lang#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.
@pietroalbini pietroalbini deleted the pa-unused-imports branch May 9, 2024 12:00
flip1995 pushed a commit to flip1995/rust that referenced this pull request May 17, 2024
…Nilstrieb

Remove braces when fixing a nested use tree into a single item

[Back in 2019](rust-lang#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.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 19, 2024
…s, r=Nilstrieb

Followup fixes from rust-lang#123344

`@Nilstrieb` doesn't deserve [to be sad](rust-lang#123344 (comment)), so this PR addresses the two pieces of feedback from that PR.

r? `@Nilstrieb`
bors added a commit to rust-lang-ci/rust that referenced this pull request May 19, 2024
…llaumeGomez

Rollup of 3 pull requests

Successful merges:

 - rust-lang#125261 (crashes: add more)
 - rust-lang#125270 (Followup fixes from rust-lang#123344)
 - rust-lang#125275 (Migrate `run-make/rustdoc-scrape-examples-test` to new `rmake.rs`)

r? `@ghost`
`@rustbot` modify labels: rollup
compiler-errors added a commit to compiler-errors/rust that referenced this pull request May 19, 2024
…s, r=Nilstrieb

Followup fixes from rust-lang#123344

``@Nilstrieb`` doesn't deserve [to be sad](rust-lang#123344 (comment)), so this PR addresses the two pieces of feedback from that PR.

r? ``@Nilstrieb``
bors added a commit to rust-lang-ci/rust that referenced this pull request May 19, 2024
…mpiler-errors

Rollup of 7 pull requests

Successful merges:

 - rust-lang#123709 (Update documentation related to the recent cmd.exe fix)
 - rust-lang#124304 (revise the interpretation of ReadDir for HermitOS)
 - rust-lang#124708 (Actually use the `#[do_not_recommend]` attribute if present)
 - rust-lang#125252 (Add `#[inline]` to float `Debug` fallback used by `cfg(no_fp_fmt_parse)`)
 - rust-lang#125261 (crashes: add more)
 - rust-lang#125270 (Followup fixes from rust-lang#123344)
 - rust-lang#125275 (Migrate `run-make/rustdoc-scrape-examples-test` to new `rmake.rs`)

r? `@ghost`
`@rustbot` modify labels: rollup
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 19, 2024
Rollup merge of rust-lang#125270 - pietroalbini:pa-no-sad-contributors, r=Nilstrieb

Followup fixes from rust-lang#123344

``@Nilstrieb`` doesn't deserve [to be sad](rust-lang#123344 (comment)), so this PR addresses the two pieces of feedback from that PR.

r? ``@Nilstrieb``
bors added a commit to rust-lang-ci/rust that referenced this pull request May 19, 2024
…llaumeGomez

Rollup of 3 pull requests

Successful merges:

 - rust-lang#125261 (crashes: add more)
 - rust-lang#125270 (Followup fixes from rust-lang#123344)
 - rust-lang#125275 (Migrate `run-make/rustdoc-scrape-examples-test` to new `rmake.rs`)

r? `@ghost`
`@rustbot` modify labels: rollup
calebcartwright pushed a commit to calebcartwright/rust that referenced this pull request Jun 22, 2024
…Nilstrieb

Remove braces when fixing a nested use tree into a single item

[Back in 2019](rust-lang#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants