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"
18 pub struct DefaultHashTypes {
19 map: FxHashMap<Symbol, Symbol>,
22 impl DefaultHashTypes {
23 // we are allowed to use `HashMap` and `HashSet` as identifiers for implementing the lint itself
24 #[allow(rustc::default_hash_types)]
25 pub fn new() -> Self {
26 let mut map = FxHashMap::default();
27 map.insert(sym::HashMap, sym::FxHashMap);
28 map.insert(sym::HashSet, sym::FxHashSet);
33 impl_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
35 impl EarlyLintPass for DefaultHashTypes {
36 fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
37 if let Some(replace) = self.map.get(&ident.name) {
38 let msg = format!("Prefer {} over {}, it has better performance", replace, ident);
39 let mut db = cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, &msg);
44 Applicability::MaybeIncorrect, // FxHashMap, ... needs another import
46 db.note(&format!("a `use rustc_data_structures::fx::{}` may be necessary", replace))
53 pub rustc::USAGE_OF_TY_TYKIND,
55 "usage of `ty::TyKind` outside of the `ty::sty` module"
59 pub rustc::TY_PASS_BY_REFERENCE,
61 "passing `Ty` or `TyCtxt` by reference"
65 pub rustc::USAGE_OF_QUALIFIED_TY,
67 "using `ty::{Ty,TyCtxt}` instead of importing it"
70 declare_lint_pass!(TyTyKind => [
73 USAGE_OF_QUALIFIED_TY,
76 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
77 fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path<'tcx>, _: HirId) {
78 let segments = path.segments.iter().rev().skip(1).rev();
80 if let Some(last) = segments.last() {
81 let span = path.span.with_hi(last.ident.span.hi());
82 if lint_ty_kind_usage(cx, last) {
83 cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, "usage of `ty::TyKind::<kind>`")
86 "try using ty::<kind> directly",
88 Applicability::MaybeIncorrect, // ty maybe needs an import
95 fn check_ty(&mut self, cx: &LateContext<'_, '_>, ty: &'tcx Ty<'tcx>) {
97 TyKind::Path(qpath) => {
98 if let QPath::Resolved(_, path) = qpath {
99 if let Some(last) = path.segments.iter().last() {
100 if lint_ty_kind_usage(cx, last) {
104 "usage of `ty::TyKind`",
106 .help("try using `Ty` instead")
109 if ty.span.from_expansion() {
112 if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {
113 if path.segments.len() > 1 {
115 USAGE_OF_QUALIFIED_TY,
117 &format!("usage of qualified `ty::{}`", t),
121 "try using it unqualified",
123 // The import probably needs to be changed
124 Applicability::MaybeIncorrect,
133 TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::Not }) => {
134 if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner_def_id()) {
135 if cx.tcx.impl_trait_ref(impl_did).is_some() {
139 if let Some(t) = is_ty_or_ty_ctxt(cx, &inner_ty) {
141 TY_PASS_BY_REFERENCE,
143 &format!("passing `{}` by reference", t),
147 "try passing by value",
149 // Changing type of function argument
150 Applicability::MaybeIncorrect,
160 fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment<'_>) -> bool {
161 if let Some(res) = segment.res {
162 if let Some(did) = res.opt_def_id() {
163 return cx.tcx.is_diagnostic_item(sym::TyKind, did);
170 fn is_ty_or_ty_ctxt(cx: &LateContext<'_, '_>, ty: &Ty<'_>) -> Option<String> {
172 TyKind::Path(qpath) => {
173 if let QPath::Resolved(_, path) = qpath {
174 let did = path.res.opt_def_id()?;
175 if cx.tcx.is_diagnostic_item(sym::Ty, did) {
176 return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
177 } else if cx.tcx.is_diagnostic_item(sym::TyCtxt, did) {
178 return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
188 fn gen_args(segment: &PathSegment<'_>) -> String {
189 if let Some(args) = &segment.args {
194 if let GenericArg::Lifetime(lt) = arg {
195 Some(lt.name.ident().to_string())
200 .collect::<Vec<_>>();
202 if !lifetimes.is_empty() {
203 return format!("<{}>", lifetimes.join(", "));
211 pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
213 "`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
216 declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
218 impl EarlyLintPass for LintPassImpl {
219 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
220 if let ItemKind::Impl(_, _, _, _, Some(lint_pass), _, _) = &item.kind {
221 if let Some(last) = lint_pass.path.segments.last() {
222 if last.ident.name == sym::LintPass {
223 let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
224 let call_site = expn_data.call_site;
225 if expn_data.kind.descr() != sym::impl_lint_pass
226 && call_site.ctxt().outer_expn_data().kind.descr() != sym::declare_lint_pass
229 LINT_PASS_IMPL_WITHOUT_MACRO,
231 "implementing `LintPass` by hand",
233 .help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")