]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check_unused.rs
Rollup merge of #76060 - pickfire:patch-12, r=jyn514
[rust.git] / compiler / rustc_typeck / src / check_unused.rs
1 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2 use rustc_errors::Applicability;
3 use rustc_hir as hir;
4 use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
5 use rustc_hir::itemlikevisit::ItemLikeVisitor;
6 use rustc_middle::ty::TyCtxt;
7 use rustc_session::lint;
8 use rustc_span::{Span, Symbol};
9
10 pub fn check_crate(tcx: TyCtxt<'_>) {
11     let mut used_trait_imports = FxHashSet::default();
12     for &body_id in tcx.hir().krate().bodies.keys() {
13         let item_def_id = tcx.hir().body_owner_def_id(body_id);
14         let imports = tcx.used_trait_imports(item_def_id);
15         debug!("GatherVisitor: item_def_id={:?} with imports {:#?}", item_def_id, imports);
16         used_trait_imports.extend(imports.iter());
17     }
18
19     let mut visitor = CheckVisitor { tcx, used_trait_imports };
20     tcx.hir().krate().visit_all_item_likes(&mut visitor);
21
22     unused_crates_lint(tcx);
23 }
24
25 impl ItemLikeVisitor<'v> for CheckVisitor<'tcx> {
26     fn visit_item(&mut self, item: &hir::Item<'_>) {
27         if item.vis.node.is_pub() || item.span.is_dummy() {
28             return;
29         }
30         if let hir::ItemKind::Use(ref path, _) = item.kind {
31             self.check_import(item.hir_id, path.span);
32         }
33     }
34
35     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
36
37     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
38 }
39
40 struct CheckVisitor<'tcx> {
41     tcx: TyCtxt<'tcx>,
42     used_trait_imports: FxHashSet<LocalDefId>,
43 }
44
45 impl CheckVisitor<'tcx> {
46     fn check_import(&self, id: hir::HirId, span: Span) {
47         let def_id = self.tcx.hir().local_def_id(id);
48         if !self.tcx.maybe_unused_trait_import(def_id) {
49             return;
50         }
51
52         if self.used_trait_imports.contains(&def_id) {
53             return;
54         }
55
56         self.tcx.struct_span_lint_hir(lint::builtin::UNUSED_IMPORTS, id, span, |lint| {
57             let msg = if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
58                 format!("unused import: `{}`", snippet)
59             } else {
60                 "unused import".to_owned()
61             };
62             lint.build(&msg).emit();
63         });
64     }
65 }
66
67 fn unused_crates_lint(tcx: TyCtxt<'_>) {
68     let lint = lint::builtin::UNUSED_EXTERN_CRATES;
69
70     // Collect first the crates that are completely unused.  These we
71     // can always suggest removing (no matter which edition we are
72     // in).
73     let unused_extern_crates: FxHashMap<LocalDefId, Span> = tcx
74         .maybe_unused_extern_crates(LOCAL_CRATE)
75         .iter()
76         .filter(|&&(def_id, _)| {
77             // The `def_id` here actually was calculated during resolution (at least
78             // at the time of this writing) and is being shipped to us via a side
79             // channel of the tcx. There may have been extra expansion phases,
80             // however, which ended up removing the `def_id` *after* expansion such
81             // as the `ReplaceBodyWithLoop` pass (which is a bit of a hack, but hey)
82             //
83             // As a result we need to verify that `def_id` is indeed still valid for
84             // our AST and actually present in the HIR map. If it's not there then
85             // there's safely nothing to warn about, and otherwise we carry on with
86             // our execution.
87             //
88             // Note that if we carry through to the `extern_mod_stmt_cnum` query
89             // below it'll cause a panic because `def_id` is actually bogus at this
90             // point in time otherwise.
91             if tcx.hir().find(tcx.hir().local_def_id_to_hir_id(def_id)).is_none() {
92                 return false;
93             }
94             true
95         })
96         .filter(|&&(def_id, _)| {
97             tcx.extern_mod_stmt_cnum(def_id).map_or(true, |cnum| {
98                 !tcx.is_compiler_builtins(cnum)
99                     && !tcx.is_panic_runtime(cnum)
100                     && !tcx.has_global_allocator(cnum)
101                     && !tcx.has_panic_handler(cnum)
102             })
103         })
104         .cloned()
105         .collect();
106
107     // Collect all the extern crates (in a reliable order).
108     let mut crates_to_lint = vec![];
109     tcx.hir().krate().visit_all_item_likes(&mut CollectExternCrateVisitor {
110         tcx,
111         crates_to_lint: &mut crates_to_lint,
112     });
113
114     for extern_crate in &crates_to_lint {
115         let def_id = extern_crate.def_id.expect_local();
116         let id = tcx.hir().local_def_id_to_hir_id(def_id);
117         let item = tcx.hir().expect_item(id);
118
119         // If the crate is fully unused, we suggest removing it altogether.
120         // We do this in any edition.
121         if extern_crate.warn_if_unused {
122             if let Some(&span) = unused_extern_crates.get(&def_id) {
123                 tcx.struct_span_lint_hir(lint, id, span, |lint| {
124                     // Removal suggestion span needs to include attributes (Issue #54400)
125                     let span_with_attrs = tcx
126                         .get_attrs(extern_crate.def_id)
127                         .iter()
128                         .map(|attr| attr.span)
129                         .fold(span, |acc, attr_span| acc.to(attr_span));
130
131                     lint.build("unused extern crate")
132                         .span_suggestion_short(
133                             span_with_attrs,
134                             "remove it",
135                             String::new(),
136                             Applicability::MachineApplicable,
137                         )
138                         .emit();
139                 });
140                 continue;
141             }
142         }
143
144         // If we are not in Rust 2018 edition, then we don't make any further
145         // suggestions.
146         if !tcx.sess.rust_2018() {
147             continue;
148         }
149
150         // If the extern crate isn't in the extern prelude,
151         // there is no way it can be written as an `use`.
152         let orig_name = extern_crate.orig_name.unwrap_or(item.ident.name);
153         if !tcx.extern_prelude.get(&orig_name).map_or(false, |from_item| !from_item) {
154             continue;
155         }
156
157         // If the extern crate is renamed, then we cannot suggest replacing it with a use as this
158         // would not insert the new name into the prelude, where other imports in the crate may be
159         // expecting it.
160         if extern_crate.orig_name.is_some() {
161             continue;
162         }
163
164         // If the extern crate has any attributes, they may have funky
165         // semantics we can't faithfully represent using `use` (most
166         // notably `#[macro_use]`). Ignore it.
167         if !tcx.get_attrs(extern_crate.def_id).is_empty() {
168             continue;
169         }
170         tcx.struct_span_lint_hir(lint, id, extern_crate.span, |lint| {
171             // Otherwise, we can convert it into a `use` of some kind.
172             let base_replacement = match extern_crate.orig_name {
173                 Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
174                 None => format!("use {};", item.ident.name),
175             };
176             let vis = tcx.sess.source_map().span_to_snippet(item.vis.span).unwrap_or_default();
177             let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
178             lint.build("`extern crate` is not idiomatic in the new edition")
179                 .span_suggestion_short(
180                     extern_crate.span,
181                     &format!("convert it to a `{}`", add_vis("use".to_string())),
182                     add_vis(base_replacement),
183                     Applicability::MachineApplicable,
184                 )
185                 .emit();
186         })
187     }
188 }
189
190 struct CollectExternCrateVisitor<'a, 'tcx> {
191     tcx: TyCtxt<'tcx>,
192     crates_to_lint: &'a mut Vec<ExternCrateToLint>,
193 }
194
195 struct ExternCrateToLint {
196     /// `DefId` of the extern crate
197     def_id: DefId,
198
199     /// span from the item
200     span: Span,
201
202     /// if `Some`, then this is renamed (`extern crate orig_name as
203     /// crate_name`), and -- perhaps surprisingly -- this stores the
204     /// *original* name (`item.name` will contain the new name)
205     orig_name: Option<Symbol>,
206
207     /// if `false`, the original name started with `_`, so we shouldn't lint
208     /// about it going unused (but we should still emit idiom lints).
209     warn_if_unused: bool,
210 }
211
212 impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> {
213     fn visit_item(&mut self, item: &hir::Item<'_>) {
214         if let hir::ItemKind::ExternCrate(orig_name) = item.kind {
215             let extern_crate_def_id = self.tcx.hir().local_def_id(item.hir_id);
216             self.crates_to_lint.push(ExternCrateToLint {
217                 def_id: extern_crate_def_id.to_def_id(),
218                 span: item.span,
219                 orig_name,
220                 warn_if_unused: !item.ident.as_str().starts_with('_'),
221             });
222         }
223     }
224
225     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
226
227     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
228 }