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.
|
|
|
|
|
2022-10-22 16:32:54 -04:00
|
|
|
use crate::lints::{
|
2023-04-10 22:02:52 +02:00
|
|
|
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistentDocKeyword,
|
2023-09-25 00:15:00 -07:00
|
|
|
QueryInstability, SpanUseEqCtxtDiag, TyQualified, TykindDiag, TykindKind, UntranslatableDiag,
|
2022-10-22 16:32:54 -04:00
|
|
|
};
|
2020-01-09 07:52:01 +01:00
|
|
|
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
2021-07-02 14:15:11 -05:00
|
|
|
use rustc_ast as ast;
|
2020-09-19 12:34:31 +02:00
|
|
|
use rustc_hir::def::Res;
|
2022-06-10 15:50:06 +01:00
|
|
|
use rustc_hir::{def_id::DefId, Expr, ExprKind, GenericArg, PatKind, Path, PathSegment, QPath};
|
2023-09-25 00:15:00 -07:00
|
|
|
use rustc_hir::{BinOp, BinOpKind, HirId, Impl, Item, ItemKind, Node, Pat, Ty, TyKind};
|
2024-02-20 14:12:50 +11:00
|
|
|
use rustc_middle::ty::{self, Ty as MiddleTy};
|
2021-07-02 14:15:11 -05:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-20 22:22:36 +02:00
|
|
|
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
2021-07-02 14:15:11 -05:00
|
|
|
use rustc_span::symbol::{kw, sym, Symbol};
|
2022-06-10 15:50:06 +01:00
|
|
|
use rustc_span::Span;
|
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 `default_hash_type` lint detects use of [`std::collections::HashMap`]/[`std::collections::HashSet`],
|
|
|
|
/// suggesting the use of `FxHashMap`/`FxHashSet`.
|
|
|
|
///
|
|
|
|
/// 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 {
|
|
|
|
fn check_path(&mut self, cx: &LateContext<'_>, path: &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 };
|
2023-12-01 05:28:34 -08:00
|
|
|
if matches!(cx.tcx.hir_node(hir_id), Node::Item(Item { kind: ItemKind::Use(..), .. })) {
|
2021-07-02 14:15:11 -05:00
|
|
|
// don't lint imports, only actual usages
|
|
|
|
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
|
|
|
};
|
2024-01-16 14:40:39 +11:00
|
|
|
cx.emit_span_lint(
|
2022-09-16 11:01:02 +04:00
|
|
|
DEFAULT_HASH_TYPES,
|
|
|
|
path.span,
|
2022-10-22 16:32:54 -04:00
|
|
|
DefaultHashTypesDiag { preferred, used: cx.tcx.item_name(def_id) },
|
2022-09-16 11:01:02 +04:00
|
|
|
);
|
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>,
|
|
|
|
expr: &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 {
|
2022-09-01 13:27:31 +09:00
|
|
|
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
|
|
|
}
|
|
|
|
_ => match cx.typeck_results().node_type(expr.hir_id).kind() {
|
2023-07-11 22:35:29 +01:00
|
|
|
&ty::FnDef(def_id, args) => Some((expr.span, def_id, args)),
|
2022-06-10 15:50:06 +01:00
|
|
|
_ => None,
|
2023-10-13 08:58:33 +00:00
|
|
|
},
|
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,
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for QueryStability {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
2023-07-11 22:35:29 +01:00
|
|
|
let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return };
|
|
|
|
if let Ok(Some(instance)) = ty::Instance::resolve(cx.tcx, cx.param_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) {
|
2024-01-16 14:40:39 +11:00
|
|
|
cx.emit_span_lint(
|
2022-09-16 11:01:02 +04:00
|
|
|
POTENTIAL_QUERY_INSTABILITY,
|
|
|
|
span,
|
2022-10-22 16:32:54 -04:00
|
|
|
QueryInstability { query: cx.tcx.item_name(def_id) },
|
|
|
|
);
|
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)
|
2022-08-30 15:10:28 +10:00
|
|
|
&& lint_ty_kind_usage(cx, &segment.res)
|
2022-05-26 20:22:28 -07: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx Ty<'tcx>) {
|
2019-09-26 17:25:31 +01:00
|
|
|
match &ty.kind {
|
2021-11-07 10:33:27 +01:00
|
|
|
TyKind::Path(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) {
|
|
|
|
Node::Pat(Pat {
|
2022-09-16 11:01:02 +04:00
|
|
|
kind:
|
|
|
|
PatKind::Path(qpath)
|
|
|
|
| PatKind::TupleStruct(qpath, ..)
|
|
|
|
| PatKind::Struct(qpath, ..),
|
|
|
|
..
|
2024-02-09 23:58:36 +03:00
|
|
|
}) => {
|
2022-09-16 11:01:02 +04:00
|
|
|
if let QPath::TypeRelative(qpath_ty, ..) = qpath
|
|
|
|
&& qpath_ty.hir_id == ty.hir_id
|
|
|
|
{
|
|
|
|
Some(path.span)
|
|
|
|
} else {
|
|
|
|
None
|
2022-05-26 20:22:28 -07:00
|
|
|
}
|
2022-09-16 11:01:02 +04:00
|
|
|
}
|
2024-02-09 23:58:36 +03:00
|
|
|
Node::Expr(Expr { kind: ExprKind::Path(qpath), .. }) => {
|
2022-09-16 11:01:02 +04:00
|
|
|
if let QPath::TypeRelative(qpath_ty, ..) = qpath
|
|
|
|
&& qpath_ty.hir_id == ty.hir_id
|
|
|
|
{
|
|
|
|
Some(path.span)
|
|
|
|
} else {
|
|
|
|
None
|
2019-04-24 23:22:54 +02:00
|
|
|
}
|
2022-09-16 11:01:02 +04:00
|
|
|
}
|
|
|
|
// Can't unify these two branches because qpath below is `&&` and above is `&`
|
|
|
|
// and `A | B` paths don't play well together with adjustments, apparently.
|
2024-02-09 23:58:36 +03:00
|
|
|
Node::Expr(Expr { kind: ExprKind::Struct(qpath, ..), .. }) => {
|
2022-09-16 11:01:02 +04:00
|
|
|
if let QPath::TypeRelative(qpath_ty, ..) = qpath
|
|
|
|
&& 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
|
|
|
}
|
2022-09-16 11:01:02 +04:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
match span {
|
|
|
|
Some(span) => {
|
2024-01-16 14:40:39 +11:00
|
|
|
cx.emit_span_lint(
|
2022-10-22 16:32:54 -04:00
|
|
|
USAGE_OF_TY_TYKIND,
|
|
|
|
path.span,
|
|
|
|
TykindKind { suggestion: span },
|
|
|
|
);
|
2022-09-16 11:01:02 +04: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-01-15 15:02:02 +01: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)
|
2022-10-22 16:32:54 -04:00
|
|
|
{
|
2024-01-16 14:40:39 +11:00
|
|
|
cx.emit_span_lint(
|
2022-10-22 16:32:54 -04:00
|
|
|
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
|
|
|
|
2022-05-26 20:22:28 -07:00
|
|
|
fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &Path<'_>) -> Option<String> {
|
|
|
|
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, .. } => {
|
2023-07-11 22:35:29 +01:00
|
|
|
if let ty::Adt(adt, args) = cx.tcx.type_of(did).instantiate_identity().kind() {
|
2022-05-26 20:22:28 -07:00
|
|
|
if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(adt.did())
|
|
|
|
{
|
|
|
|
// NOTE: This path is currently unreachable as `Ty<'tcx>` is
|
|
|
|
// defined as a type alias meaning that `impl<'tcx> Ty<'tcx>`
|
|
|
|
// is not actually allowed.
|
|
|
|
//
|
|
|
|
// I(@lcnr) still kept this branch in so we don't miss this
|
|
|
|
// if we ever change it in the future.
|
2023-07-11 22:35:29 +01:00
|
|
|
return Some(format!("{}<{}>", name, args[0]));
|
2020-09-19 12:34:31 +02:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn gen_args(segment: &PathSegment<'_>) -> String {
|
2019-04-24 23:22:54 +02:00
|
|
|
if let Some(args) = &segment.args {
|
|
|
|
let lifetimes = args
|
|
|
|
.args
|
|
|
|
.iter()
|
|
|
|
.filter_map(|arg| {
|
2022-11-05 22:41:07 +00:00
|
|
|
if let 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
|
|
|
|
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
|
|
|
|
|
|
|
declare_tool_lint! {
|
2023-03-08 16:55:28 +00:00
|
|
|
/// The `existing_doc_keyword` lint detects use `#[doc()]` keywords
|
|
|
|
/// that don't exist, e.g. `#[doc(keyword = "..")]`.
|
2020-11-29 22:34:41 +01:00
|
|
|
pub rustc::EXISTING_DOC_KEYWORD,
|
2020-12-03 15:32:41 +01:00
|
|
|
Allow,
|
2020-11-29 22:34:41 +01:00
|
|
|
"Check that documented keywords in std and core actually exist",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(ExistingDocKeyword => [EXISTING_DOC_KEYWORD]);
|
|
|
|
|
|
|
|
fn is_doc_keyword(s: Symbol) -> bool {
|
|
|
|
s <= kw::Union
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for ExistingDocKeyword {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::Item<'_>) {
|
2021-01-24 13:17:54 +01:00
|
|
|
for attr in cx.tcx.hir().attrs(item.hir_id()) {
|
2020-11-29 22:34:41 +01:00
|
|
|
if !attr.has_name(sym::doc) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(list) = attr.meta_item_list() {
|
|
|
|
for nested in list {
|
|
|
|
if nested.has_name(sym::keyword) {
|
2022-10-22 16:32:54 -04:00
|
|
|
let keyword = nested
|
2020-11-29 22:34:41 +01:00
|
|
|
.value_str()
|
|
|
|
.expect("#[doc(keyword = \"...\")] expected a value!");
|
2022-10-22 16:32:54 -04:00
|
|
|
if is_doc_keyword(keyword) {
|
2020-11-29 22:34:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2024-01-16 14:40:39 +11:00
|
|
|
cx.emit_span_lint(
|
2022-09-16 11:01:02 +04:00
|
|
|
EXISTING_DOC_KEYWORD,
|
|
|
|
attr.span,
|
2023-04-10 22:02:52 +02:00
|
|
|
NonExistentDocKeyword { keyword },
|
2022-09-16 11:01:02 +04: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-02-06 09:51:39 +11:00
|
|
|
Deny,
|
2022-06-10 15:50:06 +01:00
|
|
|
"prevent creation of diagnostics which cannot be translated",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-20 14:12:50 +11:00
|
|
|
/// `DecorateLint` impl, or a `#[derive(Diagnostic)]`, `#[derive(Subdiagnostic)]`,
|
|
|
|
/// `#[derive(DecorateLint)]` expansion.
|
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-02-06 09:51:39 +11:00
|
|
|
Deny,
|
2024-03-06 14:00:16 +11:00
|
|
|
"prevent creation of diagnostics outside of `Diagnostic`/`Subdiagnostic` impls",
|
2022-06-10 15:50:06 +01:00
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
2024-02-20 14:12:50 +11:00
|
|
|
// Only check function calls and method calls.
|
|
|
|
let (span, def_id, fn_gen_args, call_tys) = match expr.kind {
|
|
|
|
ExprKind::Call(callee, args) => {
|
|
|
|
match cx.typeck_results().node_type(callee.hir_id).kind() {
|
|
|
|
&ty::FnDef(def_id, fn_gen_args) => {
|
|
|
|
let call_tys: Vec<_> =
|
|
|
|
args.iter().map(|arg| cx.typeck_results().expr_ty(arg)).collect();
|
|
|
|
(callee.span, def_id, fn_gen_args, call_tys)
|
|
|
|
}
|
|
|
|
_ => return, // occurs for fns passed as args
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 15:09:53 +08:00
|
|
|
ExprKind::MethodCall(_segment, _recv, args, _span) => {
|
|
|
|
let Some((span, def_id, fn_gen_args)) = typeck_results_of_method_fn(cx, expr)
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
};
|
2024-02-20 14:12:50 +11:00
|
|
|
let mut call_tys: Vec<_> =
|
|
|
|
args.iter().map(|arg| cx.typeck_results().expr_ty(arg)).collect();
|
|
|
|
call_tys.insert(0, cx.tcx.types.self_param); // dummy inserted for `self`
|
2024-03-08 15:09:53 +08:00
|
|
|
(span, def_id, fn_gen_args, call_tys)
|
2024-02-20 14:12:50 +11:00
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Is the callee marked with `#[rustc_lint_diagnostics]`?
|
|
|
|
let has_attr = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, fn_gen_args)
|
2022-06-22 11:25:10 +01:00
|
|
|
.ok()
|
2023-05-24 14:33:43 +00:00
|
|
|
.flatten()
|
|
|
|
.is_some_and(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics));
|
2022-06-10 15:50:06 +01:00
|
|
|
|
2024-02-20 14:12:50 +11:00
|
|
|
// Closure: is the type `{D,Subd}iagMessage`?
|
|
|
|
let is_diag_message = |ty: MiddleTy<'_>| {
|
|
|
|
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
|
2022-08-31 12:06:22 +01:00
|
|
|
}
|
2024-02-20 14:12:50 +11:00
|
|
|
};
|
2022-08-31 12:06:22 +01:00
|
|
|
|
2024-02-20 14:12:50 +11:00
|
|
|
// Does the callee have a `impl Into<{D,Subd}iagMessage>` parameter? (There should be at
|
|
|
|
// most one.)
|
|
|
|
let mut impl_into_diagnostic_message_param = None;
|
|
|
|
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() {
|
|
|
|
if let ty::Param(p) = param_ty.kind() {
|
|
|
|
// 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)
|
|
|
|
&& is_diag_message(ty1)
|
|
|
|
{
|
|
|
|
if impl_into_diagnostic_message_param.is_some() {
|
|
|
|
cx.tcx.dcx().span_bug(
|
|
|
|
span,
|
|
|
|
"can't handle multiple `impl Into<{D,Sub}iagMessage>` params",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
impl_into_diagnostic_message_param = Some((i, p.name));
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-20 14:12:50 +11:00
|
|
|
|
|
|
|
// Is the callee interesting?
|
|
|
|
if !has_attr && impl_into_diagnostic_message_param.is_none() {
|
|
|
|
return;
|
2022-06-10 15:50:06 +01:00
|
|
|
}
|
|
|
|
|
2024-02-20 14:12:50 +11:00
|
|
|
// Is the parent method marked with `#[rustc_lint_diagnostics]`?
|
|
|
|
let mut parent_has_attr = false;
|
|
|
|
for (hir_id, _parent) in cx.tcx.hir().parent_iter(expr.hir_id) {
|
|
|
|
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-02-20 14:12:50 +11:00
|
|
|
parent_has_attr = true;
|
2022-06-10 15:50:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-02-20 14:12:50 +11:00
|
|
|
|
|
|
|
// Calls to `#[rustc_lint_diagnostics]`-marked functions should only occur:
|
2024-03-06 14:00:16 +11:00
|
|
|
// - inside an impl of `Diagnostic`, `Subdiagnostic`, or `DecorateLint`, 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.
|
|
|
|
if has_attr && !parent_has_attr {
|
|
|
|
let mut is_inside_appropriate_impl = false;
|
|
|
|
for (_hir_id, parent) in cx.tcx.hir().parent_iter(expr.hir_id) {
|
|
|
|
debug!(?parent);
|
|
|
|
if let Node::Item(Item { kind: ItemKind::Impl(impl_), .. }) = parent
|
|
|
|
&& let Impl { of_trait: Some(of_trait), .. } = impl_
|
|
|
|
&& let Some(def_id) = of_trait.trait_def_id()
|
|
|
|
&& let Some(name) = cx.tcx.get_diagnostic_name(def_id)
|
2024-03-06 14:00:16 +11:00
|
|
|
&& matches!(name, sym::Diagnostic | sym::Subdiagnostic | sym::DecorateLint)
|
2024-02-20 14:12:50 +11:00
|
|
|
{
|
|
|
|
is_inside_appropriate_impl = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!(?is_inside_appropriate_impl);
|
|
|
|
if !is_inside_appropriate_impl {
|
|
|
|
cx.emit_span_lint(DIAGNOSTIC_OUTSIDE_OF_IMPL, span, DiagOutOfImpl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if let Some((param_i, param_i_p_name)) = impl_into_diagnostic_message_param {
|
|
|
|
// Is the arg type `{Sub,D}iagMessage`or `impl Into<{Sub,D}iagMessage>`?
|
|
|
|
let arg_ty = call_tys[param_i];
|
|
|
|
let is_translatable = is_diag_message(arg_ty)
|
|
|
|
|| matches!(arg_ty.kind(), ty::Param(p) if p.name == param_i_p_name);
|
|
|
|
if !is_translatable {
|
|
|
|
cx.emit_span_lint(UNTRANSLATABLE_DIAGNOSTIC, span, UntranslatableDiag);
|
|
|
|
}
|
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 {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|
|
|
let ExprKind::Field(base, target) = expr.kind else { return };
|
|
|
|
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() {
|
|
|
|
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()
|
2022-11-24 15:00:09 +11:00
|
|
|
&& let Some(lit) = item.lit()
|
|
|
|
&& let ast::LitKind::Str(val, _) = lit.kind
|
2022-07-25 13:02:39 +01:00
|
|
|
{
|
2024-01-16 14:40:39 +11:00
|
|
|
cx.emit_span_lint(
|
2022-10-22 16:32:54 -04:00
|
|
|
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 {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
|
2024-01-02 23:32:40 +03:00
|
|
|
if let ExprKind::Binary(BinOp { node: BinOpKind::Eq | BinOpKind::Ne, .. }, lhs, rhs) =
|
|
|
|
expr.kind
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
|
|
|
match &expr.kind {
|
2023-10-16 01:05:11 -07:00
|
|
|
ExprKind::MethodCall(..) => cx
|
|
|
|
.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,
|
|
|
|
}
|
|
|
|
}
|