Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 24c700b

Browse files
committed
Use DefId in interning defined symbol lint
1 parent 2950c8e commit 24c700b

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

clippy_lints/src/utils/internal_lints.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc_errors::Applicability;
1111
use rustc_hir as hir;
1212
use rustc_hir::def::{DefKind, Res};
13+
use rustc_hir::def_id::DefId;
1314
use rustc_hir::hir_id::CRATE_HIR_ID;
1415
use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
1516
use rustc_hir::{Crate, Expr, ExprKind, HirId, Item, MutTy, Mutability, Node, Path, StmtKind, Ty, TyKind};
@@ -868,8 +869,8 @@ impl<'tcx> LateLintPass<'tcx> for InvalidPaths {
868869

869870
#[derive(Default)]
870871
pub struct InterningDefinedSymbol {
871-
// Maps the symbol value to the constant name.
872-
symbol_map: FxHashMap<u32, String>,
872+
// Maps the symbol value to the constant DefId.
873+
symbol_map: FxHashMap<u32, DefId>,
873874
}
874875

875876
impl_lint_pass!(InterningDefinedSymbol => [INTERNING_DEFINED_SYMBOL]);
@@ -889,7 +890,7 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
889890
if let Ok(ConstValue::Scalar(value)) = cx.tcx.const_eval_poly(item_def_id);
890891
if let Ok(value) = value.to_u32();
891892
then {
892-
self.symbol_map.insert(value, item.ident.to_string());
893+
self.symbol_map.insert(value, item_def_id);
893894
}
894895
}
895896
}
@@ -903,15 +904,15 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
903904
if match_def_path(cx, *def_id, &paths::SYMBOL_INTERN);
904905
if let Some(Constant::Str(arg)) = constant_simple(cx, cx.typeck_results(), arg);
905906
let value = Symbol::intern(&arg).as_u32();
906-
if let Some(symbol_const) = self.symbol_map.get(&value);
907+
if let Some(&def_id) = self.symbol_map.get(&value);
907908
then {
908909
span_lint_and_sugg(
909910
cx,
910911
INTERNING_DEFINED_SYMBOL,
911912
is_expn_of(expr.span, "sym").unwrap_or(expr.span),
912913
"interning a defined symbol",
913914
"try",
914-
format!("rustc_span::symbol::sym::{}", symbol_const),
915+
cx.tcx.def_path_str(def_id),
915916
Applicability::MachineApplicable,
916917
);
917918
}

tests/ui-internal/interning_defined_symbol.fixed

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ macro_rules! sym {
1414

1515
fn main() {
1616
// Direct use of Symbol::intern
17-
let _ = rustc_span::symbol::sym::f32;
17+
let _ = rustc_span::sym::f32;
1818

1919
// Using a sym macro
20-
let _ = rustc_span::symbol::sym::f32;
20+
let _ = rustc_span::sym::f32;
2121

2222
// Correct suggestion when symbol isn't stringified constant name
23-
let _ = rustc_span::symbol::sym::proc_dash_macro;
23+
let _ = rustc_span::sym::proc_dash_macro;
2424

2525
// Interning a symbol that is not defined
2626
let _ = Symbol::intern("xyz123");

tests/ui-internal/interning_defined_symbol.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: interning a defined symbol
22
--> $DIR/interning_defined_symbol.rs:17:13
33
|
44
LL | let _ = Symbol::intern("f32");
5-
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::symbol::sym::f32`
5+
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::sym::f32`
66
|
77
note: the lint level is defined here
88
--> $DIR/interning_defined_symbol.rs:2:9
@@ -15,13 +15,13 @@ error: interning a defined symbol
1515
--> $DIR/interning_defined_symbol.rs:20:13
1616
|
1717
LL | let _ = sym!(f32);
18-
| ^^^^^^^^^ help: try: `rustc_span::symbol::sym::f32`
18+
| ^^^^^^^^^ help: try: `rustc_span::sym::f32`
1919

2020
error: interning a defined symbol
2121
--> $DIR/interning_defined_symbol.rs:23:13
2222
|
2323
LL | let _ = Symbol::intern("proc-macro");
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::symbol::sym::proc_dash_macro`
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::sym::proc_dash_macro`
2525

2626
error: aborting due to 3 previous errors
2727

0 commit comments

Comments
 (0)