1 //! Some lints that are only useful in the compiler or crates that use compiler internals, such as
4 use crate::hir::{GenericArg, HirId, MutTy, Mutability, Path, PathSegment, QPath, Ty, TyKind};
6 EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass,
8 use errors::Applicability;
9 use rustc_data_structures::fx::FxHashMap;
10 use syntax::ast::Ident;
11 use syntax::symbol::{sym, Symbol};
14 pub DEFAULT_HASH_TYPES,
16 "forbid HashMap and HashSet and suggest the FxHash* variants"
19 pub struct DefaultHashTypes {
20 map: FxHashMap<Symbol, Symbol>,
23 impl DefaultHashTypes {
24 // we are allowed to use `HashMap` and `HashSet` as identifiers for implementing the lint itself
26 pub fn new() -> Self {
27 let mut map = FxHashMap::default();
28 map.insert(sym::HashMap, sym::FxHashMap);
29 map.insert(sym::HashSet, sym::FxHashSet);
34 impl_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
36 impl EarlyLintPass for DefaultHashTypes {
37 fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
38 if let Some(replace) = self.map.get(&ident.name) {
40 "Prefer {} over {}, it has better performance",
43 let mut db = cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, &msg);
48 Applicability::MaybeIncorrect, // FxHashMap, ... needs another import
51 "a `use rustc_data_structures::fx::{}` may be necessary",
60 pub USAGE_OF_TY_TYKIND,
62 "usage of `ty::TyKind` outside of the `ty::sty` module"
66 pub TY_PASS_BY_REFERENCE,
68 "passing `Ty` or `TyCtxt` by reference"
72 pub USAGE_OF_QUALIFIED_TY,
74 "using `ty::{Ty,TyCtxt}` instead of importing it"
77 declare_lint_pass!(TyTyKind => [
80 USAGE_OF_QUALIFIED_TY,
83 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
84 fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path, _: HirId) {
85 let segments = path.segments.iter().rev().skip(1).rev();
87 if let Some(last) = segments.last() {
88 let span = path.span.with_hi(last.ident.span.hi());
89 if lint_ty_kind_usage(cx, last) {
90 cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, "usage of `ty::TyKind::<kind>`")
93 "try using ty::<kind> directly",
95 Applicability::MaybeIncorrect, // ty maybe needs an import
102 fn check_ty(&mut self, cx: &LateContext<'_, '_>, ty: &'tcx Ty) {
104 TyKind::Path(qpath) => {
105 if let QPath::Resolved(_, path) = qpath {
106 if let Some(last) = path.segments.iter().last() {
107 if lint_ty_kind_usage(cx, last) {
111 "usage of `ty::TyKind`",
113 .help("try using `Ty` instead")
116 if ty.span.ctxt().outer().expn_info().is_some() {
119 if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {
120 if path.segments.len() > 1 {
122 USAGE_OF_QUALIFIED_TY,
124 &format!("usage of qualified `ty::{}`", t),
128 "try using it unqualified",
130 // The import probably needs to be changed
131 Applicability::MaybeIncorrect,
144 mutbl: Mutability::MutImmutable,
147 if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner_def_id()) {
148 if cx.tcx.impl_trait_ref(impl_did).is_some() {
152 if let Some(t) = is_ty_or_ty_ctxt(cx, &inner_ty) {
154 TY_PASS_BY_REFERENCE,
156 &format!("passing `{}` by reference", t),
160 "try passing by value",
162 // Changing type of function argument
163 Applicability::MaybeIncorrect,
173 fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool {
174 if segment.ident.name == sym::TyKind {
175 if let Some(res) = segment.res {
176 if let Some(did) = res.opt_def_id() {
177 return cx.match_def_path(did, TYKIND_PATH);
185 const TYKIND_PATH: &[Symbol] = &[sym::rustc, sym::ty, sym::sty, sym::TyKind];
186 const TY_PATH: &[Symbol] = &[sym::rustc, sym::ty, sym::Ty];
187 const TYCTXT_PATH: &[Symbol] = &[sym::rustc, sym::ty, sym::context, sym::TyCtxt];
189 fn is_ty_or_ty_ctxt(cx: &LateContext<'_, '_>, ty: &Ty) -> Option<String> {
191 TyKind::Path(qpath) => {
192 if let QPath::Resolved(_, path) = qpath {
193 let did = path.res.opt_def_id()?;
194 if cx.match_def_path(did, TY_PATH) {
195 return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
196 } else if cx.match_def_path(did, TYCTXT_PATH) {
197 return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
207 fn gen_args(segment: &PathSegment) -> String {
208 if let Some(args) = &segment.args {
213 if let GenericArg::Lifetime(lt) = arg {
214 Some(lt.name.ident().to_string())
219 .collect::<Vec<_>>();
221 if !lifetimes.is_empty() {
222 return format!("<{}>", lifetimes.join(", "));