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;
8 use rustc_hir::{Expr, ExprKind, GenericArg, Path, PathSegment, QPath};
9 use rustc_hir::{HirId, Item, ItemKind, Node, Ty, TyKind};
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
12 use rustc_span::hygiene::{ExpnKind, MacroKind};
13 use rustc_span::symbol::{kw, sym, Symbol};
16 pub rustc::DEFAULT_HASH_TYPES,
18 "forbid HashMap and HashSet and suggest the FxHash* variants",
19 report_in_external_macro: true
22 declare_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
24 impl LateLintPass<'_> for DefaultHashTypes {
25 fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, hir_id: HirId) {
26 let Res::Def(rustc_hir::def::DefKind::Struct, def_id) = path.res else { return };
27 if matches!(cx.tcx.hir().get(hir_id), Node::Item(Item { kind: ItemKind::Use(..), .. })) {
28 // don't lint imports, only actual usages
31 let replace = match cx.tcx.get_diagnostic_name(def_id) {
32 Some(sym::HashMap) => "FxHashMap",
33 Some(sym::HashSet) => "FxHashSet",
36 cx.struct_span_lint(DEFAULT_HASH_TYPES, path.span, |lint| {
38 "prefer `{}` over `{}`, it has better performance",
40 cx.tcx.item_name(def_id)
43 .note(&format!("a `use rustc_data_structures::fx::{}` may be necessary", replace))
50 pub rustc::POTENTIAL_QUERY_INSTABILITY,
52 "require explicit opt-in when using potentially unstable methods or functions",
53 report_in_external_macro: true
56 declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY]);
58 impl LateLintPass<'_> for QueryStability {
59 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
60 // FIXME(rustdoc): This lint uses typecheck results, causing rustdoc to
61 // error if there are resolution failures.
63 // As internal lints are currently always run if there are `unstable_options`,
64 // they are added to the lint store of rustdoc. Internal lints are also
65 // not used via the `lint_mod` query. Crate lints run outside of a query
66 // so rustdoc currently doesn't disable them.
68 // Instead of relying on this, either change crate lints to a query disabled by
69 // rustdoc, only run internal lints if the user is explicitly opting in
70 // or figure out a different way to avoid running lints for rustdoc.
71 if cx.tcx.sess.opts.actually_rustdoc {
75 let (span, def_id, substs) = match expr.kind {
76 ExprKind::MethodCall(segment, _, _)
77 if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
79 (segment.ident.span, def_id, cx.typeck_results().node_substs(expr.hir_id))
82 let &ty::FnDef(def_id, substs) =
84 .node_type(expr.hir_id)
85 .kind() else { return };
86 (expr.span, def_id, substs)
89 if let Ok(Some(instance)) = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs) {
90 let def_id = instance.def_id();
91 if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) {
92 cx.struct_span_lint(POTENTIAL_QUERY_INSTABILITY, span, |lint| {
94 "using `{}` can result in unstable query results",
95 cx.tcx.item_name(def_id)
98 .note("if you believe this case to be fine, allow this lint and add a comment explaining your rationale")
107 pub rustc::USAGE_OF_TY_TYKIND,
109 "usage of `ty::TyKind` outside of the `ty::sty` module",
110 report_in_external_macro: true
114 pub rustc::USAGE_OF_QUALIFIED_TY,
116 "using `ty::{Ty,TyCtxt}` instead of importing it",
117 report_in_external_macro: true
120 declare_lint_pass!(TyTyKind => [
122 USAGE_OF_QUALIFIED_TY,
125 impl<'tcx> LateLintPass<'tcx> for TyTyKind {
126 fn check_path(&mut self, cx: &LateContext<'_>, path: &'tcx Path<'tcx>, _: HirId) {
127 let segments = path.segments.iter().rev().skip(1).rev();
129 if let Some(last) = segments.last() {
130 let span = path.span.with_hi(last.ident.span.hi());
131 if lint_ty_kind_usage(cx, last) {
132 cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, |lint| {
133 lint.build("usage of `ty::TyKind::<kind>`")
136 "try using ty::<kind> directly",
138 Applicability::MaybeIncorrect, // ty maybe needs an import
146 fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx Ty<'tcx>) {
148 TyKind::Path(QPath::Resolved(_, path)) => {
149 if let Some(last) = path.segments.iter().last() {
150 if lint_ty_kind_usage(cx, last) {
151 cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, |lint| {
152 lint.build("usage of `ty::TyKind`")
153 .help("try using `Ty` instead")
157 if ty.span.from_expansion() {
160 if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {
161 if path.segments.len() > 1 {
162 cx.struct_span_lint(USAGE_OF_QUALIFIED_TY, path.span, |lint| {
163 lint.build(&format!("usage of qualified `ty::{}`", t))
166 "try importing it and using it unqualified",
168 // The import probably needs to be changed
169 Applicability::MaybeIncorrect,
183 fn lint_ty_kind_usage(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> bool {
184 if let Some(res) = segment.res {
185 if let Some(did) = res.opt_def_id() {
186 return cx.tcx.is_diagnostic_item(sym::TyKind, did);
193 fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<String> {
194 if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind {
196 Res::Def(_, def_id) => {
197 if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(def_id) {
198 return Some(format!("{}{}", name, gen_args(path.segments.last().unwrap())));
201 // Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
202 Res::SelfTy { trait_: None, alias_to: Some((did, _)) } => {
203 if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
204 if let Some(name @ (sym::Ty | sym::TyCtxt)) =
205 cx.tcx.get_diagnostic_name(adt.did())
207 // NOTE: This path is currently unreachable as `Ty<'tcx>` is
208 // defined as a type alias meaning that `impl<'tcx> Ty<'tcx>`
209 // is not actually allowed.
211 // I(@lcnr) still kept this branch in so we don't miss this
212 // if we ever change it in the future.
213 return Some(format!("{}<{}>", name, substs[0]));
224 fn gen_args(segment: &PathSegment<'_>) -> String {
225 if let Some(args) = &segment.args {
230 if let GenericArg::Lifetime(lt) = arg {
231 Some(lt.name.ident().to_string())
236 .collect::<Vec<_>>();
238 if !lifetimes.is_empty() {
239 return format!("<{}>", lifetimes.join(", "));
247 pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
249 "`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
252 declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
254 impl EarlyLintPass for LintPassImpl {
255 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
256 if let ast::ItemKind::Impl(box ast::Impl { of_trait: Some(lint_pass), .. }) = &item.kind {
257 if let Some(last) = lint_pass.path.segments.last() {
258 if last.ident.name == sym::LintPass {
259 let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
260 let call_site = expn_data.call_site;
261 if expn_data.kind != ExpnKind::Macro(MacroKind::Bang, sym::impl_lint_pass)
262 && call_site.ctxt().outer_expn_data().kind
263 != ExpnKind::Macro(MacroKind::Bang, sym::declare_lint_pass)
266 LINT_PASS_IMPL_WITHOUT_MACRO,
269 lint.build("implementing `LintPass` by hand")
270 .help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")
282 pub rustc::EXISTING_DOC_KEYWORD,
284 "Check that documented keywords in std and core actually exist",
285 report_in_external_macro: true
288 declare_lint_pass!(ExistingDocKeyword => [EXISTING_DOC_KEYWORD]);
290 fn is_doc_keyword(s: Symbol) -> bool {
294 impl<'tcx> LateLintPass<'tcx> for ExistingDocKeyword {
295 fn check_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::Item<'_>) {
296 for attr in cx.tcx.hir().attrs(item.hir_id()) {
297 if !attr.has_name(sym::doc) {
300 if let Some(list) = attr.meta_item_list() {
302 if nested.has_name(sym::keyword) {
305 .expect("#[doc(keyword = \"...\")] expected a value!");
306 if is_doc_keyword(v) {
309 cx.struct_span_lint(EXISTING_DOC_KEYWORD, attr.span, |lint| {
311 "Found non-existing keyword `{}` used in \
312 `#[doc(keyword = \"...\")]`",
315 .help("only existing keywords are allowed in core/std")