|
| 1 | +use rustc_hir::def_id::LOCAL_CRATE; |
1 | 2 | use rustc_middle::bug;
|
2 | 3 | use rustc_session::Session;
|
3 | 4 | use rustc_session::config::ExpectedValues;
|
4 | 5 | use rustc_span::edit_distance::find_best_match_for_name;
|
5 | 6 | use rustc_span::symbol::Ident;
|
6 |
| -use rustc_span::{Span, Symbol, sym}; |
| 7 | +use rustc_span::{ExpnKind, Span, Symbol, sym}; |
7 | 8 |
|
8 | 9 | use crate::lints;
|
9 | 10 |
|
@@ -60,6 +61,35 @@ fn cargo_help_sub(
|
60 | 61 | }
|
61 | 62 | }
|
62 | 63 |
|
| 64 | +fn rustc_macro_help(span: Span) -> Option<lints::UnexpectedCfgRustcMacroHelp> { |
| 65 | + let oexpn = span.ctxt().outer_expn_data(); |
| 66 | + if let Some(def_id) = oexpn.macro_def_id |
| 67 | + && let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind |
| 68 | + && def_id.krate != LOCAL_CRATE |
| 69 | + { |
| 70 | + Some(lints::UnexpectedCfgRustcMacroHelp { macro_kind: macro_kind.descr(), macro_name }) |
| 71 | + } else { |
| 72 | + None |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn cargo_macro_help(span: Span) -> Option<lints::UnexpectedCfgCargoMacroHelp> { |
| 77 | + let oexpn = span.ctxt().outer_expn_data(); |
| 78 | + if let Some(def_id) = oexpn.macro_def_id |
| 79 | + && let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind |
| 80 | + && def_id.krate != LOCAL_CRATE |
| 81 | + { |
| 82 | + Some(lints::UnexpectedCfgCargoMacroHelp { |
| 83 | + macro_kind: macro_kind.descr(), |
| 84 | + macro_name, |
| 85 | + // FIXME: Get access to a `TyCtxt` from an `EarlyContext` |
| 86 | + // crate_name: cx.tcx.crate_name(def_id.krate), |
| 87 | + }) |
| 88 | + } else { |
| 89 | + None |
| 90 | + } |
| 91 | +} |
| 92 | + |
63 | 93 | pub(super) fn unexpected_cfg_name(
|
64 | 94 | sess: &Session,
|
65 | 95 | (name, name_span): (Symbol, Span),
|
@@ -186,16 +216,21 @@ pub(super) fn unexpected_cfg_name(
|
186 | 216 | };
|
187 | 217 |
|
188 | 218 | let invocation_help = if is_from_cargo {
|
189 |
| - let sub = if !is_feature_cfg && !is_from_external_macro { |
| 219 | + let help = if !is_feature_cfg && !is_from_external_macro { |
190 | 220 | Some(cargo_help_sub(sess, &inst))
|
191 | 221 | } else {
|
192 | 222 | None
|
193 | 223 | };
|
194 |
| - lints::unexpected_cfg_name::InvocationHelp::Cargo { sub } |
| 224 | + lints::unexpected_cfg_name::InvocationHelp::Cargo { |
| 225 | + help, |
| 226 | + macro_help: cargo_macro_help(name_span), |
| 227 | + } |
195 | 228 | } else {
|
196 |
| - lints::unexpected_cfg_name::InvocationHelp::Rustc(lints::UnexpectedCfgRustcHelp::new( |
197 |
| - &inst(EscapeQuotes::No), |
198 |
| - )) |
| 229 | + let help = lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No)); |
| 230 | + lints::unexpected_cfg_name::InvocationHelp::Rustc { |
| 231 | + help, |
| 232 | + macro_help: rustc_macro_help(name_span), |
| 233 | + } |
199 | 234 | };
|
200 | 235 |
|
201 | 236 | lints::UnexpectedCfgName { code_sugg, invocation_help, name }
|
@@ -302,14 +337,20 @@ pub(super) fn unexpected_cfg_value(
|
302 | 337 | } else {
|
303 | 338 | None
|
304 | 339 | };
|
305 |
| - lints::unexpected_cfg_value::InvocationHelp::Cargo(help) |
| 340 | + lints::unexpected_cfg_value::InvocationHelp::Cargo { |
| 341 | + help, |
| 342 | + macro_help: cargo_macro_help(name_span), |
| 343 | + } |
306 | 344 | } else {
|
307 | 345 | let help = if can_suggest_adding_value {
|
308 | 346 | Some(lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No)))
|
309 | 347 | } else {
|
310 | 348 | None
|
311 | 349 | };
|
312 |
| - lints::unexpected_cfg_value::InvocationHelp::Rustc(help) |
| 350 | + lints::unexpected_cfg_value::InvocationHelp::Rustc { |
| 351 | + help, |
| 352 | + macro_help: rustc_macro_help(name_span), |
| 353 | + } |
313 | 354 | };
|
314 | 355 |
|
315 | 356 | lints::UnexpectedCfgValue {
|
|
0 commit comments