2018-12-06 13:50:04 +01:00
|
|
|
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
|
|
|
|
//! Clippy.
|
|
|
|
|
2025-03-26 03:51:41 +00:00
|
|
|
use rustc_hir::HirId;
|
2020-09-19 12:34:31 +02:00
|
|
|
use rustc_hir::def::Res;
|
2024-07-29 08:13:50 +10:00
|
|
|
use rustc_hir::def_id::DefId;
|
2024-08-10 21:40:07 +03:00
|
|
|
use rustc_middle::ty::{self, GenericArgsRef, Ty as MiddleTy};
|
2021-07-02 14:15:11 -05:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2024-09-22 19:05:04 -04:00
|
|
|
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::{Span, sym};
|
2024-05-22 14:09:17 +10:00
|
|
|
use tracing::debug;
|
2025-03-26 03:51:41 +00:00
|
|
|
use {rustc_ast as ast, rustc_hir as hir};
|
2018-12-06 13:50:04 +01:00
|
|
|
|
2024-07-29 08:13:50 +10:00
|
|
|
use crate::lints::{
|
2024-12-12 13:57:46 +11:00
|
|
|
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand,
|
2024-11-27 17:21:59 +00:00
|
|
|
NonGlobImportTypeIrInherent, QueryInstability, QueryUntracked, SpanUseEqCtxtDiag,
|
|
|
|
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrInherentUsage,
|
2025-03-26 04:09:07 +00:00
|
|
|
TypeIrTraitUsage, UntranslatableDiag,
|
2024-07-29 08:13:50 +10:00
|
|
|
};
|
|
|
|
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
|
|
|
|
2019-06-24 10:43:51 +02:00
|
|
|
declare_tool_lint! {
|
2024-05-07 14:12:37 +10:00
|
|
|
/// The `default_hash_type` lint detects use of [`std::collections::HashMap`] and
|
|
|
|
/// [`std::collections::HashSet`], suggesting the use of `FxHashMap`/`FxHashSet`.
|
2023-03-08 16:55:28 +00:00
|
|
|
///
|
2024-05-07 14:12:37 +10:00
|
|
|
/// This can help as `FxHasher` can perform better than the default hasher. DOS protection is
|
|
|
|
/// not required as input is assumed to be trusted.
|
2019-06-24 10:43:51 +02:00
|
|
|
pub rustc::DEFAULT_HASH_TYPES,
|
2019-03-16 14:59:34 +01:00
|
|
|
Allow,
|
2020-01-03 17:27:14 -08:00
|
|
|
"forbid HashMap and HashSet and suggest the FxHash* variants",
|
|
|
|
report_in_external_macro: true
|
2018-12-06 13:50:04 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 14:15:11 -05:00
|
|
|
declare_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
|
2018-12-06 13:50:04 +01:00
|
|
|
|
2021-07-02 14:15:11 -05:00
|
|
|
impl LateLintPass<'_> for DefaultHashTypes {
|
2025-03-26 03:51:41 +00:00
|
|
|
fn check_path(&mut self, cx: &LateContext<'_>, path: &hir::Path<'_>, hir_id: HirId) {
|
2022-02-19 00:48:49 +01:00
|
|
|
let Res::Def(rustc_hir::def::DefKind::Struct, def_id) = path.res else { return };
|
2025-03-26 03:51:41 +00:00
|
|
|
if matches!(
|
|
|
|
cx.tcx.hir_node(hir_id),
|
|
|
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Use(..), .. })
|
|
|
|
) {
|
2024-05-07 14:12:37 +10:00
|
|
|
// Don't lint imports, only actual usages.
|
2021-07-02 14:15:11 -05:00
|
|
|
return;
|
2018-12-06 13:50:04 +01:00
|
|
|
}
|
2022-10-22 16:32:54 -04:00
|
|
|
let preferred = match cx.tcx.get_diagnostic_name(def_id) {
|
2021-10-04 16:11:22 -05:00
|
|
|
Some(sym::HashMap) => "FxHashMap",
|
|
|
|
Some(sym::HashSet) => "FxHashSet",
|
|
|
|
_ => return,
|
2021-07-02 14:15:11 -05:00
|
|
|
};
|
2025-02-08 22:12:13 +00:00
|
|
|
cx.emit_span_lint(
|
|
|
|
DEFAULT_HASH_TYPES,
|
|
|
|
path.span,
|
|
|
|
DefaultHashTypesDiag { preferred, used: cx.tcx.item_name(def_id) },
|
|
|
|
);
|
2018-12-06 13:50:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:50:06 +01:00
|
|
|
/// Helper function for lints that check for expressions with calls and use typeck results to
|
2023-07-11 22:35:29 +01:00
|
|
|
/// get the `DefId` and `GenericArgsRef` of the function.
|
2022-06-10 15:50:06 +01:00
|
|
|
fn typeck_results_of_method_fn<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
2025-03-26 03:51:41 +00:00
|
|
|
expr: &hir::Expr<'_>,
|
2023-07-11 22:35:29 +01:00
|
|
|
) -> Option<(Span, DefId, ty::GenericArgsRef<'tcx>)> {
|
2022-06-10 15:50:06 +01:00
|
|
|
match expr.kind {
|
2025-03-26 03:51:41 +00:00
|
|
|
hir::ExprKind::MethodCall(segment, ..)
|
2022-06-10 15:50:06 +01:00
|
|
|
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
|
|
|
|
{
|
2023-07-11 22:35:29 +01:00
|
|
|
Some((segment.ident.span, def_id, cx.typeck_results().node_args(expr.hir_id)))
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
2023-10-13 08:58:33 +00:00
|
|
|
_ => match cx.typeck_results().node_type(expr.hir_id).kind() {
|
|
|
|
&ty::FnDef(def_id, args) => Some((expr.span, def_id, args)),
|
|
|
|
_ => None,
|
|
|
|
},
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 13:02:16 +01:00
|
|
|
declare_tool_lint! {
|
2023-03-08 16:55:28 +00:00
|
|
|
/// The `potential_query_instability` lint detects use of methods which can lead to
|
|
|
|
/// potential query instability, such as iterating over a `HashMap`.
|
|
|
|
///
|
|
|
|
/// Due to the [incremental compilation](https://rustc-dev-guide.rust-lang.org/queries/incremental-compilation.html) model,
|
2024-05-07 14:12:37 +10:00
|
|
|
/// queries must return deterministic, stable results. `HashMap` iteration order can change
|
|
|
|
/// between compilations, and will introduce instability if query results expose the order.
|
2022-01-05 13:02:16 +01:00
|
|
|
pub rustc::POTENTIAL_QUERY_INSTABILITY,
|
|
|
|
Allow,
|
|
|
|
"require explicit opt-in when using potentially unstable methods or functions",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
2024-08-09 09:52:12 +02:00
|
|
|
declare_tool_lint! {
|
|
|
|
/// The `untracked_query_information` lint detects use of methods which leak information not
|
|
|
|
/// tracked by the query system, such as whether a `Steal<T>` value has already been stolen. In
|
|
|
|
/// order not to break incremental compilation, such methods must be used very carefully or not
|
|
|
|
/// at all.
|
|
|
|
pub rustc::UNTRACKED_QUERY_INFORMATION,
|
|
|
|
Allow,
|
|
|
|
"require explicit opt-in when accessing information not tracked by the query system",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY, UNTRACKED_QUERY_INFORMATION]);
|
2022-01-05 13:02:16 +01:00
|
|
|
|
|
|
|
impl LateLintPass<'_> for QueryStability {
|
2025-03-26 03:51:41 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
|
2023-07-11 22:35:29 +01:00
|
|
|
let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return };
|
2024-11-15 13:53:31 +01:00
|
|
|
if let Ok(Some(instance)) = ty::Instance::try_resolve(cx.tcx, cx.typing_env(), def_id, args)
|
|
|
|
{
|
2022-01-05 13:02:16 +01:00
|
|
|
let def_id = instance.def_id();
|
|
|
|
if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) {
|
2025-02-08 22:12:13 +00:00
|
|
|
cx.emit_span_lint(
|
|
|
|
POTENTIAL_QUERY_INSTABILITY,
|
|
|
|
span,
|
|
|
|
QueryInstability { query: cx.tcx.item_name(def_id) },
|
|
|
|
);
|
2022-01-05 13:02:16 +01:00
|
|
|
}
|
2024-08-09 09:52:12 +02:00
|
|
|
if cx.tcx.has_attr(def_id, sym::rustc_lint_untracked_query_information) {
|
2025-02-08 22:12:13 +00:00
|
|
|
cx.emit_span_lint(
|
|
|
|
UNTRACKED_QUERY_INFORMATION,
|
|
|
|
span,
|
|
|
|
QueryUntracked { method: cx.tcx.item_name(def_id) },
|
|
|
|
);
|
2024-08-09 09:52:12 +02:00
|
|
|
}
|
2022-01-05 13:02:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 10:43:51 +02:00
|
|
|
declare_tool_lint! {
|
2023-03-08 16:55:28 +00:00
|
|
|
/// The `usage_of_ty_tykind` lint detects usages of `ty::TyKind::<kind>`,
|
|
|
|
/// where `ty::<kind>` would suffice.
|
2019-06-24 10:43:51 +02:00
|
|
|
pub rustc::USAGE_OF_TY_TYKIND,
|
2019-03-16 14:59:34 +01:00
|
|
|
Allow,
|
2020-01-03 17:27:14 -08:00
|
|
|
"usage of `ty::TyKind` outside of the `ty::sty` module",
|
|
|
|
report_in_external_macro: true
|
2018-12-06 13:50:04 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 10:43:51 +02:00
|
|
|
declare_tool_lint! {
|
2023-03-08 16:55:28 +00:00
|
|
|
/// The `usage_of_qualified_ty` lint detects usages of `ty::TyKind`,
|
|
|
|
/// where `Ty` should be used instead.
|
2019-06-24 10:43:51 +02:00
|
|
|
pub rustc::USAGE_OF_QUALIFIED_TY,
|
2019-04-24 23:22:54 +02:00
|
|
|
Allow,
|
2020-01-03 17:27:14 -08:00
|
|
|
"using `ty::{Ty,TyCtxt}` instead of importing it",
|
|
|
|
report_in_external_macro: true
|
2019-04-24 23:22:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(TyTyKind => [
|
|
|
|
USAGE_OF_TY_TYKIND,
|
|
|
|
USAGE_OF_QUALIFIED_TY,
|
|
|
|
]);
|
2018-12-06 13:50:04 +01:00
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for TyTyKind {
|
2022-05-26 20:22:28 -07:00
|
|
|
fn check_path(
|
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'tcx>,
|
2022-11-25 11:26:36 +03:00
|
|
|
path: &rustc_hir::Path<'tcx>,
|
2022-05-26 20:22:28 -07:00
|
|
|
_: rustc_hir::HirId,
|
|
|
|
) {
|
|
|
|
if let Some(segment) = path.segments.iter().nth_back(1)
|
2023-10-13 08:58:33 +00:00
|
|
|
&& lint_ty_kind_usage(cx, &segment.res)
|
2022-05-26 20:22:28 -07:00
|
|
|
{
|
2023-10-13 08:58:33 +00:00
|
|
|
let span =
|
|
|
|
path.span.with_hi(segment.args.map_or(segment.ident.span, |a| a.span_ext).hi());
|
2024-01-16 14:40:39 +11:00
|
|
|
cx.emit_span_lint(USAGE_OF_TY_TYKIND, path.span, TykindKind { suggestion: span });
|
2018-12-06 13:50:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-26 03:51:41 +00:00
|
|
|
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx, hir::AmbigArg>) {
|
2019-09-26 17:25:31 +01:00
|
|
|
match &ty.kind {
|
2025-03-26 03:51:41 +00:00
|
|
|
hir::TyKind::Path(hir::QPath::Resolved(_, path)) => {
|
2022-05-26 20:22:28 -07:00
|
|
|
if lint_ty_kind_usage(cx, &path.res) {
|
2024-02-09 23:58:36 +03:00
|
|
|
let span = match cx.tcx.parent_hir_node(ty.hir_id) {
|
2025-03-26 03:51:41 +00:00
|
|
|
hir::Node::PatExpr(hir::PatExpr {
|
|
|
|
kind: hir::PatExprKind::Path(qpath),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| hir::Node::Pat(hir::Pat {
|
|
|
|
kind:
|
|
|
|
hir::PatKind::TupleStruct(qpath, ..) | hir::PatKind::Struct(qpath, ..),
|
2022-09-16 11:01:02 +04:00
|
|
|
..
|
2024-12-12 19:13:26 +00:00
|
|
|
})
|
2025-03-26 03:51:41 +00:00
|
|
|
| hir::Node::Expr(
|
|
|
|
hir::Expr { kind: hir::ExprKind::Path(qpath), .. }
|
|
|
|
| &hir::Expr { kind: hir::ExprKind::Struct(qpath, ..), .. },
|
2024-12-12 19:13:26 +00:00
|
|
|
) => {
|
2025-03-26 03:51:41 +00:00
|
|
|
if let hir::QPath::TypeRelative(qpath_ty, ..) = qpath
|
2022-09-16 11:01:02 +04:00
|
|
|
&& qpath_ty.hir_id == ty.hir_id
|
|
|
|
{
|
|
|
|
Some(path.span)
|
|
|
|
} else {
|
|
|
|
None
|
2022-05-26 20:22:28 -07:00
|
|
|
}
|
2019-04-24 23:22:54 +02:00
|
|
|
}
|
2023-10-13 08:58:33 +00:00
|
|
|
_ => None,
|
2022-09-16 11:01:02 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
match span {
|
|
|
|
Some(span) => {
|
2025-02-08 22:12:13 +00:00
|
|
|
cx.emit_span_lint(
|
|
|
|
USAGE_OF_TY_TYKIND,
|
|
|
|
path.span,
|
|
|
|
TykindKind { suggestion: span },
|
|
|
|
);
|
2023-10-13 08:58:33 +00:00
|
|
|
}
|
2024-01-16 14:40:39 +11:00
|
|
|
None => cx.emit_span_lint(USAGE_OF_TY_TYKIND, path.span, TykindDiag),
|
2019-04-24 23:22:54 +02:00
|
|
|
}
|
2023-10-13 08:58:33 +00:00
|
|
|
} else if !ty.span.from_expansion()
|
|
|
|
&& path.segments.len() > 1
|
2023-11-21 20:07:32 +01:00
|
|
|
&& let Some(ty) = is_ty_or_ty_ctxt(cx, path)
|
2023-10-13 08:58:33 +00:00
|
|
|
{
|
2025-02-08 22:12:13 +00:00
|
|
|
cx.emit_span_lint(
|
|
|
|
USAGE_OF_QUALIFIED_TY,
|
|
|
|
path.span,
|
|
|
|
TyQualified { ty, suggestion: path.span },
|
|
|
|
);
|
2019-04-24 23:22:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2018-12-06 13:50:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 17:03:45 +01:00
|
|
|
|
2022-05-26 20:22:28 -07:00
|
|
|
fn lint_ty_kind_usage(cx: &LateContext<'_>, res: &Res) -> bool {
|
|
|
|
if let Some(did) = res.opt_def_id() {
|
|
|
|
cx.tcx.is_diagnostic_item(sym::TyKind, did) || cx.tcx.is_diagnostic_item(sym::IrTyKind, did)
|
|
|
|
} else {
|
|
|
|
false
|
2019-03-21 17:03:45 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 23:22:54 +02:00
|
|
|
|
2025-03-26 03:51:41 +00:00
|
|
|
fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &hir::Path<'_>) -> Option<String> {
|
2022-05-26 20:22:28 -07:00
|
|
|
match &path.res {
|
|
|
|
Res::Def(_, def_id) => {
|
|
|
|
if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(*def_id) {
|
|
|
|
return Some(format!("{}{}", name, gen_args(path.segments.last().unwrap())));
|
2021-11-07 10:33:27 +01:00
|
|
|
}
|
2022-05-26 20:22:28 -07:00
|
|
|
}
|
|
|
|
// Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
|
2022-09-16 11:45:33 +10:00
|
|
|
Res::SelfTyAlias { alias_to: did, is_trait_impl: false, .. } => {
|
2024-12-02 13:43:16 +01:00
|
|
|
if let ty::Adt(adt, args) = cx.tcx.type_of(did).instantiate_identity().kind()
|
|
|
|
&& let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(adt.did())
|
|
|
|
{
|
|
|
|
return Some(format!("{}<{}>", name, args[0]));
|
2019-04-24 23:22:54 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-26 20:22:28 -07:00
|
|
|
_ => (),
|
2019-04-24 23:22:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2025-03-26 03:51:41 +00:00
|
|
|
fn gen_args(segment: &hir::PathSegment<'_>) -> String {
|
2019-04-24 23:22:54 +02:00
|
|
|
if let Some(args) = &segment.args {
|
|
|
|
let lifetimes = args
|
|
|
|
.args
|
|
|
|
.iter()
|
|
|
|
.filter_map(|arg| {
|
2025-03-26 03:51:41 +00:00
|
|
|
if let hir::GenericArg::Lifetime(lt) = arg {
|
|
|
|
Some(lt.ident.to_string())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-04-24 23:22:54 +02:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if !lifetimes.is_empty() {
|
|
|
|
return format!("<{}>", lifetimes.join(", "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String::new()
|
|
|
|
}
|
2019-05-02 16:53:12 +02:00
|
|
|
|
2024-07-17 12:35:50 +02:00
|
|
|
declare_tool_lint! {
|
|
|
|
/// The `non_glob_import_of_type_ir_inherent_item` lint detects
|
|
|
|
/// non-glob imports of module `rustc_type_ir::inherent`.
|
|
|
|
pub rustc::NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT,
|
|
|
|
Allow,
|
|
|
|
"non-glob import of `rustc_type_ir::inherent`",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
2024-08-28 00:14:41 -04:00
|
|
|
declare_tool_lint! {
|
2025-03-26 04:09:07 +00:00
|
|
|
/// The `usage_of_type_ir_inherent` lint detects usage of `rustc_type_ir::inherent`.
|
2024-08-28 00:14:41 -04:00
|
|
|
///
|
|
|
|
/// This module should only be used within the trait solver.
|
|
|
|
pub rustc::USAGE_OF_TYPE_IR_INHERENT,
|
|
|
|
Allow,
|
|
|
|
"usage `rustc_type_ir::inherent` outside of trait system",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
2025-03-26 04:09:07 +00:00
|
|
|
declare_tool_lint! {
|
|
|
|
/// The `usage_of_type_ir_traits` lint detects usage of `rustc_type_ir::Interner`,
|
|
|
|
/// or `rustc_infer::InferCtxtLike`.
|
|
|
|
///
|
|
|
|
/// Methods of this trait should only be used within the type system abstraction layer,
|
|
|
|
/// and in the generic next trait solver implementation. Look for an analogously named
|
|
|
|
/// method on `TyCtxt` or `InferCtxt` (respectively).
|
|
|
|
pub rustc::USAGE_OF_TYPE_IR_TRAITS,
|
|
|
|
Allow,
|
|
|
|
"usage `rustc_type_ir`-specific abstraction traits outside of trait system",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_TRAITS]);
|
2024-07-17 12:35:50 +02:00
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for TypeIr {
|
2025-03-26 04:09:07 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
|
|
|
|
let res_def_id = match expr.kind {
|
|
|
|
hir::ExprKind::Path(hir::QPath::Resolved(_, path)) => path.res.opt_def_id(),
|
|
|
|
hir::ExprKind::Path(hir::QPath::TypeRelative(..)) | hir::ExprKind::MethodCall(..) => {
|
|
|
|
cx.typeck_results().type_dependent_def_id(expr.hir_id)
|
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
let Some(res_def_id) = res_def_id else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
if let Some(assoc_item) = cx.tcx.opt_associated_item(res_def_id)
|
|
|
|
&& let Some(trait_def_id) = assoc_item.trait_container(cx.tcx)
|
|
|
|
&& (cx.tcx.is_diagnostic_item(sym::type_ir_interner, trait_def_id)
|
|
|
|
| cx.tcx.is_diagnostic_item(sym::type_ir_infer_ctxt_like, trait_def_id))
|
|
|
|
{
|
|
|
|
cx.emit_span_lint(USAGE_OF_TYPE_IR_TRAITS, expr.span, TypeIrTraitUsage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-26 03:51:41 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
|
2024-07-17 12:35:50 +02:00
|
|
|
let rustc_hir::ItemKind::Use(path, kind) = item.kind else { return };
|
|
|
|
|
|
|
|
let is_mod_inherent = |def_id| cx.tcx.is_diagnostic_item(sym::type_ir_inherent, def_id);
|
2024-08-28 00:14:41 -04:00
|
|
|
|
|
|
|
// Path segments except for the final.
|
|
|
|
if let Some(seg) =
|
|
|
|
path.segments.iter().find(|seg| seg.res.opt_def_id().is_some_and(is_mod_inherent))
|
|
|
|
{
|
|
|
|
cx.emit_span_lint(USAGE_OF_TYPE_IR_INHERENT, seg.ident.span, TypeIrInherentUsage);
|
|
|
|
}
|
|
|
|
// Final path resolutions, like `use rustc_type_ir::inherent`
|
|
|
|
else if path.res.iter().any(|res| res.opt_def_id().is_some_and(is_mod_inherent)) {
|
|
|
|
cx.emit_span_lint(
|
|
|
|
USAGE_OF_TYPE_IR_INHERENT,
|
|
|
|
path.segments.last().unwrap().ident.span,
|
|
|
|
TypeIrInherentUsage,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-17 12:35:50 +02:00
|
|
|
let (lo, hi, snippet) = match path.segments {
|
|
|
|
[.., penultimate, segment]
|
|
|
|
if penultimate.res.opt_def_id().is_some_and(is_mod_inherent) =>
|
|
|
|
{
|
Move `hir::Item::ident` into `hir::ItemKind`.
`hir::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
Trait`, TraitAalis`.
- It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`,
`Impl`.
- For `Use`, it is non-empty for `UseKind::Single` and empty for
`UseKind::{Glob,ListStem}`.
All of this is quite non-obvious; the only documentation is a single
comment saying "The name might be a dummy name in case of anonymous
items". Some sites that handle items check for an empty ident, some
don't. This is a very C-like way of doing things, but this is Rust, we
have sum types, we can do this properly and never forget to check for
the exceptional case and never YOLO possibly empty identifiers (or
possibly dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- A similar transformation makes sense for `ast::Item`, but this is
already a big change. That can be done later.
- Lots of assertions are added to item lowering to ensure that
identifiers are empty/non-empty as expected. These will be removable
when `ast::Item` is done later.
- `ItemKind::Use` doesn't get an `Ident`, but `UseKind::Single` does.
- `lower_use_tree` is significantly simpler. No more confusing `&mut
Ident` to deal with.
- `ItemKind::ident` is a new method, it returns an `Option<Ident>`. It's
used with `unwrap` in a few places; sometimes it's hard to tell
exactly which item kinds might occur. None of these unwraps fail on
the test suite. It's conceivable that some might fail on alternative
input. We can deal with those if/when they happen.
- In `trait_path` the `find_map`/`if let` is replaced with a loop, and
things end up much clearer that way.
- `named_span` no longer checks for an empty name; instead the call site
now checks for a missing identifier if necessary.
- `maybe_inline_local` doesn't need the `glob` argument, it can be
computed in-function from the `renamed` argument.
- `arbitrary_source_item_ordering::check_mod` had a big `if` statement
that was just getting the ident from the item kinds that had one. It
could be mostly replaced by a single call to the new `ItemKind::ident`
method.
- `ItemKind` grows from 56 to 64 bytes, but `Item` stays the same size,
and that's what matters, because `ItemKind` only occurs within `Item`.
2025-03-06 19:07:36 +11:00
|
|
|
(segment.ident.span, item.kind.ident().unwrap().span, "*")
|
2024-07-17 12:35:50 +02:00
|
|
|
}
|
|
|
|
[.., segment]
|
|
|
|
if path.res.iter().flat_map(Res::opt_def_id).any(is_mod_inherent)
|
Move `hir::Item::ident` into `hir::ItemKind`.
`hir::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
Trait`, TraitAalis`.
- It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`,
`Impl`.
- For `Use`, it is non-empty for `UseKind::Single` and empty for
`UseKind::{Glob,ListStem}`.
All of this is quite non-obvious; the only documentation is a single
comment saying "The name might be a dummy name in case of anonymous
items". Some sites that handle items check for an empty ident, some
don't. This is a very C-like way of doing things, but this is Rust, we
have sum types, we can do this properly and never forget to check for
the exceptional case and never YOLO possibly empty identifiers (or
possibly dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- A similar transformation makes sense for `ast::Item`, but this is
already a big change. That can be done later.
- Lots of assertions are added to item lowering to ensure that
identifiers are empty/non-empty as expected. These will be removable
when `ast::Item` is done later.
- `ItemKind::Use` doesn't get an `Ident`, but `UseKind::Single` does.
- `lower_use_tree` is significantly simpler. No more confusing `&mut
Ident` to deal with.
- `ItemKind::ident` is a new method, it returns an `Option<Ident>`. It's
used with `unwrap` in a few places; sometimes it's hard to tell
exactly which item kinds might occur. None of these unwraps fail on
the test suite. It's conceivable that some might fail on alternative
input. We can deal with those if/when they happen.
- In `trait_path` the `find_map`/`if let` is replaced with a loop, and
things end up much clearer that way.
- `named_span` no longer checks for an empty name; instead the call site
now checks for a missing identifier if necessary.
- `maybe_inline_local` doesn't need the `glob` argument, it can be
computed in-function from the `renamed` argument.
- `arbitrary_source_item_ordering::check_mod` had a big `if` statement
that was just getting the ident from the item kinds that had one. It
could be mostly replaced by a single call to the new `ItemKind::ident`
method.
- `ItemKind` grows from 56 to 64 bytes, but `Item` stays the same size,
and that's what matters, because `ItemKind` only occurs within `Item`.
2025-03-06 19:07:36 +11:00
|
|
|
&& let rustc_hir::UseKind::Single(ident) = kind =>
|
2024-07-17 12:35:50 +02:00
|
|
|
{
|
|
|
|
let (lo, snippet) =
|
|
|
|
match cx.tcx.sess.source_map().span_to_snippet(path.span).as_deref() {
|
|
|
|
Ok("self") => (path.span, "*"),
|
|
|
|
_ => (segment.ident.span.shrink_to_hi(), "::*"),
|
|
|
|
};
|
Move `hir::Item::ident` into `hir::ItemKind`.
`hir::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
Trait`, TraitAalis`.
- It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`,
`Impl`.
- For `Use`, it is non-empty for `UseKind::Single` and empty for
`UseKind::{Glob,ListStem}`.
All of this is quite non-obvious; the only documentation is a single
comment saying "The name might be a dummy name in case of anonymous
items". Some sites that handle items check for an empty ident, some
don't. This is a very C-like way of doing things, but this is Rust, we
have sum types, we can do this properly and never forget to check for
the exceptional case and never YOLO possibly empty identifiers (or
possibly dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- A similar transformation makes sense for `ast::Item`, but this is
already a big change. That can be done later.
- Lots of assertions are added to item lowering to ensure that
identifiers are empty/non-empty as expected. These will be removable
when `ast::Item` is done later.
- `ItemKind::Use` doesn't get an `Ident`, but `UseKind::Single` does.
- `lower_use_tree` is significantly simpler. No more confusing `&mut
Ident` to deal with.
- `ItemKind::ident` is a new method, it returns an `Option<Ident>`. It's
used with `unwrap` in a few places; sometimes it's hard to tell
exactly which item kinds might occur. None of these unwraps fail on
the test suite. It's conceivable that some might fail on alternative
input. We can deal with those if/when they happen.
- In `trait_path` the `find_map`/`if let` is replaced with a loop, and
things end up much clearer that way.
- `named_span` no longer checks for an empty name; instead the call site
now checks for a missing identifier if necessary.
- `maybe_inline_local` doesn't need the `glob` argument, it can be
computed in-function from the `renamed` argument.
- `arbitrary_source_item_ordering::check_mod` had a big `if` statement
that was just getting the ident from the item kinds that had one. It
could be mostly replaced by a single call to the new `ItemKind::ident`
method.
- `ItemKind` grows from 56 to 64 bytes, but `Item` stays the same size,
and that's what matters, because `ItemKind` only occurs within `Item`.
2025-03-06 19:07:36 +11:00
|
|
|
(lo, if segment.ident == ident { lo } else { ident.span }, snippet)
|
2024-07-17 12:35:50 +02:00
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
cx.emit_span_lint(
|
|
|
|
NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT,
|
|
|
|
path.span,
|
|
|
|
NonGlobImportTypeIrInherent { suggestion: lo.eq_ctxt(hi).then(|| lo.to(hi)), snippet },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 10:43:51 +02:00
|
|
|
declare_tool_lint! {
|
2023-03-08 16:55:28 +00:00
|
|
|
/// The `lint_pass_impl_without_macro` detects manual implementations of a lint
|
|
|
|
/// pass, without using [`declare_lint_pass`] or [`impl_lint_pass`].
|
2019-06-24 10:43:51 +02:00
|
|
|
pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
|
2019-05-02 16:53:12 +02:00
|
|
|
Allow,
|
|
|
|
"`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
|
|
|
|
|
|
|
|
impl EarlyLintPass for LintPassImpl {
|
2021-07-02 14:15:11 -05:00
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
|
2021-11-07 16:43:49 +08:00
|
|
|
if let ast::ItemKind::Impl(box ast::Impl { of_trait: Some(lint_pass), .. }) = &item.kind {
|
2019-06-13 15:49:33 +02:00
|
|
|
if let Some(last) = lint_pass.path.segments.last() {
|
|
|
|
if last.ident.name == sym::LintPass {
|
2019-08-13 23:56:42 +03:00
|
|
|
let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
|
|
|
|
let call_site = expn_data.call_site;
|
2021-07-10 22:14:52 +03:00
|
|
|
if expn_data.kind != ExpnKind::Macro(MacroKind::Bang, sym::impl_lint_pass)
|
|
|
|
&& call_site.ctxt().outer_expn_data().kind
|
|
|
|
!= ExpnKind::Macro(MacroKind::Bang, sym::declare_lint_pass)
|
|
|
|
{
|
2024-01-16 14:40:39 +11:00
|
|
|
cx.emit_span_lint(
|
2019-08-11 03:00:05 +03:00
|
|
|
LINT_PASS_IMPL_WITHOUT_MACRO,
|
|
|
|
lint_pass.path.span,
|
2022-10-22 16:32:54 -04:00
|
|
|
LintPassByHand,
|
|
|
|
);
|
2019-05-02 16:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 22:34:41 +01:00
|
|
|
|
2022-06-10 15:50:06 +01:00
|
|
|
declare_tool_lint! {
|
2024-02-20 14:12:50 +11:00
|
|
|
/// The `untranslatable_diagnostic` lint detects messages passed to functions with `impl
|
|
|
|
/// Into<{D,Subd}iagMessage` parameters without using translatable Fluent strings.
|
2023-03-08 16:55:28 +00:00
|
|
|
///
|
2024-02-20 14:12:50 +11:00
|
|
|
/// More details on translatable diagnostics can be found
|
|
|
|
/// [here](https://rustc-dev-guide.rust-lang.org/diagnostics/translation.html).
|
2022-06-10 15:50:06 +01:00
|
|
|
pub rustc::UNTRANSLATABLE_DIAGNOSTIC,
|
2024-10-26 13:07:26 +00:00
|
|
|
Allow,
|
2022-06-10 15:50:06 +01:00
|
|
|
"prevent creation of diagnostics which cannot be translated",
|
2023-11-13 14:35:37 +01:00
|
|
|
report_in_external_macro: true,
|
2024-10-11 00:31:17 +02:00
|
|
|
@eval_always = true
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_tool_lint! {
|
2024-02-20 14:12:50 +11:00
|
|
|
/// The `diagnostic_outside_of_impl` lint detects calls to functions annotated with
|
2024-03-06 14:00:16 +11:00
|
|
|
/// `#[rustc_lint_diagnostics]` that are outside an `Diagnostic`, `Subdiagnostic`, or
|
2024-03-08 12:13:39 +11:00
|
|
|
/// `LintDiagnostic` impl (either hand-written or derived).
|
2023-03-08 16:55:28 +00:00
|
|
|
///
|
2024-02-20 14:12:50 +11:00
|
|
|
/// More details on diagnostics implementations can be found
|
|
|
|
/// [here](https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-structs.html).
|
2022-06-10 15:50:06 +01:00
|
|
|
pub rustc::DIAGNOSTIC_OUTSIDE_OF_IMPL,
|
2024-10-26 13:07:26 +00:00
|
|
|
Allow,
|
2024-03-08 12:03:51 +11:00
|
|
|
"prevent diagnostic creation outside of `Diagnostic`/`Subdiagnostic`/`LintDiagnostic` impls",
|
2023-11-13 14:35:37 +01:00
|
|
|
report_in_external_macro: true,
|
2024-10-11 00:31:17 +02:00
|
|
|
@eval_always = true
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
|
2024-02-05 16:27:53 +11:00
|
|
|
declare_lint_pass!(Diagnostics => [UNTRANSLATABLE_DIAGNOSTIC, DIAGNOSTIC_OUTSIDE_OF_IMPL]);
|
2022-06-10 15:50:06 +01:00
|
|
|
|
|
|
|
impl LateLintPass<'_> for Diagnostics {
|
2025-03-26 03:51:41 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
|
|
|
|
let collect_args_tys_and_spans = |args: &[hir::Expr<'_>], reserve_one_extra: bool| {
|
2024-08-10 19:29:20 +03:00
|
|
|
let mut result = Vec::with_capacity(args.len() + usize::from(reserve_one_extra));
|
|
|
|
result.extend(args.iter().map(|arg| (cx.typeck_results().expr_ty(arg), arg.span)));
|
|
|
|
result
|
|
|
|
};
|
2024-02-20 14:12:50 +11:00
|
|
|
// Only check function calls and method calls.
|
2024-08-10 19:29:20 +03:00
|
|
|
let (span, def_id, fn_gen_args, arg_tys_and_spans) = match expr.kind {
|
2025-03-26 03:51:41 +00:00
|
|
|
hir::ExprKind::Call(callee, args) => {
|
2024-02-20 14:12:50 +11:00
|
|
|
match cx.typeck_results().node_type(callee.hir_id).kind() {
|
|
|
|
&ty::FnDef(def_id, fn_gen_args) => {
|
2024-08-10 19:29:20 +03:00
|
|
|
(callee.span, def_id, fn_gen_args, collect_args_tys_and_spans(args, false))
|
2024-02-20 14:12:50 +11:00
|
|
|
}
|
|
|
|
_ => return, // occurs for fns passed as args
|
|
|
|
}
|
|
|
|
}
|
2025-03-26 03:51:41 +00:00
|
|
|
hir::ExprKind::MethodCall(_segment, _recv, args, _span) => {
|
2024-03-08 15:09:53 +08:00
|
|
|
let Some((span, def_id, fn_gen_args)) = typeck_results_of_method_fn(cx, expr)
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
};
|
2024-08-10 19:29:20 +03:00
|
|
|
let mut args = collect_args_tys_and_spans(args, true);
|
|
|
|
args.insert(0, (cx.tcx.types.self_param, _recv.span)); // dummy inserted for `self`
|
|
|
|
(span, def_id, fn_gen_args, args)
|
2024-02-20 14:12:50 +11:00
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2024-08-10 21:40:07 +03:00
|
|
|
Self::diagnostic_outside_of_impl(cx, span, expr.hir_id, def_id, fn_gen_args);
|
|
|
|
Self::untranslatable_diagnostic(cx, def_id, &arg_tys_and_spans);
|
|
|
|
}
|
|
|
|
}
|
2022-08-31 12:06:22 +01:00
|
|
|
|
2024-08-10 21:40:07 +03:00
|
|
|
impl Diagnostics {
|
|
|
|
// Is the type `{D,Subd}iagMessage`?
|
|
|
|
fn is_diag_message<'cx>(cx: &LateContext<'cx>, ty: MiddleTy<'cx>) -> bool {
|
|
|
|
if let Some(adt_def) = ty.ty_adt_def()
|
|
|
|
&& let Some(name) = cx.tcx.get_diagnostic_name(adt_def.did())
|
|
|
|
&& matches!(name, sym::DiagMessage | sym::SubdiagMessage)
|
|
|
|
{
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn untranslatable_diagnostic<'cx>(
|
|
|
|
cx: &LateContext<'cx>,
|
|
|
|
def_id: DefId,
|
|
|
|
arg_tys_and_spans: &[(MiddleTy<'cx>, Span)],
|
|
|
|
) {
|
2024-02-20 14:12:50 +11:00
|
|
|
let fn_sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder();
|
|
|
|
let predicates = cx.tcx.predicates_of(def_id).instantiate_identity(cx.tcx).predicates;
|
|
|
|
for (i, ¶m_ty) in fn_sig.inputs().iter().enumerate() {
|
2024-08-10 21:40:07 +03:00
|
|
|
if let ty::Param(sig_param) = param_ty.kind() {
|
2024-02-20 14:12:50 +11:00
|
|
|
// It is a type parameter. Check if it is `impl Into<{D,Subd}iagMessage>`.
|
|
|
|
for pred in predicates.iter() {
|
|
|
|
if let Some(trait_pred) = pred.as_trait_clause()
|
|
|
|
&& let trait_ref = trait_pred.skip_binder().trait_ref
|
|
|
|
&& trait_ref.self_ty() == param_ty // correct predicate for the param?
|
|
|
|
&& cx.tcx.is_diagnostic_item(sym::Into, trait_ref.def_id)
|
|
|
|
&& let ty1 = trait_ref.args.type_at(1)
|
2024-08-10 21:40:07 +03:00
|
|
|
&& Self::is_diag_message(cx, ty1)
|
2024-02-20 14:12:50 +11:00
|
|
|
{
|
2024-08-10 21:40:07 +03:00
|
|
|
// Calls to methods with an `impl Into<{D,Subd}iagMessage>` parameter must be passed an arg
|
|
|
|
// with type `{D,Subd}iagMessage` or `impl Into<{D,Subd}iagMessage>`. Otherwise, emit an
|
|
|
|
// `UNTRANSLATABLE_DIAGNOSTIC` lint.
|
|
|
|
let (arg_ty, arg_span) = arg_tys_and_spans[i];
|
|
|
|
|
|
|
|
// Is the arg type `{Sub,D}iagMessage`or `impl Into<{Sub,D}iagMessage>`?
|
|
|
|
let is_translatable = Self::is_diag_message(cx, arg_ty)
|
|
|
|
|| matches!(arg_ty.kind(), ty::Param(arg_param) if arg_param.name == sig_param.name);
|
|
|
|
if !is_translatable {
|
|
|
|
cx.emit_span_lint(
|
|
|
|
UNTRANSLATABLE_DIAGNOSTIC,
|
|
|
|
arg_span,
|
|
|
|
UntranslatableDiag,
|
|
|
|
);
|
|
|
|
}
|
2024-02-20 14:12:50 +11:00
|
|
|
}
|
|
|
|
}
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
}
|
2024-08-10 21:40:07 +03:00
|
|
|
}
|
2024-02-20 14:12:50 +11:00
|
|
|
|
2024-08-10 21:40:07 +03:00
|
|
|
fn diagnostic_outside_of_impl<'cx>(
|
|
|
|
cx: &LateContext<'cx>,
|
|
|
|
span: Span,
|
|
|
|
current_id: HirId,
|
|
|
|
def_id: DefId,
|
|
|
|
fn_gen_args: GenericArgsRef<'cx>,
|
|
|
|
) {
|
|
|
|
// Is the callee marked with `#[rustc_lint_diagnostics]`?
|
|
|
|
let Some(inst) =
|
2024-11-15 13:53:31 +01:00
|
|
|
ty::Instance::try_resolve(cx.tcx, cx.typing_env(), def_id, fn_gen_args).ok().flatten()
|
2024-08-10 21:40:07 +03:00
|
|
|
else {
|
2024-02-20 14:12:50 +11:00
|
|
|
return;
|
2024-08-10 21:40:07 +03:00
|
|
|
};
|
|
|
|
let has_attr = cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics);
|
|
|
|
if !has_attr {
|
|
|
|
return;
|
|
|
|
};
|
2022-06-10 15:50:06 +01:00
|
|
|
|
2025-02-21 07:54:35 +11:00
|
|
|
for (hir_id, _parent) in cx.tcx.hir_parent_iter(current_id) {
|
2024-02-20 14:12:50 +11:00
|
|
|
if let Some(owner_did) = hir_id.as_owner()
|
|
|
|
&& cx.tcx.has_attr(owner_did, sym::rustc_lint_diagnostics)
|
2022-06-10 15:50:06 +01:00
|
|
|
{
|
2024-08-10 21:40:07 +03:00
|
|
|
// The parent method is marked with `#[rustc_lint_diagnostics]`
|
|
|
|
return;
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-20 14:12:50 +11:00
|
|
|
|
|
|
|
// Calls to `#[rustc_lint_diagnostics]`-marked functions should only occur:
|
2024-03-08 12:03:51 +11:00
|
|
|
// - inside an impl of `Diagnostic`, `Subdiagnostic`, or `LintDiagnostic`, or
|
2024-02-20 14:12:50 +11:00
|
|
|
// - inside a parent function that is itself marked with `#[rustc_lint_diagnostics]`.
|
|
|
|
//
|
|
|
|
// Otherwise, emit a `DIAGNOSTIC_OUTSIDE_OF_IMPL` lint.
|
2024-08-10 21:40:07 +03:00
|
|
|
let mut is_inside_appropriate_impl = false;
|
2025-02-21 07:54:35 +11:00
|
|
|
for (_hir_id, parent) in cx.tcx.hir_parent_iter(current_id) {
|
2024-08-10 21:40:07 +03:00
|
|
|
debug!(?parent);
|
2025-03-26 03:51:41 +00:00
|
|
|
if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) = parent
|
|
|
|
&& let hir::Impl { of_trait: Some(of_trait), .. } = impl_
|
2024-08-10 21:40:07 +03:00
|
|
|
&& let Some(def_id) = of_trait.trait_def_id()
|
|
|
|
&& let Some(name) = cx.tcx.get_diagnostic_name(def_id)
|
|
|
|
&& matches!(name, sym::Diagnostic | sym::Subdiagnostic | sym::LintDiagnostic)
|
|
|
|
{
|
|
|
|
is_inside_appropriate_impl = true;
|
|
|
|
break;
|
2024-02-20 14:12:50 +11:00
|
|
|
}
|
|
|
|
}
|
2024-08-10 21:40:07 +03:00
|
|
|
debug!(?is_inside_appropriate_impl);
|
|
|
|
if !is_inside_appropriate_impl {
|
|
|
|
cx.emit_span_lint(DIAGNOSTIC_OUTSIDE_OF_IMPL, span, DiagOutOfImpl);
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 13:02:39 +01:00
|
|
|
|
|
|
|
declare_tool_lint! {
|
2023-04-10 22:02:52 +02:00
|
|
|
/// The `bad_opt_access` lint detects accessing options by field instead of
|
2023-03-08 16:55:28 +00:00
|
|
|
/// the wrapper function.
|
2022-07-25 13:02:39 +01:00
|
|
|
pub rustc::BAD_OPT_ACCESS,
|
|
|
|
Deny,
|
|
|
|
"prevent using options by field access when there is a wrapper function",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
2024-02-20 14:12:50 +11:00
|
|
|
declare_lint_pass!(BadOptAccess => [BAD_OPT_ACCESS]);
|
2022-07-25 13:02:39 +01:00
|
|
|
|
|
|
|
impl LateLintPass<'_> for BadOptAccess {
|
2025-03-26 03:51:41 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
|
|
|
|
let hir::ExprKind::Field(base, target) = expr.kind else { return };
|
2022-07-25 13:02:39 +01:00
|
|
|
let Some(adt_def) = cx.typeck_results().expr_ty(base).ty_adt_def() else { return };
|
|
|
|
// Skip types without `#[rustc_lint_opt_ty]` - only so that the rest of the lint can be
|
|
|
|
// avoided.
|
|
|
|
if !cx.tcx.has_attr(adt_def.did(), sym::rustc_lint_opt_ty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for field in adt_def.all_fields() {
|
2023-10-13 08:58:33 +00:00
|
|
|
if field.name == target.name
|
|
|
|
&& let Some(attr) =
|
|
|
|
cx.tcx.get_attr(field.did, sym::rustc_lint_opt_deny_field_access)
|
|
|
|
&& let Some(items) = attr.meta_item_list()
|
|
|
|
&& let Some(item) = items.first()
|
|
|
|
&& let Some(lit) = item.lit()
|
|
|
|
&& let ast::LitKind::Str(val, _) = lit.kind
|
2022-07-25 13:02:39 +01:00
|
|
|
{
|
2025-02-08 22:12:13 +00:00
|
|
|
cx.emit_span_lint(
|
|
|
|
BAD_OPT_ACCESS,
|
|
|
|
expr.span,
|
|
|
|
BadOptAccessDiag { msg: val.as_str() },
|
|
|
|
);
|
2022-07-25 13:02:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 00:15:00 -07:00
|
|
|
|
|
|
|
declare_tool_lint! {
|
|
|
|
pub rustc::SPAN_USE_EQ_CTXT,
|
2023-10-16 01:05:11 -07:00
|
|
|
Allow,
|
2023-10-16 01:17:52 -07:00
|
|
|
"forbid uses of `==` with `Span::ctxt`, suggest `Span::eq_ctxt` instead",
|
2023-09-25 00:15:00 -07:00
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(SpanUseEqCtxt => [SPAN_USE_EQ_CTXT]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
|
2025-03-26 03:51:41 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) {
|
|
|
|
if let hir::ExprKind::Binary(
|
|
|
|
hir::BinOp { node: hir::BinOpKind::Eq | hir::BinOpKind::Ne, .. },
|
|
|
|
lhs,
|
|
|
|
rhs,
|
|
|
|
) = expr.kind
|
2024-01-02 23:32:40 +03:00
|
|
|
{
|
2023-09-25 00:15:00 -07:00
|
|
|
if is_span_ctxt_call(cx, lhs) && is_span_ctxt_call(cx, rhs) {
|
2024-01-16 14:40:39 +11:00
|
|
|
cx.emit_span_lint(SPAN_USE_EQ_CTXT, expr.span, SpanUseEqCtxtDiag);
|
2023-09-25 00:15:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-26 03:51:41 +00:00
|
|
|
fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
|
2023-09-25 00:15:00 -07:00
|
|
|
match &expr.kind {
|
2025-03-26 03:51:41 +00:00
|
|
|
hir::ExprKind::MethodCall(..) => cx
|
2023-10-16 01:05:11 -07:00
|
|
|
.typeck_results()
|
|
|
|
.type_dependent_def_id(expr.hir_id)
|
|
|
|
.is_some_and(|call_did| cx.tcx.is_diagnostic_item(sym::SpanCtxt, call_did)),
|
2023-09-25 00:15:00 -07:00
|
|
|
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2024-11-27 17:21:59 +00:00
|
|
|
|
|
|
|
declare_tool_lint! {
|
|
|
|
/// The `symbol_intern_string_literal` detects `Symbol::intern` being called on a string literal
|
|
|
|
pub rustc::SYMBOL_INTERN_STRING_LITERAL,
|
|
|
|
// rustc_driver crates out of the compiler can't/shouldn't add preinterned symbols;
|
|
|
|
// bootstrap will deny this manually
|
|
|
|
Allow,
|
|
|
|
"Forbid uses of string literals in `Symbol::intern`, suggesting preinterning instead",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(SymbolInternStringLiteral => [SYMBOL_INTERN_STRING_LITERAL]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for SymbolInternStringLiteral {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
|
2025-03-26 03:51:41 +00:00
|
|
|
if let hir::ExprKind::Call(path, [arg]) = expr.kind
|
|
|
|
&& let hir::ExprKind::Path(ref qpath) = path.kind
|
2024-11-27 17:21:59 +00:00
|
|
|
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
|
|
|
&& cx.tcx.is_diagnostic_item(sym::SymbolIntern, def_id)
|
2025-03-26 03:51:41 +00:00
|
|
|
&& let hir::ExprKind::Lit(kind) = arg.kind
|
2024-11-27 17:21:59 +00:00
|
|
|
&& let rustc_ast::LitKind::Str(_, _) = kind.node
|
|
|
|
{
|
|
|
|
cx.emit_span_lint(
|
|
|
|
SYMBOL_INTERN_STRING_LITERAL,
|
|
|
|
kind.span,
|
|
|
|
SymbolInternStringLiteralDiag,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|