]> git.lizzy.rs Git - rust.git/commitdiff
rustc_hir: Change representation of import paths to support multiple resolutions
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Fri, 25 Nov 2022 14:39:38 +0000 (17:39 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Thu, 1 Dec 2022 15:51:05 +0000 (18:51 +0300)
clippy_lints/src/disallowed_types.rs
clippy_lints/src/macro_use.rs
clippy_lints/src/missing_enforced_import_rename.rs
clippy_lints/src/redundant_pub_crate.rs
clippy_lints/src/wildcard_imports.rs

index aee3d8c4f08527234f622a6ea81dc6f551a9920f..1f56d0118a404b50d88b938620c01b70fcc11212 100644 (file)
@@ -106,7 +106,9 @@ fn check_crate(&mut self, cx: &LateContext<'_>) {
 
     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
         if let ItemKind::Use(path, UseKind::Single) = &item.kind {
-            self.check_res_emit(cx, &path.res, item.span);
+            for res in &path.res {
+                self.check_res_emit(cx, res, item.span);
+            }
         }
     }
 
index 594f6af76b3d874f339203d06ce5c066e9bab9e8..e2e6a87a30151472b7bc93e250f556275641f441 100644 (file)
@@ -94,7 +94,10 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
             let hir_id = item.hir_id();
             let attrs = cx.tcx.hir().attrs(hir_id);
             if let Some(mac_attr) = attrs.iter().find(|attr| attr.has_name(sym::macro_use));
-            if let Res::Def(DefKind::Mod, id) = path.res;
+            if let Some(id) = path.res.iter().find_map(|res| match res {
+                Res::Def(DefKind::Mod, id) => Some(id),
+                _ => None,
+            });
             if !id.is_local();
             then {
                 for kid in cx.tcx.module_children(id).iter() {
index 4712846939e6a0fcaae2a242895f7d9709988314..773174679dbdce618b244bfdfe9f5516d2eaaa0f 100644 (file)
@@ -66,35 +66,38 @@ fn check_crate(&mut self, cx: &LateContext<'_>) {
     }
 
     fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
-        if_chain! {
-            if let ItemKind::Use(path, UseKind::Single) = &item.kind;
-            if let Res::Def(_, id) = path.res;
-            if let Some(name) = self.renames.get(&id);
-            // Remove semicolon since it is not present for nested imports
-            let span_without_semi = cx.sess().source_map().span_until_char(item.span, ';');
-            if let Some(snip) = snippet_opt(cx, span_without_semi);
-            if let Some(import) = match snip.split_once(" as ") {
-                None => Some(snip.as_str()),
-                Some((import, rename)) => {
-                    if rename.trim() == name.as_str() {
-                        None
-                    } else {
-                        Some(import.trim())
+        if let ItemKind::Use(path, UseKind::Single) = &item.kind {
+            for &res in &path.res {
+                if_chain! {
+                    if let Res::Def(_, id) = res;
+                    if let Some(name) = self.renames.get(&id);
+                    // Remove semicolon since it is not present for nested imports
+                    let span_without_semi = cx.sess().source_map().span_until_char(item.span, ';');
+                    if let Some(snip) = snippet_opt(cx, span_without_semi);
+                    if let Some(import) = match snip.split_once(" as ") {
+                        None => Some(snip.as_str()),
+                        Some((import, rename)) => {
+                            if rename.trim() == name.as_str() {
+                                None
+                            } else {
+                                Some(import.trim())
+                            }
+                        },
+                    };
+                    then {
+                        span_lint_and_sugg(
+                            cx,
+                            MISSING_ENFORCED_IMPORT_RENAMES,
+                            span_without_semi,
+                            "this import should be renamed",
+                            "try",
+                            format!(
+                                "{import} as {name}",
+                            ),
+                            Applicability::MachineApplicable,
+                        );
                     }
-                },
-            };
-            then {
-                span_lint_and_sugg(
-                    cx,
-                    MISSING_ENFORCED_IMPORT_RENAMES,
-                    span_without_semi,
-                    "this import should be renamed",
-                    "try",
-                    format!(
-                        "{import} as {name}",
-                    ),
-                    Applicability::MachineApplicable,
-                );
+                }
             }
         }
     }
index 833dc4913b46922a215653311abb91bb6da5c745..d612d249c2f000ab3c869952f1806ff491ac2450 100644 (file)
@@ -84,7 +84,7 @@ fn check_item_post(&mut self, _cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
 
 fn is_not_macro_export<'tcx>(item: &'tcx Item<'tcx>) -> bool {
     if let ItemKind::Use(path, _) = item.kind {
-        if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = path.res {
+        if path.res.iter().all(|res| matches!(res, Res::Def(DefKind::Macro(MacroKind::Bang), _))) {
             return false;
         }
     } else if let ItemKind::Macro(..) = item.kind {
index be98344470b9c5dbc2a6635b9657c820c201a47d..e4d1ee195c4dfb10aed5eed874f7de6fe280d84d 100644 (file)
@@ -176,7 +176,8 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
                     format!("{import_source_snippet}::{imports_string}")
                 };
 
-                let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res {
+                // Glob imports always have a single resolution.
+                let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res[0] {
                     (ENUM_GLOB_USE, "usage of wildcard import for enum variants")
                 } else {
                     (WILDCARD_IMPORTS, "usage of wildcard import")