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, Item, ItemKind};
11 use syntax::symbol::{sym, Symbol};
14 pub rustc::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
25 #[allow(rustc::default_hash_types)]
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) {
39 let msg = format!("Prefer {} over {}, it has better performance", replace, ident);
40 let mut db = cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, &msg);
45 Applicability::MaybeIncorrect, // FxHashMap, ... needs another import
47 db.note(&format!("a `use rustc_data_structures::fx::{}` may be necessary", replace))
54 pub rustc::USAGE_OF_TY_TYKIND,
56 "usage of `ty::TyKind` outside of the `ty::sty` module"
60 pub rustc::TY_PASS_BY_REFERENCE,
62 "passing `Ty` or `TyCtxt` by reference"
66 pub rustc::USAGE_OF_QUALIFIED_TY,
68 "using `ty::{Ty,TyCtxt}` instead of importing it"
71 declare_lint_pass!(TyTyKind => [
74 USAGE_OF_QUALIFIED_TY,
77 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
78 fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path, _: HirId) {
79 let segments = path.segments.iter().rev().skip(1).rev();
81 if let Some(last) = segments.last() {
82 let span = path.span.with_hi(last.ident.span.hi());
83 if lint_ty_kind_usage(cx, last) {
84 cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, "usage of `ty::TyKind::<kind>`")
87 "try using ty::<kind> directly",
89 Applicability::MaybeIncorrect, // ty maybe needs an import
96 fn check_ty(&mut self, cx: &LateContext<'_, '_>, ty: &'tcx Ty) {
98 TyKind::Path(qpath) => {
99 if let QPath::Resolved(_, path) = qpath {
100 if let Some(last) = path.segments.iter().last() {
101 if lint_ty_kind_usage(cx, last) {
105 "usage of `ty::TyKind`",
107 .help("try using `Ty` instead")
110 if ty.span.from_expansion() {
113 if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {
114 if path.segments.len() > 1 {
116 USAGE_OF_QUALIFIED_TY,
118 &format!("usage of qualified `ty::{}`", t),
122 "try using it unqualified",
124 // The import probably needs to be changed
125 Applicability::MaybeIncorrect,
134 TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::MutImmutable }) => {
135 if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner_def_id()) {
136 if cx.tcx.impl_trait_ref(impl_did).is_some() {
140 if let Some(t) = is_ty_or_ty_ctxt(cx, &inner_ty) {
142 TY_PASS_BY_REFERENCE,
144 &format!("passing `{}` by reference", t),
148 "try passing by value",
150 // Changing type of function argument
151 Applicability::MaybeIncorrect,
161 fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool {
162 if segment.ident.name == sym::TyKind {
163 if let Some(res) = segment.res {
164 if let Some(did) = res.opt_def_id() {
165 return cx.match_def_path(did, TYKIND_PATH);
173 const TYKIND_PATH: &[Symbol] = &[sym::rustc, sym::ty, sym::sty, sym::TyKind];
174 const TY_PATH: &[Symbol] = &[sym::rustc, sym::ty, sym::Ty];
175 const TYCTXT_PATH: &[Symbol] = &[sym::rustc, sym::ty, sym::context, sym::TyCtxt];
177 fn is_ty_or_ty_ctxt(cx: &LateContext<'_, '_>, ty: &Ty) -> Option<String> {
179 TyKind::Path(qpath) => {
180 if let QPath::Resolved(_, path) = qpath {
181 let did = path.res.opt_def_id()?;
182 if cx.match_def_path(did, TY_PATH) {
183 return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
184 } else if cx.match_def_path(did, TYCTXT_PATH) {
185 return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
195 fn gen_args(segment: &PathSegment) -> String {
196 if let Some(args) = &segment.args {
201 if let GenericArg::Lifetime(lt) = arg {
202 Some(lt.name.ident().to_string())
207 .collect::<Vec<_>>();
209 if !lifetimes.is_empty() {
210 return format!("<{}>", lifetimes.join(", "));
218 pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
220 "`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
223 declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
225 impl EarlyLintPass for LintPassImpl {
226 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
227 if let ItemKind::Impl(_, _, _, _, Some(lint_pass), _, _) = &item.node {
228 if let Some(last) = lint_pass.path.segments.last() {
229 if last.ident.name == sym::LintPass {
230 let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
231 let call_site = expn_data.call_site;
232 if expn_data.kind.descr() != sym::impl_lint_pass &&
233 call_site.ctxt().outer_expn_data().kind.descr() != 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")