Add keywords to interning defined symbol lint

This commit is contained in:
Cameron Steffen 2020-12-29 15:40:55 -06:00
parent 24c700b5d7
commit 121c65f0cf
5 changed files with 27 additions and 11 deletions

@ -881,16 +881,18 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
return;
}
if let Some(Res::Def(_, def_id)) = path_to_res(cx, &paths::SYM_MODULE) {
for item in cx.tcx.item_children(def_id).iter() {
if_chain! {
if let Res::Def(DefKind::Const, item_def_id) = item.res;
let ty = cx.tcx.type_of(item_def_id);
if match_type(cx, ty, &paths::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_def_id);
for &module in &[&paths::KW_MODULE, &paths::SYM_MODULE] {
if let Some(Res::Def(_, def_id)) = path_to_res(cx, module) {
for item in cx.tcx.item_children(def_id).iter() {
if_chain! {
if let Res::Def(DefKind::Const, item_def_id) = item.res;
let ty = cx.tcx.type_of(item_def_id);
if match_type(cx, ty, &paths::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_def_id);
}
}
}
}

@ -65,6 +65,8 @@ pub const IPADDR_V4: [&str; 4] = ["std", "net", "IpAddr", "V4"];
pub const IPADDR_V6: [&str; 4] = ["std", "net", "IpAddr", "V6"];
pub const ITERATOR: [&str; 5] = ["core", "iter", "traits", "iterator", "Iterator"];
#[cfg(feature = "internal-lints")]
pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"];
#[cfg(feature = "internal-lints")]
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
#[cfg(feature = "internal-lints")]

@ -22,6 +22,9 @@ fn main() {
// Correct suggestion when symbol isn't stringified constant name
let _ = rustc_span::sym::proc_dash_macro;
// interning a keyword
let _ = rustc_span::symbol::kw::SelfLower;
// Interning a symbol that is not defined
let _ = Symbol::intern("xyz123");
let _ = sym!(xyz123);

@ -22,6 +22,9 @@ fn main() {
// Correct suggestion when symbol isn't stringified constant name
let _ = Symbol::intern("proc-macro");
// interning a keyword
let _ = Symbol::intern("self");
// Interning a symbol that is not defined
let _ = Symbol::intern("xyz123");
let _ = sym!(xyz123);

@ -23,5 +23,11 @@ error: interning a defined symbol
LL | let _ = Symbol::intern("proc-macro");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::sym::proc_dash_macro`
error: aborting due to 3 previous errors
error: interning a defined symbol
--> $DIR/interning_defined_symbol.rs:26:13
|
LL | let _ = Symbol::intern("self");
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::symbol::kw::SelfLower`
error: aborting due to 4 previous errors