|
| 1 | +use crate::errors::{ExternCrateNotIdiomatic, UnusedExternCrate}; |
1 | 2 | use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2 |
| -use rustc_errors::Applicability; |
3 | 3 | use rustc_hir as hir;
|
4 | 4 | use rustc_hir::def::DefKind;
|
5 | 5 | use rustc_hir::def_id::{DefId, LocalDefId};
|
@@ -108,25 +108,16 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
|
108 | 108 | // We do this in any edition.
|
109 | 109 | if extern_crate.warn_if_unused {
|
110 | 110 | if let Some(&span) = unused_extern_crates.get(&def_id) {
|
| 111 | + // Removal suggestion span needs to include attributes (Issue #54400) |
111 | 112 | let id = tcx.hir().local_def_id_to_hir_id(def_id);
|
112 |
| - tcx.struct_span_lint_hir(lint, id, span, |lint| { |
113 |
| - // Removal suggestion span needs to include attributes (Issue #54400) |
114 |
| - let span_with_attrs = tcx |
115 |
| - .hir() |
116 |
| - .attrs(id) |
117 |
| - .iter() |
118 |
| - .map(|attr| attr.span) |
119 |
| - .fold(span, |acc, attr_span| acc.to(attr_span)); |
120 |
| - |
121 |
| - lint.build("unused extern crate") |
122 |
| - .span_suggestion_short( |
123 |
| - span_with_attrs, |
124 |
| - "remove it", |
125 |
| - "", |
126 |
| - Applicability::MachineApplicable, |
127 |
| - ) |
128 |
| - .emit(); |
129 |
| - }); |
| 113 | + let span_with_attrs = tcx |
| 114 | + .hir() |
| 115 | + .attrs(id) |
| 116 | + .iter() |
| 117 | + .map(|attr| attr.span) |
| 118 | + .fold(span, |acc, attr_span| acc.to(attr_span)); |
| 119 | + |
| 120 | + tcx.emit_spanned_lint(lint, id, span, UnusedExternCrate { span: span_with_attrs }); |
130 | 121 | continue;
|
131 | 122 | }
|
132 | 123 | }
|
@@ -158,23 +149,23 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
|
158 | 149 | if !tcx.hir().attrs(id).is_empty() {
|
159 | 150 | continue;
|
160 | 151 | }
|
161 |
| - tcx.struct_span_lint_hir(lint, id, extern_crate.span, |lint| { |
162 |
| - // Otherwise, we can convert it into a `use` of some kind. |
163 |
| - let base_replacement = match extern_crate.orig_name { |
164 |
| - Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name), |
165 |
| - None => format!("use {};", item.ident.name), |
166 |
| - }; |
167 |
| - let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default(); |
168 |
| - let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) }; |
169 |
| - lint.build("`extern crate` is not idiomatic in the new edition") |
170 |
| - .span_suggestion_short( |
171 |
| - extern_crate.span, |
172 |
| - &format!("convert it to a `{}`", add_vis("use".to_string())), |
173 |
| - add_vis(base_replacement), |
174 |
| - Applicability::MachineApplicable, |
175 |
| - ) |
176 |
| - .emit(); |
177 |
| - }) |
| 152 | + |
| 153 | + let base_replacement = match extern_crate.orig_name { |
| 154 | + Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name), |
| 155 | + None => format!("use {};", item.ident.name), |
| 156 | + }; |
| 157 | + let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default(); |
| 158 | + let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) }; |
| 159 | + tcx.emit_spanned_lint( |
| 160 | + lint, |
| 161 | + id, |
| 162 | + extern_crate.span, |
| 163 | + ExternCrateNotIdiomatic { |
| 164 | + span: extern_crate.span, |
| 165 | + msg_code: add_vis("use".to_string()), |
| 166 | + suggestion_code: add_vis(base_replacement), |
| 167 | + }, |
| 168 | + ); |
178 | 169 | }
|
179 | 170 | }
|
180 | 171 |
|
|
0 commit comments