use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
+use rustc_hir::def_id::DefId;
use rustc_hir::hir_id::CRATE_HIR_ID;
use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
use rustc_hir::{Crate, Expr, ExprKind, HirId, Item, MutTy, Mutability, Node, Path, StmtKind, Ty, TyKind};
#[derive(Default)]
pub struct InterningDefinedSymbol {
- // Maps the symbol value to the constant name.
- symbol_map: FxHashMap<u32, String>,
+ // Maps the symbol value to the constant DefId.
+ symbol_map: FxHashMap<u32, DefId>,
}
impl_lint_pass!(InterningDefinedSymbol => [INTERNING_DEFINED_SYMBOL]);
if let Ok(ConstValue::Scalar(value)) = cx.tcx.const_eval_poly(item_def_id);
if let Ok(value) = value.to_u32();
then {
- self.symbol_map.insert(value, item.ident.to_string());
+ self.symbol_map.insert(value, item_def_id);
}
}
}
if match_def_path(cx, *def_id, &paths::SYMBOL_INTERN);
if let Some(Constant::Str(arg)) = constant_simple(cx, cx.typeck_results(), arg);
let value = Symbol::intern(&arg).as_u32();
- if let Some(symbol_const) = self.symbol_map.get(&value);
+ if let Some(&def_id) = self.symbol_map.get(&value);
then {
span_lint_and_sugg(
cx,
is_expn_of(expr.span, "sym").unwrap_or(expr.span),
"interning a defined symbol",
"try",
- format!("rustc_span::symbol::sym::{}", symbol_const),
+ cx.tcx.def_path_str(def_id),
Applicability::MachineApplicable,
);
}
fn main() {
// Direct use of Symbol::intern
- let _ = rustc_span::symbol::sym::f32;
+ let _ = rustc_span::sym::f32;
// Using a sym macro
- let _ = rustc_span::symbol::sym::f32;
+ let _ = rustc_span::sym::f32;
// Correct suggestion when symbol isn't stringified constant name
- let _ = rustc_span::symbol::sym::proc_dash_macro;
+ let _ = rustc_span::sym::proc_dash_macro;
// Interning a symbol that is not defined
let _ = Symbol::intern("xyz123");
--> $DIR/interning_defined_symbol.rs:17:13
|
LL | let _ = Symbol::intern("f32");
- | ^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::symbol::sym::f32`
+ | ^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::sym::f32`
|
note: the lint level is defined here
--> $DIR/interning_defined_symbol.rs:2:9
--> $DIR/interning_defined_symbol.rs:20:13
|
LL | let _ = sym!(f32);
- | ^^^^^^^^^ help: try: `rustc_span::symbol::sym::f32`
+ | ^^^^^^^^^ help: try: `rustc_span::sym::f32`
error: interning a defined symbol
--> $DIR/interning_defined_symbol.rs:23:13
|
LL | let _ = Symbol::intern("proc-macro");
- | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::symbol::sym::proc_dash_macro`
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::sym::proc_dash_macro`
error: aborting due to 3 previous errors