1use rustc_ast::{Pat, PatKind, Path};
5use rustc_hir::def::Res;
6use rustc_hir::def_id::DefId;
7use rustc_hir::{Expr, ExprKind, HirId, find_attr};
8use rustc_middle::ty::{self, GenericArgsRef, PredicatePolarity};
9use rustc_session::{declare_lint_pass, declare_tool_lint};
10use rustc_span::hygiene::{ExpnKind, MacroKind};
11use rustc_span::{Span, sym};
12use {rustc_ast as ast, rustc_hir as hir};
13
14use crate::lints::{
15 AttributeKindInFindAttr, BadOptAccessDiag, DefaultHashTypesDiag,
16 ImplicitSysrootCrateImportDiag, LintPassByHand, NonGlobImportTypeIrInherent, QueryInstability,
17 QueryUntracked, SpanUseEqCtxtDiag, SymbolInternStringLiteralDiag, TyQualified, TykindDiag,
18 TykindKind, TypeIrDirectUse, TypeIrInherentUsage, TypeIrTraitUsage,
19};
20use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
21
22#[doc =
r" The `default_hash_type` lint detects use of [`std::collections::HashMap`] and"]
#[doc =
r" [`std::collections::HashSet`], suggesting the use of `FxHashMap`/`FxHashSet`."]
#[doc = r""]
#[doc =
r" This can help as `FxHasher` can perform better than the default hasher. DOS protection is"]
#[doc = r" not required as input is assumed to be trusted."]
pub static DEFAULT_HASH_TYPES: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::DEFAULT_HASH_TYPES",
default_level: ::rustc_lint_defs::Allow,
desc: "forbid HashMap and HashSet and suggest the FxHash* variants",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
23 pub rustc::DEFAULT_HASH_TYPES,
29 Allow,
30 "forbid HashMap and HashSet and suggest the FxHash* variants",
31 report_in_external_macro: true
32}
33
34pub struct DefaultHashTypes;
#[automatically_derived]
impl ::core::marker::Copy for DefaultHashTypes { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for DefaultHashTypes { }
#[automatically_derived]
impl ::core::clone::Clone for DefaultHashTypes {
#[inline]
fn clone(&self) -> DefaultHashTypes { *self }
}
impl ::rustc_lint_defs::LintPass for DefaultHashTypes {
fn name(&self) -> &'static str { "DefaultHashTypes" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[DEFAULT_HASH_TYPES]))
}
}
impl DefaultHashTypes {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[DEFAULT_HASH_TYPES]))
}
}declare_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
35
36impl LateLintPass<'_> for DefaultHashTypes {
37 fn check_path(&mut self, cx: &LateContext<'_>, path: &hir::Path<'_>, hir_id: HirId) {
38 let Res::Def(rustc_hir::def::DefKind::Struct, def_id) = path.res else { return };
39 if #[allow(non_exhaustive_omitted_patterns)] match cx.tcx.hir_node(hir_id) {
hir::Node::Item(hir::Item { kind: hir::ItemKind::Use(..), .. }) => true,
_ => false,
}matches!(
40 cx.tcx.hir_node(hir_id),
41 hir::Node::Item(hir::Item { kind: hir::ItemKind::Use(..), .. })
42 ) {
43 return;
45 }
46 let preferred = match cx.tcx.get_diagnostic_name(def_id) {
47 Some(sym::HashMap) => "FxHashMap",
48 Some(sym::HashSet) => "FxHashSet",
49 _ => return,
50 };
51 cx.emit_span_lint(
52 DEFAULT_HASH_TYPES,
53 path.span,
54 DefaultHashTypesDiag { preferred, used: cx.tcx.item_name(def_id) },
55 );
56 }
57}
58
59#[doc =
r" The `potential_query_instability` lint detects use of methods which can lead to"]
#[doc = r" potential query instability, such as iterating over a `HashMap`."]
#[doc = r""]
#[doc =
r" Due to the [incremental compilation](https://rustc-dev-guide.rust-lang.org/queries/incremental-compilation.html) model,"]
#[doc =
r" queries must return deterministic, stable results. `HashMap` iteration order can change"]
#[doc =
r" between compilations, and will introduce instability if query results expose the order."]
pub static POTENTIAL_QUERY_INSTABILITY: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::POTENTIAL_QUERY_INSTABILITY",
default_level: ::rustc_lint_defs::Allow,
desc: "require explicit opt-in when using potentially unstable methods or functions",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
60 pub rustc::POTENTIAL_QUERY_INSTABILITY,
67 Allow,
68 "require explicit opt-in when using potentially unstable methods or functions",
69 report_in_external_macro: true
70}
71
72#[doc =
r" The `untracked_query_information` lint detects use of methods which leak information not"]
#[doc =
r" tracked by the query system, such as whether a `Steal<T>` value has already been stolen. In"]
#[doc =
r" order not to break incremental compilation, such methods must be used very carefully or not"]
#[doc = r" at all."]
pub static UNTRACKED_QUERY_INFORMATION: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::UNTRACKED_QUERY_INFORMATION",
default_level: ::rustc_lint_defs::Allow,
desc: "require explicit opt-in when accessing information not tracked by the query system",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
73 pub rustc::UNTRACKED_QUERY_INFORMATION,
78 Allow,
79 "require explicit opt-in when accessing information not tracked by the query system",
80 report_in_external_macro: true
81}
82
83pub struct QueryStability;
#[automatically_derived]
impl ::core::marker::Copy for QueryStability { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for QueryStability { }
#[automatically_derived]
impl ::core::clone::Clone for QueryStability {
#[inline]
fn clone(&self) -> QueryStability { *self }
}
impl ::rustc_lint_defs::LintPass for QueryStability {
fn name(&self) -> &'static str { "QueryStability" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[POTENTIAL_QUERY_INSTABILITY, UNTRACKED_QUERY_INFORMATION]))
}
}
impl QueryStability {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[POTENTIAL_QUERY_INSTABILITY, UNTRACKED_QUERY_INFORMATION]))
}
}declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY, UNTRACKED_QUERY_INFORMATION]);
84
85impl<'tcx> LateLintPass<'tcx> for QueryStability {
86 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
87 if let Some((callee_def_id, span, generic_args, _recv, _args)) =
88 get_callee_span_generic_args_and_args(cx, expr)
89 && let Ok(Some(instance)) =
90 ty::Instance::try_resolve(cx.tcx, cx.typing_env(), callee_def_id, generic_args)
91 {
92 let def_id = instance.def_id();
93 if {
#[allow(deprecated)]
{
{
'done:
{
for i in cx.tcx.get_all_attrs(def_id) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcLintQueryInstability) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}.is_some()find_attr!(cx.tcx, def_id, RustcLintQueryInstability) {
94 cx.emit_span_lint(
95 POTENTIAL_QUERY_INSTABILITY,
96 span,
97 QueryInstability { query: cx.tcx.item_name(def_id) },
98 );
99 } else if has_unstable_into_iter_predicate(cx, callee_def_id, generic_args) {
100 let call_span = span.with_hi(expr.span.hi());
101 cx.emit_span_lint(
102 POTENTIAL_QUERY_INSTABILITY,
103 call_span,
104 QueryInstability { query: sym::into_iter },
105 );
106 }
107
108 if {
#[allow(deprecated)]
{
{
'done:
{
for i in cx.tcx.get_all_attrs(def_id) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcLintUntrackedQueryInformation)
=> {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}.is_some()find_attr!(cx.tcx, def_id, RustcLintUntrackedQueryInformation) {
109 cx.emit_span_lint(
110 UNTRACKED_QUERY_INFORMATION,
111 span,
112 QueryUntracked { method: cx.tcx.item_name(def_id) },
113 );
114 }
115 }
116 }
117}
118
119fn has_unstable_into_iter_predicate<'tcx>(
120 cx: &LateContext<'tcx>,
121 callee_def_id: DefId,
122 generic_args: GenericArgsRef<'tcx>,
123) -> bool {
124 let Some(into_iterator_def_id) = cx.tcx.get_diagnostic_item(sym::IntoIterator) else {
125 return false;
126 };
127 let Some(into_iter_fn_def_id) = cx.tcx.lang_items().into_iter_fn() else {
128 return false;
129 };
130 let predicates = cx.tcx.predicates_of(callee_def_id).instantiate(cx.tcx, generic_args);
131 for (predicate, _) in predicates {
132 let Some(trait_pred) = predicate.as_trait_clause() else {
133 continue;
134 };
135 if trait_pred.def_id() != into_iterator_def_id
136 || trait_pred.polarity() != PredicatePolarity::Positive
137 {
138 continue;
139 }
140 let into_iter_fn_args =
142 cx.tcx.instantiate_bound_regions_with_erased(trait_pred).trait_ref.args;
143 let Ok(Some(instance)) = ty::Instance::try_resolve(
144 cx.tcx,
145 cx.typing_env(),
146 into_iter_fn_def_id,
147 into_iter_fn_args,
148 ) else {
149 continue;
150 };
151 if {
#[allow(deprecated)]
{
{
'done:
{
for i in cx.tcx.get_all_attrs(instance.def_id()) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcLintQueryInstability) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}.is_some()find_attr!(cx.tcx, instance.def_id(), RustcLintQueryInstability) {
154 return true;
155 }
156 }
157 false
158}
159
160fn get_callee_span_generic_args_and_args<'tcx>(
164 cx: &LateContext<'tcx>,
165 expr: &'tcx Expr<'tcx>,
166) -> Option<(DefId, Span, GenericArgsRef<'tcx>, Option<&'tcx Expr<'tcx>>, &'tcx [Expr<'tcx>])> {
167 if let ExprKind::Call(callee, args) = expr.kind
168 && let callee_ty = cx.typeck_results().expr_ty(callee)
169 && let ty::FnDef(callee_def_id, generic_args) = callee_ty.kind()
170 {
171 return Some((*callee_def_id, callee.span, generic_args, None, args));
172 }
173 if let ExprKind::MethodCall(segment, recv, args, _) = expr.kind
174 && let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
175 {
176 let generic_args = cx.typeck_results().node_args(expr.hir_id);
177 return Some((method_def_id, segment.ident.span, generic_args, Some(recv), args));
178 }
179 None
180}
181
182#[doc =
r" The `usage_of_ty_tykind` lint detects usages of `ty::TyKind::<kind>`,"]
#[doc = r" where `ty::<kind>` would suffice."]
pub static USAGE_OF_TY_TYKIND: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::USAGE_OF_TY_TYKIND",
default_level: ::rustc_lint_defs::Allow,
desc: "usage of `ty::TyKind` outside of the `ty::sty` module",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
183 pub rustc::USAGE_OF_TY_TYKIND,
186 Allow,
187 "usage of `ty::TyKind` outside of the `ty::sty` module",
188 report_in_external_macro: true
189}
190
191#[doc = r" The `usage_of_qualified_ty` lint detects usages of `ty::TyKind`,"]
#[doc = r" where `Ty` should be used instead."]
pub static USAGE_OF_QUALIFIED_TY: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::USAGE_OF_QUALIFIED_TY",
default_level: ::rustc_lint_defs::Allow,
desc: "using `ty::{Ty,TyCtxt}` instead of importing it",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
192 pub rustc::USAGE_OF_QUALIFIED_TY,
195 Allow,
196 "using `ty::{Ty,TyCtxt}` instead of importing it",
197 report_in_external_macro: true
198}
199
200pub struct TyTyKind;
#[automatically_derived]
impl ::core::marker::Copy for TyTyKind { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for TyTyKind { }
#[automatically_derived]
impl ::core::clone::Clone for TyTyKind {
#[inline]
fn clone(&self) -> TyTyKind { *self }
}
impl ::rustc_lint_defs::LintPass for TyTyKind {
fn name(&self) -> &'static str { "TyTyKind" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[USAGE_OF_TY_TYKIND, USAGE_OF_QUALIFIED_TY]))
}
}
impl TyTyKind {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[USAGE_OF_TY_TYKIND, USAGE_OF_QUALIFIED_TY]))
}
}declare_lint_pass!(TyTyKind => [
201 USAGE_OF_TY_TYKIND,
202 USAGE_OF_QUALIFIED_TY,
203]);
204
205impl<'tcx> LateLintPass<'tcx> for TyTyKind {
206 fn check_path(
207 &mut self,
208 cx: &LateContext<'tcx>,
209 path: &rustc_hir::Path<'tcx>,
210 _: rustc_hir::HirId,
211 ) {
212 if let Some(segment) = path.segments.iter().nth_back(1)
213 && lint_ty_kind_usage(cx, &segment.res)
214 {
215 let span =
216 path.span.with_hi(segment.args.map_or(segment.ident.span, |a| a.span_ext).hi());
217 cx.emit_span_lint(USAGE_OF_TY_TYKIND, path.span, TykindKind { suggestion: span });
218 }
219 }
220
221 fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx, hir::AmbigArg>) {
222 match &ty.kind {
223 hir::TyKind::Path(hir::QPath::Resolved(_, path)) => {
224 if lint_ty_kind_usage(cx, &path.res) {
225 let span = match cx.tcx.parent_hir_node(ty.hir_id) {
226 hir::Node::PatExpr(hir::PatExpr {
227 kind: hir::PatExprKind::Path(qpath),
228 ..
229 })
230 | hir::Node::Pat(hir::Pat {
231 kind:
232 hir::PatKind::TupleStruct(qpath, ..) | hir::PatKind::Struct(qpath, ..),
233 ..
234 })
235 | hir::Node::Expr(
236 hir::Expr { kind: hir::ExprKind::Path(qpath), .. }
237 | &hir::Expr { kind: hir::ExprKind::Struct(qpath, ..), .. },
238 ) => {
239 if let hir::QPath::TypeRelative(qpath_ty, ..) = qpath
240 && qpath_ty.hir_id == ty.hir_id
241 {
242 Some(path.span)
243 } else {
244 None
245 }
246 }
247 _ => None,
248 };
249
250 match span {
251 Some(span) => {
252 cx.emit_span_lint(
253 USAGE_OF_TY_TYKIND,
254 path.span,
255 TykindKind { suggestion: span },
256 );
257 }
258 None => cx.emit_span_lint(USAGE_OF_TY_TYKIND, path.span, TykindDiag),
259 }
260 } else if !ty.span.from_expansion()
261 && path.segments.len() > 1
262 && let Some(ty) = is_ty_or_ty_ctxt(cx, path)
263 {
264 cx.emit_span_lint(
265 USAGE_OF_QUALIFIED_TY,
266 path.span,
267 TyQualified { ty, suggestion: path.span },
268 );
269 }
270 }
271 _ => {}
272 }
273 }
274}
275
276fn lint_ty_kind_usage(cx: &LateContext<'_>, res: &Res) -> bool {
277 if let Some(did) = res.opt_def_id() {
278 cx.tcx.is_diagnostic_item(sym::TyKind, did) || cx.tcx.is_diagnostic_item(sym::IrTyKind, did)
279 } else {
280 false
281 }
282}
283
284fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &hir::Path<'_>) -> Option<String> {
285 match path.res {
286 Res::Def(_, def_id) => {
287 if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(def_id) {
288 return Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", name,
gen_args(path.segments.last().unwrap())))
})format!("{}{}", name, gen_args(path.segments.last().unwrap())));
289 }
290 }
291 Res::SelfTyAlias { alias_to: did, is_trait_impl: false, .. } => {
293 if let ty::Adt(adt, args) = cx.tcx.type_of(did).instantiate_identity().kind()
294 && let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(adt.did())
295 {
296 return Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}<{1}>", name, args[0]))
})format!("{}<{}>", name, args[0]));
297 }
298 }
299 _ => (),
300 }
301
302 None
303}
304
305fn gen_args(segment: &hir::PathSegment<'_>) -> String {
306 if let Some(args) = &segment.args {
307 let lifetimes = args
308 .args
309 .iter()
310 .filter_map(|arg| {
311 if let hir::GenericArg::Lifetime(lt) = arg {
312 Some(lt.ident.to_string())
313 } else {
314 None
315 }
316 })
317 .collect::<Vec<_>>();
318
319 if !lifetimes.is_empty() {
320 return ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}>", lifetimes.join(", ")))
})format!("<{}>", lifetimes.join(", "));
321 }
322 }
323
324 String::new()
325}
326
327#[doc = r" The `non_glob_import_of_type_ir_inherent_item` lint detects"]
#[doc = r" non-glob imports of module `rustc_type_ir::inherent`."]
pub static NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT",
default_level: ::rustc_lint_defs::Allow,
desc: "non-glob import of `rustc_type_ir::inherent`",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
328 pub rustc::NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT,
331 Allow,
332 "non-glob import of `rustc_type_ir::inherent`",
333 report_in_external_macro: true
334}
335
336#[doc =
r" The `usage_of_type_ir_inherent` lint detects usage of `rustc_type_ir::inherent`."]
#[doc = r""]
#[doc = r" This module should only be used within the trait solver."]
pub static USAGE_OF_TYPE_IR_INHERENT: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::USAGE_OF_TYPE_IR_INHERENT",
default_level: ::rustc_lint_defs::Allow,
desc: "usage `rustc_type_ir::inherent` outside of trait system",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
337 pub rustc::USAGE_OF_TYPE_IR_INHERENT,
341 Allow,
342 "usage `rustc_type_ir::inherent` outside of trait system",
343 report_in_external_macro: true
344}
345
346#[doc =
r" The `usage_of_type_ir_traits` lint detects usage of `rustc_type_ir::Interner`,"]
#[doc = r" or `rustc_infer::InferCtxtLike`."]
#[doc = r""]
#[doc =
r" Methods of this trait should only be used within the type system abstraction layer,"]
#[doc =
r" and in the generic next trait solver implementation. Look for an analogously named"]
#[doc = r" method on `TyCtxt` or `InferCtxt` (respectively)."]
pub static USAGE_OF_TYPE_IR_TRAITS: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::USAGE_OF_TYPE_IR_TRAITS",
default_level: ::rustc_lint_defs::Allow,
desc: "usage `rustc_type_ir`-specific abstraction traits outside of trait system",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
347 pub rustc::USAGE_OF_TYPE_IR_TRAITS,
354 Allow,
355 "usage `rustc_type_ir`-specific abstraction traits outside of trait system",
356 report_in_external_macro: true
357}
358#[doc =
r" The `direct_use_of_rustc_type_ir` lint detects usage of `rustc_type_ir`."]
#[doc = r""]
#[doc =
r" This module should only be used within the trait solver and some desirable"]
#[doc = r" crates like rustc_middle."]
pub static DIRECT_USE_OF_RUSTC_TYPE_IR: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::DIRECT_USE_OF_RUSTC_TYPE_IR",
default_level: ::rustc_lint_defs::Allow,
desc: "usage `rustc_type_ir` abstraction outside of trait system",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
359 pub rustc::DIRECT_USE_OF_RUSTC_TYPE_IR,
364 Allow,
365 "usage `rustc_type_ir` abstraction outside of trait system",
366 report_in_external_macro: true
367}
368
369pub struct TypeIr;
#[automatically_derived]
impl ::core::marker::Copy for TypeIr { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for TypeIr { }
#[automatically_derived]
impl ::core::clone::Clone for TypeIr {
#[inline]
fn clone(&self) -> TypeIr { *self }
}
impl ::rustc_lint_defs::LintPass for TypeIr {
fn name(&self) -> &'static str { "TypeIr" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[DIRECT_USE_OF_RUSTC_TYPE_IR,
NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT,
USAGE_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_TRAITS]))
}
}
impl TypeIr {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[DIRECT_USE_OF_RUSTC_TYPE_IR,
NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT,
USAGE_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_TRAITS]))
}
}declare_lint_pass!(TypeIr => [DIRECT_USE_OF_RUSTC_TYPE_IR, NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_TRAITS]);
370
371impl<'tcx> LateLintPass<'tcx> for TypeIr {
372 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
373 let res_def_id = match expr.kind {
374 hir::ExprKind::Path(hir::QPath::Resolved(_, path)) => path.res.opt_def_id(),
375 hir::ExprKind::Path(hir::QPath::TypeRelative(..)) | hir::ExprKind::MethodCall(..) => {
376 cx.typeck_results().type_dependent_def_id(expr.hir_id)
377 }
378 _ => return,
379 };
380 let Some(res_def_id) = res_def_id else {
381 return;
382 };
383 if let Some(assoc_item) = cx.tcx.opt_associated_item(res_def_id)
384 && let Some(trait_def_id) = assoc_item.trait_container(cx.tcx)
385 && (cx.tcx.is_diagnostic_item(sym::type_ir_interner, trait_def_id)
386 | cx.tcx.is_diagnostic_item(sym::type_ir_infer_ctxt_like, trait_def_id))
387 {
388 cx.emit_span_lint(USAGE_OF_TYPE_IR_TRAITS, expr.span, TypeIrTraitUsage);
389 }
390 }
391
392 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
393 let rustc_hir::ItemKind::Use(path, kind) = item.kind else { return };
394
395 let is_mod_inherent = |res: Res| {
396 res.opt_def_id()
397 .is_some_and(|def_id| cx.tcx.is_diagnostic_item(sym::type_ir_inherent, def_id))
398 };
399
400 if let Some(seg) = path.segments.iter().find(|seg| is_mod_inherent(seg.res)) {
402 cx.emit_span_lint(USAGE_OF_TYPE_IR_INHERENT, seg.ident.span, TypeIrInherentUsage);
403 }
404 else if let Some(type_ns) = path.res.type_ns
406 && is_mod_inherent(type_ns)
407 {
408 cx.emit_span_lint(
409 USAGE_OF_TYPE_IR_INHERENT,
410 path.segments.last().unwrap().ident.span,
411 TypeIrInherentUsage,
412 );
413 }
414
415 let (lo, hi, snippet) = match path.segments {
416 [.., penultimate, segment] if is_mod_inherent(penultimate.res) => {
417 (segment.ident.span, item.kind.ident().unwrap().span, "*")
418 }
419 [.., segment]
420 if let Some(type_ns) = path.res.type_ns
421 && is_mod_inherent(type_ns)
422 && let rustc_hir::UseKind::Single(ident) = kind =>
423 {
424 let (lo, snippet) =
425 match cx.tcx.sess.source_map().span_to_snippet(path.span).as_deref() {
426 Ok("self") => (path.span, "*"),
427 _ => (segment.ident.span.shrink_to_hi(), "::*"),
428 };
429 (lo, if segment.ident == ident { lo } else { ident.span }, snippet)
430 }
431 _ => return,
432 };
433 cx.emit_span_lint(
434 NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT,
435 path.span,
436 NonGlobImportTypeIrInherent { suggestion: lo.eq_ctxt(hi).then(|| lo.to(hi)), snippet },
437 );
438 }
439
440 fn check_path(
441 &mut self,
442 cx: &LateContext<'tcx>,
443 path: &rustc_hir::Path<'tcx>,
444 _: rustc_hir::HirId,
445 ) {
446 if let Some(seg) = path.segments.iter().find(|seg| {
447 seg.res
448 .opt_def_id()
449 .is_some_and(|def_id| cx.tcx.is_diagnostic_item(sym::type_ir, def_id))
450 }) {
451 cx.emit_span_lint(DIRECT_USE_OF_RUSTC_TYPE_IR, seg.ident.span, TypeIrDirectUse);
452 }
453 }
454}
455
456#[doc =
r" The `lint_pass_impl_without_macro` detects manual implementations of a lint"]
#[doc = r" pass, without using [`declare_lint_pass`] or [`impl_lint_pass`]."]
pub static LINT_PASS_IMPL_WITHOUT_MACRO: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::LINT_PASS_IMPL_WITHOUT_MACRO",
default_level: ::rustc_lint_defs::Allow,
desc: "`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros",
edition_lint_opts: None,
report_in_external_macro: false,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
457 pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
460 Allow,
461 "`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
462}
463
464pub struct LintPassImpl;
#[automatically_derived]
impl ::core::marker::Copy for LintPassImpl { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for LintPassImpl { }
#[automatically_derived]
impl ::core::clone::Clone for LintPassImpl {
#[inline]
fn clone(&self) -> LintPassImpl { *self }
}
impl ::rustc_lint_defs::LintPass for LintPassImpl {
fn name(&self) -> &'static str { "LintPassImpl" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[LINT_PASS_IMPL_WITHOUT_MACRO]))
}
}
impl LintPassImpl {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[LINT_PASS_IMPL_WITHOUT_MACRO]))
}
}declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
465
466impl EarlyLintPass for LintPassImpl {
467 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
468 if let ast::ItemKind::Impl(ast::Impl { of_trait: Some(of_trait), .. }) = &item.kind
469 && let Some(last) = of_trait.trait_ref.path.segments.last()
470 && last.ident.name == sym::LintPass
471 {
472 let expn_data = of_trait.trait_ref.path.span.ctxt().outer_expn_data();
473 let call_site = expn_data.call_site;
474 if expn_data.kind != ExpnKind::Macro(MacroKind::Bang, sym::impl_lint_pass)
475 && call_site.ctxt().outer_expn_data().kind
476 != ExpnKind::Macro(MacroKind::Bang, sym::declare_lint_pass)
477 {
478 cx.emit_span_lint(
479 LINT_PASS_IMPL_WITHOUT_MACRO,
480 of_trait.trait_ref.path.span,
481 LintPassByHand,
482 );
483 }
484 }
485 }
486}
487
488#[doc =
r" The `bad_opt_access` lint detects accessing options by field instead of"]
#[doc = r" the wrapper function."]
pub static BAD_OPT_ACCESS: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::BAD_OPT_ACCESS",
default_level: ::rustc_lint_defs::Deny,
desc: "prevent using options by field access when there is a wrapper function",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
489 pub rustc::BAD_OPT_ACCESS,
492 Deny,
493 "prevent using options by field access when there is a wrapper function",
494 report_in_external_macro: true
495}
496
497pub struct BadOptAccess;
#[automatically_derived]
impl ::core::marker::Copy for BadOptAccess { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for BadOptAccess { }
#[automatically_derived]
impl ::core::clone::Clone for BadOptAccess {
#[inline]
fn clone(&self) -> BadOptAccess { *self }
}
impl ::rustc_lint_defs::LintPass for BadOptAccess {
fn name(&self) -> &'static str { "BadOptAccess" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[BAD_OPT_ACCESS]))
}
}
impl BadOptAccess {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[BAD_OPT_ACCESS]))
}
}declare_lint_pass!(BadOptAccess => [BAD_OPT_ACCESS]);
498
499impl LateLintPass<'_> for BadOptAccess {
500 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
501 let hir::ExprKind::Field(base, target) = expr.kind else { return };
502 let Some(adt_def) = cx.typeck_results().expr_ty(base).ty_adt_def() else { return };
503 if !{
#[allow(deprecated)]
{
{
'done:
{
for i in cx.tcx.get_all_attrs(adt_def.did()) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcLintOptTy) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}.is_some()find_attr!(cx.tcx, adt_def.did(), RustcLintOptTy) {
506 return;
507 }
508
509 for field in adt_def.all_fields() {
510 if field.name == target.name
511 && let Some(lint_message) = {
#[allow(deprecated)]
{
{
'done:
{
for i in cx.tcx.get_all_attrs(field.did) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcLintOptDenyFieldAccess {
lint_message }) => {
break 'done Some(lint_message);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}find_attr!(cx.tcx, field.did, RustcLintOptDenyFieldAccess { lint_message, } => lint_message)
512 {
513 cx.emit_span_lint(
514 BAD_OPT_ACCESS,
515 expr.span,
516 BadOptAccessDiag { msg: lint_message.as_str() },
517 );
518 }
519 }
520 }
521}
522
523pub static SPAN_USE_EQ_CTXT: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::SPAN_USE_EQ_CTXT",
default_level: ::rustc_lint_defs::Allow,
desc: "forbid uses of `==` with `Span::ctxt`, suggest `Span::eq_ctxt` instead",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
524 pub rustc::SPAN_USE_EQ_CTXT,
525 Allow,
526 "forbid uses of `==` with `Span::ctxt`, suggest `Span::eq_ctxt` instead",
527 report_in_external_macro: true
528}
529
530pub struct SpanUseEqCtxt;
#[automatically_derived]
impl ::core::marker::Copy for SpanUseEqCtxt { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for SpanUseEqCtxt { }
#[automatically_derived]
impl ::core::clone::Clone for SpanUseEqCtxt {
#[inline]
fn clone(&self) -> SpanUseEqCtxt { *self }
}
impl ::rustc_lint_defs::LintPass for SpanUseEqCtxt {
fn name(&self) -> &'static str { "SpanUseEqCtxt" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[SPAN_USE_EQ_CTXT]))
}
}
impl SpanUseEqCtxt {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[SPAN_USE_EQ_CTXT]))
}
}declare_lint_pass!(SpanUseEqCtxt => [SPAN_USE_EQ_CTXT]);
531
532impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
533 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) {
534 if let hir::ExprKind::Binary(
535 hir::BinOp { node: hir::BinOpKind::Eq | hir::BinOpKind::Ne, .. },
536 lhs,
537 rhs,
538 ) = expr.kind
539 {
540 if is_span_ctxt_call(cx, lhs) && is_span_ctxt_call(cx, rhs) {
541 cx.emit_span_lint(SPAN_USE_EQ_CTXT, expr.span, SpanUseEqCtxtDiag);
542 }
543 }
544 }
545}
546
547fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
548 match &expr.kind {
549 hir::ExprKind::MethodCall(..) => cx
550 .typeck_results()
551 .type_dependent_def_id(expr.hir_id)
552 .is_some_and(|call_did| cx.tcx.is_diagnostic_item(sym::SpanCtxt, call_did)),
553
554 _ => false,
555 }
556}
557
558#[doc =
r" The `symbol_intern_string_literal` detects `Symbol::intern` being called on a string literal"]
pub static SYMBOL_INTERN_STRING_LITERAL: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::SYMBOL_INTERN_STRING_LITERAL",
default_level: ::rustc_lint_defs::Allow,
desc: "Forbid uses of string literals in `Symbol::intern`, suggesting preinterning instead",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
559 pub rustc::SYMBOL_INTERN_STRING_LITERAL,
561 Allow,
564 "Forbid uses of string literals in `Symbol::intern`, suggesting preinterning instead",
565 report_in_external_macro: true
566}
567
568pub struct SymbolInternStringLiteral;
#[automatically_derived]
impl ::core::marker::Copy for SymbolInternStringLiteral { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for SymbolInternStringLiteral { }
#[automatically_derived]
impl ::core::clone::Clone for SymbolInternStringLiteral {
#[inline]
fn clone(&self) -> SymbolInternStringLiteral { *self }
}
impl ::rustc_lint_defs::LintPass for SymbolInternStringLiteral {
fn name(&self) -> &'static str { "SymbolInternStringLiteral" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[SYMBOL_INTERN_STRING_LITERAL]))
}
}
impl SymbolInternStringLiteral {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[SYMBOL_INTERN_STRING_LITERAL]))
}
}declare_lint_pass!(SymbolInternStringLiteral => [SYMBOL_INTERN_STRING_LITERAL]);
569
570impl<'tcx> LateLintPass<'tcx> for SymbolInternStringLiteral {
571 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
572 if let hir::ExprKind::Call(path, [arg]) = expr.kind
573 && let hir::ExprKind::Path(ref qpath) = path.kind
574 && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
575 && cx.tcx.is_diagnostic_item(sym::SymbolIntern, def_id)
576 && let hir::ExprKind::Lit(kind) = arg.kind
577 && let rustc_ast::LitKind::Str(_, _) = kind.node
578 {
579 cx.emit_span_lint(
580 SYMBOL_INTERN_STRING_LITERAL,
581 kind.span,
582 SymbolInternStringLiteralDiag,
583 );
584 }
585 }
586}
587
588#[doc =
r" The `implicit_sysroot_crate_import` detects use of `extern crate` to import non-sysroot crates"]
#[doc =
r" (e.g. crates.io deps) from the sysroot, which is dangerous because these crates are not guaranteed"]
#[doc =
r" to exist exactly once, and so may be missing entirely or appear multiple times resulting in ambiguity."]
pub static IMPLICIT_SYSROOT_CRATE_IMPORT: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::IMPLICIT_SYSROOT_CRATE_IMPORT",
default_level: ::rustc_lint_defs::Allow,
desc: "Forbid uses of non-sysroot crates in `extern crate`",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
589 pub rustc::IMPLICIT_SYSROOT_CRATE_IMPORT,
593 Allow,
594 "Forbid uses of non-sysroot crates in `extern crate`",
595 report_in_external_macro: true
596}
597
598pub struct ImplicitSysrootCrateImport;
#[automatically_derived]
impl ::core::marker::Copy for ImplicitSysrootCrateImport { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for ImplicitSysrootCrateImport { }
#[automatically_derived]
impl ::core::clone::Clone for ImplicitSysrootCrateImport {
#[inline]
fn clone(&self) -> ImplicitSysrootCrateImport { *self }
}
impl ::rustc_lint_defs::LintPass for ImplicitSysrootCrateImport {
fn name(&self) -> &'static str { "ImplicitSysrootCrateImport" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[IMPLICIT_SYSROOT_CRATE_IMPORT]))
}
}
impl ImplicitSysrootCrateImport {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[IMPLICIT_SYSROOT_CRATE_IMPORT]))
}
}declare_lint_pass!(ImplicitSysrootCrateImport => [IMPLICIT_SYSROOT_CRATE_IMPORT]);
599
600impl EarlyLintPass for ImplicitSysrootCrateImport {
601 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
602 fn is_whitelisted(crate_name: &str) -> bool {
603 crate_name.starts_with("rustc_")
605 || #[allow(non_exhaustive_omitted_patterns)] match crate_name {
"test" | "self" | "core" | "alloc" | "std" | "proc_macro" |
"tikv_jemalloc_sys" => true,
_ => false,
}matches!(
606 crate_name,
607 "test" | "self" | "core" | "alloc" | "std" | "proc_macro" | "tikv_jemalloc_sys"
608 )
609 }
610
611 if let ast::ItemKind::ExternCrate(original_name, imported_name) = &item.kind {
612 let name = original_name.as_ref().unwrap_or(&imported_name.name).as_str();
613 let externs = &cx.builder.sess().opts.externs;
614 if externs.get(name).is_none() && !is_whitelisted(name) {
615 cx.emit_span_lint(
616 IMPLICIT_SYSROOT_CRATE_IMPORT,
617 item.span,
618 ImplicitSysrootCrateImportDiag { name },
619 );
620 }
621 }
622 }
623}
624
625pub static BAD_USE_OF_FIND_ATTR: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: &"rustc::BAD_USE_OF_FIND_ATTR",
default_level: ::rustc_lint_defs::Allow,
desc: "Forbid `AttributeKind::` as a prefix in `find_attr!` macros.",
edition_lint_opts: None,
report_in_external_macro: true,
future_incompatible: None,
is_externally_loaded: true,
crate_level_only: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_tool_lint! {
626 pub rustc::BAD_USE_OF_FIND_ATTR,
627 Allow,
628 "Forbid `AttributeKind::` as a prefix in `find_attr!` macros.",
629 report_in_external_macro: true
630}
631pub struct BadUseOfFindAttr;
#[automatically_derived]
impl ::core::marker::Copy for BadUseOfFindAttr { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for BadUseOfFindAttr { }
#[automatically_derived]
impl ::core::clone::Clone for BadUseOfFindAttr {
#[inline]
fn clone(&self) -> BadUseOfFindAttr { *self }
}
impl ::rustc_lint_defs::LintPass for BadUseOfFindAttr {
fn name(&self) -> &'static str { "BadUseOfFindAttr" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[BAD_USE_OF_FIND_ATTR]))
}
}
impl BadUseOfFindAttr {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[BAD_USE_OF_FIND_ATTR]))
}
}declare_lint_pass!(BadUseOfFindAttr => [BAD_USE_OF_FIND_ATTR]);
632
633impl EarlyLintPass for BadUseOfFindAttr {
634 fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &rustc_ast::Arm) {
635 fn path_contains_attribute_kind(cx: &EarlyContext<'_>, path: &Path) {
636 for segment in &path.segments {
637 if segment.ident.as_str() == "AttributeKind" {
638 cx.emit_span_lint(
639 BAD_USE_OF_FIND_ATTR,
640 segment.span(),
641 AttributeKindInFindAttr,
642 );
643 }
644 }
645 }
646
647 fn find_attr_kind_in_pat(cx: &EarlyContext<'_>, pat: &Pat) {
648 match &pat.kind {
649 PatKind::Struct(_, path, fields, _) => {
650 path_contains_attribute_kind(cx, path);
651 for field in fields {
652 find_attr_kind_in_pat(cx, &field.pat);
653 }
654 }
655 PatKind::TupleStruct(_, path, fields) => {
656 path_contains_attribute_kind(cx, path);
657 for field in fields {
658 find_attr_kind_in_pat(cx, &field);
659 }
660 }
661 PatKind::Or(options) => {
662 for pat in options {
663 find_attr_kind_in_pat(cx, pat);
664 }
665 }
666 PatKind::Path(_, path) => {
667 path_contains_attribute_kind(cx, path);
668 }
669 PatKind::Tuple(elems) => {
670 for pat in elems {
671 find_attr_kind_in_pat(cx, pat);
672 }
673 }
674 PatKind::Box(pat) => {
675 find_attr_kind_in_pat(cx, pat);
676 }
677 PatKind::Deref(pat) => {
678 find_attr_kind_in_pat(cx, pat);
679 }
680 PatKind::Ref(..) => {
681 find_attr_kind_in_pat(cx, pat);
682 }
683 PatKind::Slice(elems) => {
684 for pat in elems {
685 find_attr_kind_in_pat(cx, pat);
686 }
687 }
688
689 PatKind::Guard(pat, ..) => {
690 find_attr_kind_in_pat(cx, pat);
691 }
692 PatKind::Paren(pat) => {
693 find_attr_kind_in_pat(cx, pat);
694 }
695 PatKind::Expr(..)
696 | PatKind::Range(..)
697 | PatKind::MacCall(..)
698 | PatKind::Rest
699 | PatKind::Missing
700 | PatKind::Err(..)
701 | PatKind::Ident(..)
702 | PatKind::Never
703 | PatKind::Wild => {}
704 }
705 }
706
707 if let Some(expn_data) = arm.span.source_callee()
708 && let ExpnKind::Macro(_, name) = expn_data.kind
709 && name.as_str() == "find_attr"
710 {
711 find_attr_kind_in_pat(cx, &arm.pat);
712 }
713 }
714}