Skip to content

Commit a1d2280

Browse files
committed
Auto merge of rust-lang#104963 - petrochenkov:noaddids2, r=cjgillot
rustc_ast_lowering: Stop lowering imports into multiple items Lower them into a single item with multiple resolutions instead. This also allows to remove additional `NodId`s and `DefId`s related to those additional items.
2 parents d05e286 + b0d490e commit a1d2280

15 files changed

+57
-48
lines changed

clippy_lints/src/disallowed_types.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedTypes {
106106

107107
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
108108
if let ItemKind::Use(path, UseKind::Single) = &item.kind {
109-
self.check_res_emit(cx, &path.res, item.span);
109+
for res in &path.res {
110+
self.check_res_emit(cx, res, item.span);
111+
}
110112
}
111113
}
112114

clippy_lints/src/from_over_into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SelfFinder<'a, 'tcx> {
126126
self.cx.tcx.hir()
127127
}
128128

129-
fn visit_path(&mut self, path: &'tcx Path<'tcx>, _id: HirId) {
129+
fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) {
130130
for segment in path.segments {
131131
match segment.ident.name {
132132
kw::SelfLower => self.lower.push(segment.ident.span),

clippy_lints/src/macro_use.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
9494
let hir_id = item.hir_id();
9595
let attrs = cx.tcx.hir().attrs(hir_id);
9696
if let Some(mac_attr) = attrs.iter().find(|attr| attr.has_name(sym::macro_use));
97-
if let Res::Def(DefKind::Mod, id) = path.res;
97+
if let Some(id) = path.res.iter().find_map(|res| match res {
98+
Res::Def(DefKind::Mod, id) => Some(id),
99+
_ => None,
100+
});
98101
if !id.is_local();
99102
then {
100103
for kid in cx.tcx.module_children(id).iter() {

clippy_lints/src/methods/option_map_unwrap_or.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct UnwrapVisitor<'a, 'tcx> {
9797
impl<'a, 'tcx> Visitor<'tcx> for UnwrapVisitor<'a, 'tcx> {
9898
type NestedFilter = nested_filter::All;
9999

100-
fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
100+
fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) {
101101
self.identifiers.insert(ident(path));
102102
walk_path(self, path);
103103
}
@@ -116,7 +116,7 @@ struct MapExprVisitor<'a, 'tcx> {
116116
impl<'a, 'tcx> Visitor<'tcx> for MapExprVisitor<'a, 'tcx> {
117117
type NestedFilter = nested_filter::All;
118118

119-
fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
119+
fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) {
120120
if self.identifiers.contains(&ident(path)) {
121121
self.found_identifier = true;
122122
return;

clippy_lints/src/missing_enforced_import_rename.rs

+31-28
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,38 @@ impl LateLintPass<'_> for ImportRename {
6666
}
6767

6868
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
69-
if_chain! {
70-
if let ItemKind::Use(path, UseKind::Single) = &item.kind;
71-
if let Res::Def(_, id) = path.res;
72-
if let Some(name) = self.renames.get(&id);
73-
// Remove semicolon since it is not present for nested imports
74-
let span_without_semi = cx.sess().source_map().span_until_char(item.span, ';');
75-
if let Some(snip) = snippet_opt(cx, span_without_semi);
76-
if let Some(import) = match snip.split_once(" as ") {
77-
None => Some(snip.as_str()),
78-
Some((import, rename)) => {
79-
if rename.trim() == name.as_str() {
80-
None
81-
} else {
82-
Some(import.trim())
69+
if let ItemKind::Use(path, UseKind::Single) = &item.kind {
70+
for &res in &path.res {
71+
if_chain! {
72+
if let Res::Def(_, id) = res;
73+
if let Some(name) = self.renames.get(&id);
74+
// Remove semicolon since it is not present for nested imports
75+
let span_without_semi = cx.sess().source_map().span_until_char(item.span, ';');
76+
if let Some(snip) = snippet_opt(cx, span_without_semi);
77+
if let Some(import) = match snip.split_once(" as ") {
78+
None => Some(snip.as_str()),
79+
Some((import, rename)) => {
80+
if rename.trim() == name.as_str() {
81+
None
82+
} else {
83+
Some(import.trim())
84+
}
85+
},
86+
};
87+
then {
88+
span_lint_and_sugg(
89+
cx,
90+
MISSING_ENFORCED_IMPORT_RENAMES,
91+
span_without_semi,
92+
"this import should be renamed",
93+
"try",
94+
format!(
95+
"{import} as {name}",
96+
),
97+
Applicability::MachineApplicable,
98+
);
8399
}
84-
},
85-
};
86-
then {
87-
span_lint_and_sugg(
88-
cx,
89-
MISSING_ENFORCED_IMPORT_RENAMES,
90-
span_without_semi,
91-
"this import should be renamed",
92-
"try",
93-
format!(
94-
"{import} as {name}",
95-
),
96-
Applicability::MachineApplicable,
97-
);
100+
}
98101
}
99102
}
100103
}

clippy_lints/src/redundant_pub_crate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
8484

8585
fn is_not_macro_export<'tcx>(item: &'tcx Item<'tcx>) -> bool {
8686
if let ItemKind::Use(path, _) = item.kind {
87-
if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = path.res {
87+
if path.res.iter().all(|res| matches!(res, Res::Def(DefKind::Macro(MacroKind::Bang), _))) {
8888
return false;
8989
}
9090
} else if let ItemKind::Macro(..) = item.kind {

clippy_lints/src/single_component_path_imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl SingleComponentPathImports {
149149

150150
// keep track of `use some_module;` usages
151151
if segments.len() == 1 {
152-
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
152+
if let UseTreeKind::Simple(None) = use_tree.kind {
153153
let name = segments[0].ident.name;
154154
if !macros.contains(&name) {
155155
single_use_usages.push(SingleUse {
@@ -169,7 +169,7 @@ impl SingleComponentPathImports {
169169
for tree in trees {
170170
let segments = &tree.0.prefix.segments;
171171
if segments.len() == 1 {
172-
if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
172+
if let UseTreeKind::Simple(None) = tree.0.kind {
173173
let name = segments[0].ident.name;
174174
if !macros.contains(&name) {
175175
single_use_usages.push(SingleUse {

clippy_lints/src/unnecessary_self_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl EarlyLintPass for UnnecessarySelfImports {
5757
format!(
5858
"{}{};",
5959
last_segment.ident,
60-
if let UseTreeKind::Simple(Some(alias), ..) = self_tree.kind { format!(" as {alias}") } else { String::new() },
60+
if let UseTreeKind::Simple(Some(alias)) = self_tree.kind { format!(" as {alias}") } else { String::new() },
6161
),
6262
Applicability::MaybeIncorrect,
6363
);

clippy_lints/src/unsafe_removed_from_name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl EarlyLintPass for UnsafeNameRemoval {
3939

4040
fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) {
4141
match use_tree.kind {
42-
UseTreeKind::Simple(Some(new_name), ..) => {
42+
UseTreeKind::Simple(Some(new_name)) => {
4343
let old_name = use_tree
4444
.prefix
4545
.segments
@@ -48,7 +48,7 @@ fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) {
4848
.ident;
4949
unsafe_to_safe_check(old_name, new_name, cx, span);
5050
},
51-
UseTreeKind::Simple(None, ..) | UseTreeKind::Glob => {},
51+
UseTreeKind::Simple(None) | UseTreeKind::Glob => {},
5252
UseTreeKind::Nested(ref nested_use_tree) => {
5353
for (use_tree, _) in nested_use_tree {
5454
check_use_tree(use_tree, cx, span);

clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ struct LintCollector<'a, 'tcx> {
330330
impl<'a, 'tcx> Visitor<'tcx> for LintCollector<'a, 'tcx> {
331331
type NestedFilter = nested_filter::All;
332332

333-
fn visit_path(&mut self, path: &'tcx Path<'_>, _: HirId) {
333+
fn visit_path(&mut self, path: &Path<'_>, _: HirId) {
334334
if path.segments.len() == 1 {
335335
self.output.insert(path.segments[0].ident.name);
336336
}

clippy_lints/src/utils/internal_lints/metadata_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for ApplicabilityResolver<'a, 'hir> {
10191019
self.cx.tcx.hir()
10201020
}
10211021

1022-
fn visit_path(&mut self, path: &'hir hir::Path<'hir>, _id: hir::HirId) {
1022+
fn visit_path(&mut self, path: &hir::Path<'hir>, _id: hir::HirId) {
10231023
for (index, enum_value) in paths::APPLICABILITY_VALUES.iter().enumerate() {
10241024
if match_path(path, enum_value) {
10251025
self.add_new_index(index);

clippy_lints/src/wildcard_imports.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ impl LateLintPass<'_> for WildcardImports {
176176
format!("{import_source_snippet}::{imports_string}")
177177
};
178178

179-
let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res {
179+
// Glob imports always have a single resolution.
180+
let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res[0] {
180181
(ENUM_GLOB_USE, "usage of wildcard import for enum variants")
181182
} else {
182183
(WILDCARD_IMPORTS, "usage of wildcard import")

clippy_utils/src/ast_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
566566
use UseTreeKind::*;
567567
match (l, r) {
568568
(Glob, Glob) => true,
569-
(Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
569+
(Simple(l), Simple(r)) => both(l, r, |l, r| eq_id(*l, *r)),
570570
(Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
571571
_ => false,
572572
}

clippy_utils/src/usage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for BindingUsageFinder<'a, 'tcx> {
128128
}
129129
}
130130

131-
fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) {
131+
fn visit_path(&mut self, path: &hir::Path<'tcx>, _: hir::HirId) {
132132
if let hir::def::Res::Local(id) = path.res {
133133
if self.binding_ids.contains(&id) {
134134
self.usage_found = true;

tests/ui/macro_use_imports.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
2-
--> $DIR/macro_use_imports.rs:23:5
2+
--> $DIR/macro_use_imports.rs:25:5
33
|
44
LL | #[macro_use]
5-
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::foofoo, inner::try_err};`
5+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;`
66
|
77
= note: `-D clippy::macro-use-imports` implied by `-D warnings`
88

@@ -13,10 +13,10 @@ LL | #[macro_use]
1313
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest;`
1414

1515
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
16-
--> $DIR/macro_use_imports.rs:25:5
16+
--> $DIR/macro_use_imports.rs:23:5
1717
|
1818
LL | #[macro_use]
19-
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;`
19+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::foofoo, inner::try_err};`
2020

2121
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
2222
--> $DIR/macro_use_imports.rs:19:5

0 commit comments

Comments
 (0)