@@ -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
@@ -657,3 +658,33 @@ fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
657
658
_ => false ,
658
659
}
659
660
}
661
+
662
+ declare_tool_lint ! {
663
+ /// The `symbol_intern_string_literal` detects `Symbol::intern` being called on a string literal
664
+ pub rustc:: SYMBOL_INTERN_STRING_LITERAL ,
665
+ // rustc_driver crates out of the compiler can't/shouldn't add preinterned symbols;
666
+ // bootstrap will deny this manually
667
+ Allow ,
668
+ "Forbid uses of string literals in `Symbol::intern`, suggesting preinterning instead" ,
669
+ report_in_external_macro: true
670
+ }
671
+
672
+ declare_lint_pass ! ( SymbolInternStringLiteral => [ SYMBOL_INTERN_STRING_LITERAL ] ) ;
673
+
674
+ impl < ' tcx > LateLintPass < ' tcx > for SymbolInternStringLiteral {
675
+ fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) {
676
+ if let ExprKind :: Call ( path, [ arg] ) = expr. kind
677
+ && let ExprKind :: Path ( ref qpath) = path. kind
678
+ && let Some ( def_id) = cx. qpath_res ( qpath, path. hir_id ) . opt_def_id ( )
679
+ && cx. tcx . is_diagnostic_item ( sym:: SymbolIntern , def_id)
680
+ && let ExprKind :: Lit ( kind) = arg. kind
681
+ && let rustc_ast:: LitKind :: Str ( _, _) = kind. node
682
+ {
683
+ cx. emit_span_lint (
684
+ SYMBOL_INTERN_STRING_LITERAL ,
685
+ kind. span ,
686
+ SymbolInternStringLiteralDiag ,
687
+ ) ;
688
+ }
689
+ }
690
+ }
0 commit comments