Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d30af5e

Browse files
authored
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.
2 parents c3202af + 2d3a9a5 commit d30af5e

File tree

20 files changed

+194
-60
lines changed

20 files changed

+194
-60
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2729,7 +2729,7 @@ pub enum UseTreeKind {
27292729
/// `use prefix` or `use prefix as rename`
27302730
Simple(Option<Ident>),
27312731
/// `use prefix::{...}`
2732-
Nested(ThinVec<(UseTree, NodeId)>),
2732+
Nested { items: ThinVec<(UseTree, NodeId)>, span: Span },
27332733
/// `use prefix::*`
27342734
Glob,
27352735
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ fn noop_visit_use_tree<T: MutVisitor>(use_tree: &mut UseTree, vis: &mut T) {
441441
vis.visit_path(prefix);
442442
match kind {
443443
UseTreeKind::Simple(rename) => visit_opt(rename, |rename| vis.visit_ident(rename)),
444-
UseTreeKind::Nested(items) => {
444+
UseTreeKind::Nested { items, .. } => {
445445
for (tree, id) in items {
446446
vis.visit_use_tree(tree);
447447
vis.visit_id(id);

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ pub fn walk_use_tree<'a, V: Visitor<'a>>(
517517
visit_opt!(visitor, visit_ident, rename);
518518
}
519519
UseTreeKind::Glob => {}
520-
UseTreeKind::Nested(ref use_trees) => {
521-
for &(ref nested_tree, nested_id) in use_trees {
520+
UseTreeKind::Nested { ref items, .. } => {
521+
for &(ref nested_tree, nested_id) in items {
522522
try_visit!(visitor.visit_use_tree(nested_tree, nested_id, true));
523523
}
524524
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
135135

136136
fn lower_item_id_use_tree(&mut self, tree: &UseTree, vec: &mut SmallVec<[hir::ItemId; 1]>) {
137137
match &tree.kind {
138-
UseTreeKind::Nested(nested_vec) => {
139-
for &(ref nested, id) in nested_vec {
138+
UseTreeKind::Nested { items, .. } => {
139+
for &(ref nested, id) in items {
140140
vec.push(hir::ItemId {
141141
owner_id: hir::OwnerId { def_id: self.local_def_id(id) },
142142
});
@@ -518,7 +518,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
518518
let path = self.lower_use_path(res, &path, ParamMode::Explicit);
519519
hir::ItemKind::Use(path, hir::UseKind::Glob)
520520
}
521-
UseTreeKind::Nested(ref trees) => {
521+
UseTreeKind::Nested { items: ref trees, .. } => {
522522
// Nested imports are desugared into simple imports.
523523
// So, if we start with
524524
//

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ impl<'a> State<'a> {
715715
}
716716
self.word("*");
717717
}
718-
ast::UseTreeKind::Nested(items) => {
718+
ast::UseTreeKind::Nested { items, .. } => {
719719
if !tree.prefix.segments.is_empty() {
720720
self.print_path(&tree.prefix, false, 0);
721721
self.word("::");
@@ -734,7 +734,7 @@ impl<'a> State<'a> {
734734
self.print_use_tree(&use_tree.0);
735735
if !is_last {
736736
self.word(",");
737-
if let ast::UseTreeKind::Nested(_) = use_tree.0.kind {
737+
if let ast::UseTreeKind::Nested { .. } = use_tree.0.kind {
738738
self.hardbreak();
739739
} else {
740740
self.space();

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,13 @@ impl<'cx, 'a> Context<'cx, 'a> {
120120
thin_vec![self.cx.attr_nested_word(sym::allow, sym::unused_imports, self.span)],
121121
ItemKind::Use(UseTree {
122122
prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])),
123-
kind: UseTreeKind::Nested(thin_vec![
124-
nested_tree(self, sym::TryCaptureGeneric),
125-
nested_tree(self, sym::TryCapturePrintable),
126-
]),
123+
kind: UseTreeKind::Nested {
124+
items: thin_vec![
125+
nested_tree(self, sym::TryCaptureGeneric),
126+
nested_tree(self, sym::TryCapturePrintable),
127+
],
128+
span: self.span,
129+
},
127130
span: self.span,
128131
}),
129132
),

compiler/rustc_expand/src/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,8 +1194,8 @@ impl InvocationCollectorNode for P<ast::Item> {
11941194
match &ut.kind {
11951195
ast::UseTreeKind::Glob => {}
11961196
ast::UseTreeKind::Simple(_) => idents.push(ut.ident()),
1197-
ast::UseTreeKind::Nested(nested) => {
1198-
for (ut, _) in nested {
1197+
ast::UseTreeKind::Nested { items, .. } => {
1198+
for (ut, _) in items {
11991199
collect_use_tree_leaves(ut, idents);
12001200
}
12011201
}

compiler/rustc_lint/src/unused.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ declare_lint_pass!(UnusedImportBraces => [UNUSED_IMPORT_BRACES]);
15011501

15021502
impl UnusedImportBraces {
15031503
fn check_use_tree(&self, cx: &EarlyContext<'_>, use_tree: &ast::UseTree, item: &ast::Item) {
1504-
if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
1504+
if let ast::UseTreeKind::Nested { ref items, .. } = use_tree.kind {
15051505
// Recursively check nested UseTrees
15061506
for (tree, _) in items {
15071507
self.check_use_tree(cx, tree, item);
@@ -1522,7 +1522,7 @@ impl UnusedImportBraces {
15221522
rename.unwrap_or(orig_ident).name
15231523
}
15241524
ast::UseTreeKind::Glob => Symbol::intern("*"),
1525-
ast::UseTreeKind::Nested(_) => return,
1525+
ast::UseTreeKind::Nested { .. } => return,
15261526
};
15271527

15281528
cx.emit_span_lint(

compiler/rustc_parse/src/parser/item.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<'a> Parser<'a> {
336336
UseTreeKind::Glob => {
337337
e.note("the wildcard token must be last on the path");
338338
}
339-
UseTreeKind::Nested(..) => {
339+
UseTreeKind::Nested { .. } => {
340340
e.note("glob-like brace syntax must be last on the path");
341341
}
342342
_ => (),
@@ -1056,7 +1056,11 @@ impl<'a> Parser<'a> {
10561056
Ok(if self.eat(&token::BinOp(token::Star)) {
10571057
UseTreeKind::Glob
10581058
} else {
1059-
UseTreeKind::Nested(self.parse_use_tree_list()?)
1059+
let lo = self.token.span;
1060+
UseTreeKind::Nested {
1061+
items: self.parse_use_tree_list()?,
1062+
span: lo.to(self.prev_token.span),
1063+
}
10601064
})
10611065
}
10621066

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
560560

561561
self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis);
562562
}
563-
ast::UseTreeKind::Nested(ref items) => {
563+
ast::UseTreeKind::Nested { ref items, .. } => {
564564
// Ensure there is at most one `self` in the list
565565
let self_spans = items
566566
.iter()

0 commit comments

Comments
 (0)