1 //! Some lints that are only useful in the compiler or crates that use compiler internals, such as
4 use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_errors::Applicability;
7 use rustc_hir::{GenericArg, HirId, MutTy, Mutability, Path, PathSegment, QPath, Ty, TyKind};
8 use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
9 use rustc_span::hygiene::{ExpnKind, MacroKind};
10 use rustc_span::symbol::{sym, Symbol};
11 use syntax::ast::{Ident, Item, ItemKind};
14 pub rustc::DEFAULT_HASH_TYPES,
16 "forbid HashMap and HashSet and suggest the FxHash* variants",
17 report_in_external_macro: true
20 pub struct DefaultHashTypes {
21 map: FxHashMap<Symbol, Symbol>,
24 impl DefaultHashTypes {
25 // we are allowed to use `HashMap` and `HashSet` as identifiers for implementing the lint itself
26 #[allow(rustc::default_hash_types)]
27 pub fn new() -> Self {
28 let mut map = FxHashMap::default();
29 map.insert(sym::HashMap, sym::FxHashMap);
30 map.insert(sym::HashSet, sym::FxHashSet);
35 impl_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
37 impl EarlyLintPass for DefaultHashTypes {
38 fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
39 if let Some(replace) = self.map.get(&ident.name) {
40 let msg = format!("Prefer {} over {}, it has better performance", replace, ident);
41 let mut db = cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, &msg);
46 Applicability::MaybeIncorrect, // FxHashMap, ... needs another import
48 db.note(&format!("a `use rustc_data_structures::fx::{}` may be necessary", replace))
55 pub rustc::USAGE_OF_TY_TYKIND,
57 "usage of `ty::TyKind` outside of the `ty::sty` module",
58 report_in_external_macro: true
62 pub rustc::TY_PASS_BY_REFERENCE,
64 "passing `Ty` or `TyCtxt` by reference",
65 report_in_external_macro: true
69 pub rustc::USAGE_OF_QUALIFIED_TY,
71 "using `ty::{Ty,TyCtxt}` instead of importing it",
72 report_in_external_macro: true
75 declare_lint_pass!(TyTyKind => [
78 USAGE_OF_QUALIFIED_TY,
81 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
82 fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path<'tcx>, _: HirId) {
83 let segments = path.segments.iter().rev().skip(1).rev();
85 if let Some(last) = segments.last() {
86 let span = path.span.with_hi(last.ident.span.hi());
87 if lint_ty_kind_usage(cx, last) {
88 cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, "usage of `ty::TyKind::<kind>`")
91 "try using ty::<kind> directly",
93 Applicability::MaybeIncorrect, // ty maybe needs an import
100 fn check_ty(&mut self, cx: &LateContext<'_, '_>, ty: &'tcx Ty<'tcx>) {
102 TyKind::Path(qpath) => {
103 if let QPath::Resolved(_, path) = qpath {
104 if let Some(last) = path.segments.iter().last() {
105 if lint_ty_kind_usage(cx, last) {
109 "usage of `ty::TyKind`",
111 .help("try using `Ty` instead")
114 if ty.span.from_expansion() {
117 if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {
118 if path.segments.len() > 1 {
120 USAGE_OF_QUALIFIED_TY,
122 &format!("usage of qualified `ty::{}`", t),
126 "try using it unqualified",
128 // The import probably needs to be changed
129 Applicability::MaybeIncorrect,
138 TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::Not }) => {
139 if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner_def_id()) {
140 if cx.tcx.impl_trait_ref(impl_did).is_some() {
144 if let Some(t) = is_ty_or_ty_ctxt(cx, &inner_ty) {
146 TY_PASS_BY_REFERENCE,
148 &format!("passing `{}` by reference", t),
152 "try passing by value",
154 // Changing type of function argument
155 Applicability::MaybeIncorrect,
165 fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment<'_>) -> bool {
166 if let Some(res) = segment.res {
167 if let Some(did) = res.opt_def_id() {
168 return cx.tcx.is_diagnostic_item(sym::TyKind, did);
175 fn is_ty_or_ty_ctxt(cx: &LateContext<'_, '_>, ty: &Ty<'_>) -> Option<String> {
177 TyKind::Path(qpath) => {
178 if let QPath::Resolved(_, path) = qpath {
179 let did = path.res.opt_def_id()?;
180 if cx.tcx.is_diagnostic_item(sym::Ty, did) {
181 return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
182 } else if cx.tcx.is_diagnostic_item(sym::TyCtxt, did) {
183 return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
193 fn gen_args(segment: &PathSegment<'_>) -> String {
194 if let Some(args) = &segment.args {
199 if let GenericArg::Lifetime(lt) = arg {
200 Some(lt.name.ident().to_string())
205 .collect::<Vec<_>>();
207 if !lifetimes.is_empty() {
208 return format!("<{}>", lifetimes.join(", "));
216 pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
218 "`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
221 declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
223 impl EarlyLintPass for LintPassImpl {
224 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
225 if let ItemKind::Impl { of_trait: Some(lint_pass), .. } = &item.kind {
226 if let Some(last) = lint_pass.path.segments.last() {
227 if last.ident.name == sym::LintPass {
228 let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
229 let call_site = expn_data.call_site;
230 if expn_data.kind != ExpnKind::Macro(MacroKind::Bang, sym::impl_lint_pass)
231 && call_site.ctxt().outer_expn_data().kind
232 != ExpnKind::Macro(MacroKind::Bang, sym::declare_lint_pass)
235 LINT_PASS_IMPL_WITHOUT_MACRO,
237 "implementing `LintPass` by hand",
239 .help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")