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};
6 use rustc_errors::Applicability;
7 use rustc_hir::def::Res;
9 GenericArg, HirId, Item, ItemKind, MutTy, Mutability, Node, Path, PathSegment, QPath, Ty,
13 use rustc_session::{declare_lint_pass, declare_tool_lint};
14 use rustc_span::hygiene::{ExpnKind, MacroKind};
15 use rustc_span::symbol::{kw, sym, Symbol};
18 pub rustc::DEFAULT_HASH_TYPES,
20 "forbid HashMap and HashSet and suggest the FxHash* variants",
21 report_in_external_macro: true
24 declare_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
26 impl LateLintPass<'_> for DefaultHashTypes {
27 fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, hir_id: HirId) {
28 let def_id = match path.res {
29 Res::Def(rustc_hir::def::DefKind::Struct, id) => id,
32 if matches!(cx.tcx.hir().get(hir_id), Node::Item(Item { kind: ItemKind::Use(..), .. })) {
33 // don't lint imports, only actual usages
36 let replace = match cx.tcx.get_diagnostic_name(def_id) {
37 Some(sym::HashMap) => "FxHashMap",
38 Some(sym::HashSet) => "FxHashSet",
41 cx.struct_span_lint(DEFAULT_HASH_TYPES, path.span, |lint| {
43 "prefer `{}` over `{}`, it has better performance",
45 cx.tcx.item_name(def_id)
48 .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",
58 report_in_external_macro: true
62 pub rustc::TY_PASS_BY_REFERENCE,
64 "passing `Ty` or `TyCtxt` by reference",
65 report_in_external_macro: true
69 pub rustc::USAGE_OF_QUALIFIED_TY,
71 "using `ty::{Ty,TyCtxt}` instead of importing it",
72 report_in_external_macro: true
75 declare_lint_pass!(TyTyKind => [
78 USAGE_OF_QUALIFIED_TY,
81 impl<'tcx> LateLintPass<'tcx> for TyTyKind {
82 fn check_path(&mut self, cx: &LateContext<'_>, path: &'tcx Path<'tcx>, _: HirId) {
83 let segments = path.segments.iter().rev().skip(1).rev();
85 if let Some(last) = segments.last() {
86 let span = path.span.with_hi(last.ident.span.hi());
87 if lint_ty_kind_usage(cx, last) {
88 cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, |lint| {
89 lint.build("usage of `ty::TyKind::<kind>`")
92 "try using ty::<kind> directly",
94 Applicability::MaybeIncorrect, // ty maybe needs an import
102 fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx Ty<'tcx>) {
104 TyKind::Path(QPath::Resolved(_, path)) => {
105 if let Some(last) = path.segments.iter().last() {
106 if lint_ty_kind_usage(cx, last) {
107 cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, |lint| {
108 lint.build("usage of `ty::TyKind`")
109 .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 {
118 cx.struct_span_lint(USAGE_OF_QUALIFIED_TY, path.span, |lint| {
119 lint.build(&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::Not }) => {
135 if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner.to_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) {
141 cx.struct_span_lint(TY_PASS_BY_REFERENCE, ty.span, |lint| {
142 lint.build(&format!("passing `{}` by reference", t))
145 "try passing by value",
147 // Changing type of function argument
148 Applicability::MaybeIncorrect,
159 fn lint_ty_kind_usage(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> bool {
160 if let Some(res) = segment.res {
161 if let Some(did) = res.opt_def_id() {
162 return cx.tcx.is_diagnostic_item(sym::TyKind, did);
169 fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<String> {
170 if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind {
172 Res::Def(_, def_id) => {
173 if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(def_id) {
174 return Some(format!("{}{}", name, gen_args(path.segments.last().unwrap())));
177 // Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
178 Res::SelfTy(None, Some((did, _))) => {
179 if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
180 if let Some(name @ (sym::Ty | sym::TyCtxt)) =
181 cx.tcx.get_diagnostic_name(adt.did)
183 // NOTE: This path is currently unreachable as `Ty<'tcx>` is
184 // defined as a type alias meaning that `impl<'tcx> Ty<'tcx>`
185 // is not actually allowed.
187 // I(@lcnr) still kept this branch in so we don't miss this
188 // if we ever change it in the future.
189 return Some(format!("{}<{}>", name, substs[0]));
200 fn gen_args(segment: &PathSegment<'_>) -> String {
201 if let Some(args) = &segment.args {
206 if let GenericArg::Lifetime(lt) = arg {
207 Some(lt.name.ident().to_string())
212 .collect::<Vec<_>>();
214 if !lifetimes.is_empty() {
215 return format!("<{}>", lifetimes.join(", "));
223 pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
225 "`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
228 declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
230 impl EarlyLintPass for LintPassImpl {
231 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
232 if let ast::ItemKind::Impl(box ast::Impl { of_trait: Some(lint_pass), .. }) = &item.kind {
233 if let Some(last) = lint_pass.path.segments.last() {
234 if last.ident.name == sym::LintPass {
235 let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
236 let call_site = expn_data.call_site;
237 if expn_data.kind != ExpnKind::Macro(MacroKind::Bang, sym::impl_lint_pass)
238 && call_site.ctxt().outer_expn_data().kind
239 != ExpnKind::Macro(MacroKind::Bang, sym::declare_lint_pass)
242 LINT_PASS_IMPL_WITHOUT_MACRO,
245 lint.build("implementing `LintPass` by hand")
246 .help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")
258 pub rustc::EXISTING_DOC_KEYWORD,
260 "Check that documented keywords in std and core actually exist",
261 report_in_external_macro: true
264 declare_lint_pass!(ExistingDocKeyword => [EXISTING_DOC_KEYWORD]);
266 fn is_doc_keyword(s: Symbol) -> bool {
270 impl<'tcx> LateLintPass<'tcx> for ExistingDocKeyword {
271 fn check_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::Item<'_>) {
272 for attr in cx.tcx.hir().attrs(item.hir_id()) {
273 if !attr.has_name(sym::doc) {
276 if let Some(list) = attr.meta_item_list() {
278 if nested.has_name(sym::keyword) {
281 .expect("#[doc(keyword = \"...\")] expected a value!");
282 if is_doc_keyword(v) {
285 cx.struct_span_lint(EXISTING_DOC_KEYWORD, attr.span, |lint| {
287 "Found non-existing keyword `{}` used in \
288 `#[doc(keyword = \"...\")]`",
291 .help("only existing keywords are allowed in core/std")