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

Commit 121c65f

Browse files
committed
Add keywords to interning defined symbol lint
1 parent 24c700b commit 121c65f

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

clippy_lints/src/utils/internal_lints.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -881,16 +881,18 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
881881
return;
882882
}
883883

884-
if let Some(Res::Def(_, def_id)) = path_to_res(cx, &paths::SYM_MODULE) {
885-
for item in cx.tcx.item_children(def_id).iter() {
886-
if_chain! {
887-
if let Res::Def(DefKind::Const, item_def_id) = item.res;
888-
let ty = cx.tcx.type_of(item_def_id);
889-
if match_type(cx, ty, &paths::SYMBOL);
890-
if let Ok(ConstValue::Scalar(value)) = cx.tcx.const_eval_poly(item_def_id);
891-
if let Ok(value) = value.to_u32();
892-
then {
893-
self.symbol_map.insert(value, item_def_id);
884+
for &module in &[&paths::KW_MODULE, &paths::SYM_MODULE] {
885+
if let Some(Res::Def(_, def_id)) = path_to_res(cx, module) {
886+
for item in cx.tcx.item_children(def_id).iter() {
887+
if_chain! {
888+
if let Res::Def(DefKind::Const, item_def_id) = item.res;
889+
let ty = cx.tcx.type_of(item_def_id);
890+
if match_type(cx, ty, &paths::SYMBOL);
891+
if let Ok(ConstValue::Scalar(value)) = cx.tcx.const_eval_poly(item_def_id);
892+
if let Ok(value) = value.to_u32();
893+
then {
894+
self.symbol_map.insert(value, item_def_id);
895+
}
894896
}
895897
}
896898
}

clippy_lints/src/utils/paths.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub const IPADDR_V4: [&str; 4] = ["std", "net", "IpAddr", "V4"];
6565
pub const IPADDR_V6: [&str; 4] = ["std", "net", "IpAddr", "V6"];
6666
pub const ITERATOR: [&str; 5] = ["core", "iter", "traits", "iterator", "Iterator"];
6767
#[cfg(feature = "internal-lints")]
68+
pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"];
69+
#[cfg(feature = "internal-lints")]
6870
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
6971
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
7072
#[cfg(feature = "internal-lints")]

tests/ui-internal/interning_defined_symbol.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ fn main() {
2222
// Correct suggestion when symbol isn't stringified constant name
2323
let _ = rustc_span::sym::proc_dash_macro;
2424

25+
// interning a keyword
26+
let _ = rustc_span::symbol::kw::SelfLower;
27+
2528
// Interning a symbol that is not defined
2629
let _ = Symbol::intern("xyz123");
2730
let _ = sym!(xyz123);

tests/ui-internal/interning_defined_symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ fn main() {
2222
// Correct suggestion when symbol isn't stringified constant name
2323
let _ = Symbol::intern("proc-macro");
2424

25+
// interning a keyword
26+
let _ = Symbol::intern("self");
27+
2528
// Interning a symbol that is not defined
2629
let _ = Symbol::intern("xyz123");
2730
let _ = sym!(xyz123);

tests/ui-internal/interning_defined_symbol.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@ error: interning a defined symbol
2323
LL | let _ = Symbol::intern("proc-macro");
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::sym::proc_dash_macro`
2525

26-
error: aborting due to 3 previous errors
26+
error: interning a defined symbol
27+
--> $DIR/interning_defined_symbol.rs:26:13
28+
|
29+
LL | let _ = Symbol::intern("self");
30+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::symbol::kw::SelfLower`
31+
32+
error: aborting due to 4 previous errors
2733

0 commit comments

Comments
 (0)