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 rustc_session::declare_tool_lint;
11 use syntax::ast::{Ident, Item, ItemKind};
12 use syntax::symbol::{sym, Symbol};
15 pub rustc::DEFAULT_HASH_TYPES,
17 "forbid HashMap and HashSet and suggest the FxHash* variants"
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"
61 pub rustc::TY_PASS_BY_REFERENCE,
63 "passing `Ty` or `TyCtxt` by reference"
67 pub rustc::USAGE_OF_QUALIFIED_TY,
69 "using `ty::{Ty,TyCtxt}` instead of importing it"
72 declare_lint_pass!(TyTyKind => [
75 USAGE_OF_QUALIFIED_TY,
78 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
79 fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path, _: HirId) {
80 let segments = path.segments.iter().rev().skip(1).rev();
82 if let Some(last) = segments.last() {
83 let span = path.span.with_hi(last.ident.span.hi());
84 if lint_ty_kind_usage(cx, last) {
85 cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, "usage of `ty::TyKind::<kind>`")
88 "try using ty::<kind> directly",
90 Applicability::MaybeIncorrect, // ty maybe needs an import
97 fn check_ty(&mut self, cx: &LateContext<'_, '_>, ty: &'tcx Ty) {
99 TyKind::Path(qpath) => {
100 if let QPath::Resolved(_, path) = qpath {
101 if let Some(last) = path.segments.iter().last() {
102 if lint_ty_kind_usage(cx, last) {
106 "usage of `ty::TyKind`",
108 .help("try using `Ty` instead")
111 if ty.span.from_expansion() {
114 if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {
115 if path.segments.len() > 1 {
117 USAGE_OF_QUALIFIED_TY,
119 &format!("usage of qualified `ty::{}`", t),
123 "try using it unqualified",
125 // The import probably needs to be changed
126 Applicability::MaybeIncorrect,
135 TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::Not }) => {
136 if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner_def_id()) {
137 if cx.tcx.impl_trait_ref(impl_did).is_some() {
141 if let Some(t) = is_ty_or_ty_ctxt(cx, &inner_ty) {
143 TY_PASS_BY_REFERENCE,
145 &format!("passing `{}` by reference", t),
149 "try passing by value",
151 // Changing type of function argument
152 Applicability::MaybeIncorrect,
162 fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool {
163 if let Some(res) = segment.res {
164 if let Some(did) = res.opt_def_id() {
165 return cx.tcx.is_diagnostic_item(sym::TyKind, did);
172 fn is_ty_or_ty_ctxt(cx: &LateContext<'_, '_>, ty: &Ty) -> Option<String> {
174 TyKind::Path(qpath) => {
175 if let QPath::Resolved(_, path) = qpath {
176 let did = path.res.opt_def_id()?;
177 if cx.tcx.is_diagnostic_item(sym::Ty, did) {
178 return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
179 } else if cx.tcx.is_diagnostic_item(sym::TyCtxt, did) {
180 return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
190 fn gen_args(segment: &PathSegment) -> String {
191 if let Some(args) = &segment.args {
196 if let GenericArg::Lifetime(lt) = arg {
197 Some(lt.name.ident().to_string())
202 .collect::<Vec<_>>();
204 if !lifetimes.is_empty() {
205 return format!("<{}>", lifetimes.join(", "));
213 pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
215 "`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
218 declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
220 impl EarlyLintPass for LintPassImpl {
221 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
222 if let ItemKind::Impl(_, _, _, _, Some(lint_pass), _, _) = &item.kind {
223 if let Some(last) = lint_pass.path.segments.last() {
224 if last.ident.name == sym::LintPass {
225 let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
226 let call_site = expn_data.call_site;
227 if expn_data.kind.descr() != sym::impl_lint_pass
228 && call_site.ctxt().outer_expn_data().kind.descr() != sym::declare_lint_pass
231 LINT_PASS_IMPL_WITHOUT_MACRO,
233 "implementing `LintPass` by hand",
235 .help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")