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::symbol::{sym, Symbol};
10 use syntax::ast::{Ident, Item, ItemKind};
13 pub rustc::DEFAULT_HASH_TYPES,
15 "forbid HashMap and HashSet and suggest the FxHash* variants",
16 report_in_external_macro: true
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",
57 report_in_external_macro: true
61 pub rustc::TY_PASS_BY_REFERENCE,
63 "passing `Ty` or `TyCtxt` by reference",
64 report_in_external_macro: true
68 pub rustc::USAGE_OF_QUALIFIED_TY,
70 "using `ty::{Ty,TyCtxt}` instead of importing it",
71 report_in_external_macro: true
74 declare_lint_pass!(TyTyKind => [
77 USAGE_OF_QUALIFIED_TY,
80 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
81 fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path<'tcx>, _: HirId) {
82 let segments = path.segments.iter().rev().skip(1).rev();
84 if let Some(last) = segments.last() {
85 let span = path.span.with_hi(last.ident.span.hi());
86 if lint_ty_kind_usage(cx, last) {
87 cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, "usage of `ty::TyKind::<kind>`")
90 "try using ty::<kind> directly",
92 Applicability::MaybeIncorrect, // ty maybe needs an import
99 fn check_ty(&mut self, cx: &LateContext<'_, '_>, ty: &'tcx Ty<'tcx>) {
101 TyKind::Path(qpath) => {
102 if let QPath::Resolved(_, path) = qpath {
103 if let Some(last) = path.segments.iter().last() {
104 if lint_ty_kind_usage(cx, last) {
108 "usage of `ty::TyKind`",
110 .help("try using `Ty` instead")
113 if ty.span.from_expansion() {
116 if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {
117 if path.segments.len() > 1 {
119 USAGE_OF_QUALIFIED_TY,
121 &format!("usage of qualified `ty::{}`", t),
125 "try using it unqualified",
127 // The import probably needs to be changed
128 Applicability::MaybeIncorrect,
137 TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::Not }) => {
138 if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner_def_id()) {
139 if cx.tcx.impl_trait_ref(impl_did).is_some() {
143 if let Some(t) = is_ty_or_ty_ctxt(cx, &inner_ty) {
145 TY_PASS_BY_REFERENCE,
147 &format!("passing `{}` by reference", t),
151 "try passing by value",
153 // Changing type of function argument
154 Applicability::MaybeIncorrect,
164 fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment<'_>) -> bool {
165 if let Some(res) = segment.res {
166 if let Some(did) = res.opt_def_id() {
167 return cx.tcx.is_diagnostic_item(sym::TyKind, did);
174 fn is_ty_or_ty_ctxt(cx: &LateContext<'_, '_>, ty: &Ty<'_>) -> Option<String> {
176 TyKind::Path(qpath) => {
177 if let QPath::Resolved(_, path) = qpath {
178 let did = path.res.opt_def_id()?;
179 if cx.tcx.is_diagnostic_item(sym::Ty, did) {
180 return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
181 } else if cx.tcx.is_diagnostic_item(sym::TyCtxt, did) {
182 return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
192 fn gen_args(segment: &PathSegment<'_>) -> String {
193 if let Some(args) = &segment.args {
198 if let GenericArg::Lifetime(lt) = arg {
199 Some(lt.name.ident().to_string())
204 .collect::<Vec<_>>();
206 if !lifetimes.is_empty() {
207 return format!("<{}>", lifetimes.join(", "));
215 pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
217 "`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
220 declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
222 impl EarlyLintPass for LintPassImpl {
223 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
224 if let ItemKind::Impl(_, _, _, _, Some(lint_pass), _, _) = &item.kind {
225 if let Some(last) = lint_pass.path.segments.last() {
226 if last.ident.name == sym::LintPass {
227 let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
228 let call_site = expn_data.call_site;
229 if expn_data.kind.descr() != sym::impl_lint_pass
230 && call_site.ctxt().outer_expn_data().kind.descr() != sym::declare_lint_pass
233 LINT_PASS_IMPL_WITHOUT_MACRO,
235 "implementing `LintPass` by hand",
237 .help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")