@@ -17,8 +17,9 @@ use tracing::debug;
17
17
18
18
use crate :: lints:: {
19
19
BadOptAccessDiag , DefaultHashTypesDiag , DiagOutOfImpl , LintPassByHand , NonExistentDocKeyword ,
20
- NonGlobImportTypeIrInherent , QueryInstability , QueryUntracked , SpanUseEqCtxtDiag , TyQualified ,
21
- TykindDiag , TykindKind , TypeIrInherentUsage , UntranslatableDiag ,
20
+ NonGlobImportTypeIrInherent , QueryInstability , QueryUntracked , SpanUseEqCtxtDiag ,
21
+ SymbolInternStringLiteralDiag , TyQualified , TykindDiag , TykindKind , TypeIrInherentUsage ,
22
+ UntranslatableDiag ,
22
23
} ;
23
24
use crate :: { EarlyContext , EarlyLintPass , LateContext , LateLintPass , LintContext } ;
24
25
@@ -650,3 +651,33 @@ fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
650
651
_ => false ,
651
652
}
652
653
}
654
+
655
+ declare_tool_lint ! {
656
+ /// The `symbol_intern_string_literal` detects `Symbol::intern` being called on a string literal
657
+ pub rustc:: SYMBOL_INTERN_STRING_LITERAL ,
658
+ // rustc_driver crates out of the compiler can't/shouldn't add preinterned symbols;
659
+ // bootstrap will deny this manually
660
+ Allow ,
661
+ "Forbid uses of string literals in `Symbol::intern`, suggesting preinterning instead" ,
662
+ report_in_external_macro: true
663
+ }
664
+
665
+ declare_lint_pass ! ( SymbolInternStringLiteral => [ SYMBOL_INTERN_STRING_LITERAL ] ) ;
666
+
667
+ impl < ' tcx > LateLintPass < ' tcx > for SymbolInternStringLiteral {
668
+ fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) {
669
+ if let ExprKind :: Call ( path, [ arg] ) = expr. kind
670
+ && let ExprKind :: Path ( ref qpath) = path. kind
671
+ && let Some ( def_id) = cx. qpath_res ( qpath, path. hir_id ) . opt_def_id ( )
672
+ && cx. tcx . is_diagnostic_item ( sym:: SymbolIntern , def_id)
673
+ && let ExprKind :: Lit ( kind) = arg. kind
674
+ && let rustc_ast:: LitKind :: Str ( _, _) = kind. node
675
+ {
676
+ cx. emit_span_lint (
677
+ SYMBOL_INTERN_STRING_LITERAL ,
678
+ kind. span ,
679
+ SymbolInternStringLiteralDiag ,
680
+ ) ;
681
+ }
682
+ }
683
+ }
0 commit comments