2018-12-06 06:50:04 -06:00
|
|
|
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
|
|
|
|
//! Clippy.
|
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
2021-07-02 14:15:11 -05:00
|
|
|
use rustc_ast as ast;
|
2022-06-27 08:46:45 -05:00
|
|
|
use rustc_errors::{fluent, Applicability};
|
2020-09-19 05:34:31 -05:00
|
|
|
use rustc_hir::def::Res;
|
2022-06-10 09:50:06 -05:00
|
|
|
use rustc_hir::{def_id::DefId, Expr, ExprKind, GenericArg, PatKind, Path, PathSegment, QPath};
|
|
|
|
use rustc_hir::{HirId, Impl, Item, ItemKind, Node, Pat, Ty, TyKind};
|
2020-09-19 05:34:31 -05:00
|
|
|
use rustc_middle::ty;
|
2021-07-02 14:15:11 -05:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-20 14:22:36 -06: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 09:50:06 -05:00
|
|
|
use rustc_span::Span;
|
2018-12-06 06:50:04 -06:00
|
|
|
|
2019-06-24 03:43:51 -05:00
|
|
|
declare_tool_lint! {
|
|
|
|
pub rustc::DEFAULT_HASH_TYPES,
|
2019-03-16 08:59:34 -05:00
|
|
|
Allow,
|
2020-01-03 19:27:14 -06:00
|
|
|
"forbid HashMap and HashSet and suggest the FxHash* variants",
|
|
|
|
report_in_external_macro: true
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
|
|
|
|
2021-07-02 14:15:11 -05:00
|
|
|
declare_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
|
2018-12-06 06:50:04 -06: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-18 17:48:49 -06:00
|
|
|
let Res::Def(rustc_hir::def::DefKind::Struct, def_id) = path.res else { return };
|
2021-07-02 14:15:11 -05:00
|
|
|
if matches!(cx.tcx.hir().get(hir_id), Node::Item(Item { kind: ItemKind::Use(..), .. })) {
|
|
|
|
// don't lint imports, only actual usages
|
|
|
|
return;
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
2021-10-04 16:11:22 -05:00
|
|
|
let replace = match cx.tcx.get_diagnostic_name(def_id) {
|
|
|
|
Some(sym::HashMap) => "FxHashMap",
|
|
|
|
Some(sym::HashSet) => "FxHashSet",
|
|
|
|
_ => return,
|
2021-07-02 14:15:11 -05:00
|
|
|
};
|
|
|
|
cx.struct_span_lint(DEFAULT_HASH_TYPES, path.span, |lint| {
|
2022-06-27 08:46:45 -05:00
|
|
|
lint.build(fluent::lint::default_hash_types)
|
|
|
|
.set_arg("preferred", replace)
|
|
|
|
.set_arg("used", cx.tcx.item_name(def_id))
|
|
|
|
.note(fluent::lint::note)
|
2021-07-02 14:15:11 -05:00
|
|
|
.emit();
|
|
|
|
});
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 09:50:06 -05:00
|
|
|
/// Helper function for lints that check for expressions with calls and use typeck results to
|
|
|
|
/// get the `DefId` and `SubstsRef` of the function.
|
|
|
|
fn typeck_results_of_method_fn<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &Expr<'_>,
|
|
|
|
) -> Option<(Span, DefId, ty::subst::SubstsRef<'tcx>)> {
|
|
|
|
match expr.kind {
|
2022-08-31 23:27:31 -05:00
|
|
|
ExprKind::MethodCall(segment, ..)
|
2022-06-10 09:50:06 -05:00
|
|
|
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
|
|
|
|
{
|
|
|
|
Some((segment.ident.span, def_id, cx.typeck_results().node_substs(expr.hir_id)))
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
match cx.typeck_results().node_type(expr.hir_id).kind() {
|
|
|
|
&ty::FnDef(def_id, substs) => Some((expr.span, def_id, substs)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 06:02:16 -06:00
|
|
|
declare_tool_lint! {
|
|
|
|
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<'_>) {
|
2022-06-10 09:50:06 -05:00
|
|
|
let Some((span, def_id, substs)) = typeck_results_of_method_fn(cx, expr) else { return };
|
2022-01-05 06:02:16 -06:00
|
|
|
if let Ok(Some(instance)) = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs) {
|
|
|
|
let def_id = instance.def_id();
|
|
|
|
if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) {
|
|
|
|
cx.struct_span_lint(POTENTIAL_QUERY_INSTABILITY, span, |lint| {
|
2022-06-27 08:50:58 -05:00
|
|
|
lint.build(fluent::lint::query_instability)
|
|
|
|
.set_arg("query", cx.tcx.item_name(def_id))
|
|
|
|
.note(fluent::lint::note)
|
2022-01-05 06:02:16 -06:00
|
|
|
.emit();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 03:43:51 -05:00
|
|
|
declare_tool_lint! {
|
|
|
|
pub rustc::USAGE_OF_TY_TYKIND,
|
2019-03-16 08:59:34 -05:00
|
|
|
Allow,
|
2020-01-03 19:27:14 -06:00
|
|
|
"usage of `ty::TyKind` outside of the `ty::sty` module",
|
|
|
|
report_in_external_macro: true
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
|
|
|
|
2019-06-24 03:43:51 -05:00
|
|
|
declare_tool_lint! {
|
|
|
|
pub rustc::USAGE_OF_QUALIFIED_TY,
|
2019-04-24 16:22:54 -05:00
|
|
|
Allow,
|
2020-01-03 19:27:14 -06:00
|
|
|
"using `ty::{Ty,TyCtxt}` instead of importing it",
|
|
|
|
report_in_external_macro: true
|
2019-04-24 16:22:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(TyTyKind => [
|
|
|
|
USAGE_OF_TY_TYKIND,
|
|
|
|
USAGE_OF_QUALIFIED_TY,
|
|
|
|
]);
|
2018-12-06 06:50:04 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for TyTyKind {
|
2022-05-26 22:22:28 -05:00
|
|
|
fn check_path(
|
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
path: &'tcx rustc_hir::Path<'tcx>,
|
|
|
|
_: rustc_hir::HirId,
|
|
|
|
) {
|
|
|
|
if let Some(segment) = path.segments.iter().nth_back(1)
|
2022-08-30 00:10:28 -05:00
|
|
|
&& lint_ty_kind_usage(cx, &segment.res)
|
2022-05-26 22:22:28 -05:00
|
|
|
{
|
|
|
|
let span = path.span.with_hi(
|
|
|
|
segment.args.map_or(segment.ident.span, |a| a.span_ext).hi()
|
|
|
|
);
|
|
|
|
cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, |lint| {
|
2022-06-27 09:00:01 -05:00
|
|
|
lint.build(fluent::lint::tykind_kind)
|
2022-05-26 22:22:28 -05:00
|
|
|
.span_suggestion(
|
|
|
|
span,
|
2022-06-27 09:00:01 -05:00
|
|
|
fluent::lint::suggestion,
|
2022-06-13 01:48:40 -05:00
|
|
|
"ty",
|
2022-05-26 22:22:28 -05:00
|
|
|
Applicability::MaybeIncorrect, // ty maybe needs an import
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
});
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx Ty<'tcx>) {
|
2019-09-26 11:25:31 -05:00
|
|
|
match &ty.kind {
|
2021-11-07 03:33:27 -06:00
|
|
|
TyKind::Path(QPath::Resolved(_, path)) => {
|
2022-05-26 22:22:28 -05:00
|
|
|
if lint_ty_kind_usage(cx, &path.res) {
|
|
|
|
cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, |lint| {
|
|
|
|
let hir = cx.tcx.hir();
|
|
|
|
match hir.find(hir.get_parent_node(ty.hir_id)) {
|
|
|
|
Some(Node::Pat(Pat {
|
|
|
|
kind:
|
|
|
|
PatKind::Path(qpath)
|
|
|
|
| PatKind::TupleStruct(qpath, ..)
|
|
|
|
| PatKind::Struct(qpath, ..),
|
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
if let QPath::TypeRelative(qpath_ty, ..) = qpath
|
|
|
|
&& qpath_ty.hir_id == ty.hir_id
|
|
|
|
{
|
2022-06-27 09:00:01 -05:00
|
|
|
lint.build(fluent::lint::tykind_kind)
|
2022-05-26 22:22:28 -05:00
|
|
|
.span_suggestion(
|
|
|
|
path.span,
|
2022-06-27 09:00:01 -05:00
|
|
|
fluent::lint::suggestion,
|
2022-06-13 01:48:40 -05:00
|
|
|
"ty",
|
2022-05-26 22:22:28 -05:00
|
|
|
Applicability::MaybeIncorrect, // ty maybe needs an import
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Node::Expr(Expr {
|
|
|
|
kind: ExprKind::Path(qpath),
|
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
if let QPath::TypeRelative(qpath_ty, ..) = qpath
|
|
|
|
&& qpath_ty.hir_id == ty.hir_id
|
|
|
|
{
|
2022-06-27 09:00:01 -05:00
|
|
|
lint.build(fluent::lint::tykind_kind)
|
2021-11-07 03:33:27 -06:00
|
|
|
.span_suggestion(
|
|
|
|
path.span,
|
2022-06-27 09:00:01 -05:00
|
|
|
fluent::lint::suggestion,
|
2022-06-13 01:48:40 -05:00
|
|
|
"ty",
|
2022-05-26 22:22:28 -05:00
|
|
|
Applicability::MaybeIncorrect, // ty maybe needs an import
|
2021-11-07 03:33:27 -06:00
|
|
|
)
|
|
|
|
.emit();
|
2022-05-26 22:22:28 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-04-24 16:22:54 -05:00
|
|
|
}
|
2022-05-26 22:22:28 -05: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.
|
|
|
|
Some(Node::Expr(Expr {
|
|
|
|
kind: ExprKind::Struct(qpath, ..),
|
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
if let QPath::TypeRelative(qpath_ty, ..) = qpath
|
|
|
|
&& qpath_ty.hir_id == ty.hir_id
|
|
|
|
{
|
2022-06-27 09:00:01 -05:00
|
|
|
lint.build(fluent::lint::tykind_kind)
|
2022-05-26 22:22:28 -05:00
|
|
|
.span_suggestion(
|
|
|
|
path.span,
|
2022-06-27 09:00:01 -05:00
|
|
|
fluent::lint::suggestion,
|
2022-06-13 01:48:40 -05:00
|
|
|
"ty",
|
2022-05-26 22:22:28 -05:00
|
|
|
Applicability::MaybeIncorrect, // ty maybe needs an import
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2019-04-24 16:22:54 -05:00
|
|
|
}
|
2022-06-27 09:00:01 -05:00
|
|
|
lint.build(fluent::lint::tykind).help(fluent::lint::help).emit();
|
2022-05-26 22:22:28 -05:00
|
|
|
})
|
|
|
|
} else if !ty.span.from_expansion() && let Some(t) = is_ty_or_ty_ctxt(cx, &path) {
|
|
|
|
if path.segments.len() > 1 {
|
|
|
|
cx.struct_span_lint(USAGE_OF_QUALIFIED_TY, path.span, |lint| {
|
2022-06-27 09:00:01 -05:00
|
|
|
lint.build(fluent::lint::ty_qualified)
|
|
|
|
.set_arg("ty", t.clone())
|
2022-05-26 22:22:28 -05:00
|
|
|
.span_suggestion(
|
|
|
|
path.span,
|
2022-06-27 09:00:01 -05:00
|
|
|
fluent::lint::suggestion,
|
2022-05-26 22:22:28 -05:00
|
|
|
t,
|
|
|
|
// The import probably needs to be changed
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
})
|
2019-04-24 16:22:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 11:03:45 -05:00
|
|
|
|
2022-05-26 22:22:28 -05: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 11:03:45 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 16:22:54 -05:00
|
|
|
|
2022-05-26 22:22:28 -05: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 03:33:27 -06:00
|
|
|
}
|
2022-05-26 22:22:28 -05:00
|
|
|
}
|
|
|
|
// Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
|
|
|
|
Res::SelfTy { trait_: None, alias_to: Some((did, _)) } => {
|
|
|
|
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
|
|
|
|
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.
|
|
|
|
return Some(format!("{}<{}>", name, substs[0]));
|
2020-09-19 05:34:31 -05:00
|
|
|
}
|
2019-04-24 16:22:54 -05:00
|
|
|
}
|
|
|
|
}
|
2022-05-26 22:22:28 -05:00
|
|
|
_ => (),
|
2019-04-24 16:22:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-11-30 10:46:46 -06:00
|
|
|
fn gen_args(segment: &PathSegment<'_>) -> String {
|
2019-04-24 16:22:54 -05:00
|
|
|
if let Some(args) = &segment.args {
|
|
|
|
let lifetimes = args
|
|
|
|
.args
|
|
|
|
.iter()
|
|
|
|
.filter_map(|arg| {
|
|
|
|
if let GenericArg::Lifetime(lt) = arg {
|
|
|
|
Some(lt.name.ident().to_string())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if !lifetimes.is_empty() {
|
|
|
|
return format!("<{}>", lifetimes.join(", "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String::new()
|
|
|
|
}
|
2019-05-02 09:53:12 -05:00
|
|
|
|
2019-06-24 03:43:51 -05:00
|
|
|
declare_tool_lint! {
|
|
|
|
pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
|
2019-05-02 09:53:12 -05: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 02:43:49 -06:00
|
|
|
if let ast::ItemKind::Impl(box ast::Impl { of_trait: Some(lint_pass), .. }) = &item.kind {
|
2019-06-13 08:49:33 -05:00
|
|
|
if let Some(last) = lint_pass.path.segments.last() {
|
|
|
|
if last.ident.name == sym::LintPass {
|
2019-08-13 15:56:42 -05:00
|
|
|
let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
|
|
|
|
let call_site = expn_data.call_site;
|
2021-07-10 14:14:52 -05: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)
|
|
|
|
{
|
2019-08-10 19:00:05 -05:00
|
|
|
cx.struct_span_lint(
|
|
|
|
LINT_PASS_IMPL_WITHOUT_MACRO,
|
|
|
|
lint_pass.path.span,
|
2020-01-31 06:24:57 -06:00
|
|
|
|lint| {
|
2022-06-27 09:09:24 -05:00
|
|
|
lint.build(fluent::lint::lintpass_by_hand)
|
|
|
|
.help(fluent::lint::help)
|
2020-01-31 06:24:57 -06:00
|
|
|
.emit();
|
|
|
|
},
|
2019-08-10 19:00:05 -05:00
|
|
|
)
|
2019-05-02 09:53:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 15:34:41 -06:00
|
|
|
|
|
|
|
declare_tool_lint! {
|
|
|
|
pub rustc::EXISTING_DOC_KEYWORD,
|
2020-12-03 08:32:41 -06:00
|
|
|
Allow,
|
2020-11-29 15:34:41 -06: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 06:17:54 -06:00
|
|
|
for attr in cx.tcx.hir().attrs(item.hir_id()) {
|
2020-11-29 15:34:41 -06: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) {
|
|
|
|
let v = nested
|
|
|
|
.value_str()
|
|
|
|
.expect("#[doc(keyword = \"...\")] expected a value!");
|
|
|
|
if is_doc_keyword(v) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cx.struct_span_lint(EXISTING_DOC_KEYWORD, attr.span, |lint| {
|
2022-06-27 09:18:30 -05:00
|
|
|
lint.build(fluent::lint::non_existant_doc_keyword)
|
|
|
|
.set_arg("keyword", v)
|
|
|
|
.help(fluent::lint::help)
|
|
|
|
.emit();
|
2020-11-29 15:34:41 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 09:50:06 -05:00
|
|
|
|
|
|
|
declare_tool_lint! {
|
|
|
|
pub rustc::UNTRANSLATABLE_DIAGNOSTIC,
|
|
|
|
Allow,
|
|
|
|
"prevent creation of diagnostics which cannot be translated",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_tool_lint! {
|
|
|
|
pub rustc::DIAGNOSTIC_OUTSIDE_OF_IMPL,
|
|
|
|
Allow,
|
2022-09-18 10:46:56 -05:00
|
|
|
"prevent creation of diagnostics outside of `IntoDiagnostic`/`AddToDiagnostic` impls",
|
2022-06-10 09:50:06 -05:00
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(Diagnostics => [ UNTRANSLATABLE_DIAGNOSTIC, DIAGNOSTIC_OUTSIDE_OF_IMPL ]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for Diagnostics {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|
|
|
let Some((span, def_id, substs)) = typeck_results_of_method_fn(cx, expr) else { return };
|
|
|
|
debug!(?span, ?def_id, ?substs);
|
2022-06-22 05:25:10 -05:00
|
|
|
let has_attr = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs)
|
|
|
|
.ok()
|
|
|
|
.and_then(|inst| inst)
|
|
|
|
.map(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics))
|
|
|
|
.unwrap_or(false);
|
|
|
|
if !has_attr {
|
2022-06-10 09:50:06 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-31 06:06:22 -05:00
|
|
|
let mut found_parent_with_attr = false;
|
2022-06-10 09:50:06 -05:00
|
|
|
let mut found_impl = false;
|
2022-08-31 06:06:22 -05:00
|
|
|
for (hir_id, parent) in cx.tcx.hir().parent_iter(expr.hir_id) {
|
|
|
|
if let Some(owner_did) = hir_id.as_owner() {
|
|
|
|
found_parent_with_attr = found_parent_with_attr
|
|
|
|
|| cx.tcx.has_attr(owner_did.to_def_id(), sym::rustc_lint_diagnostics);
|
|
|
|
}
|
|
|
|
|
2022-06-10 09:50:06 -05:00
|
|
|
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) &&
|
2022-09-14 23:01:44 -05:00
|
|
|
matches!(name, sym::IntoDiagnostic | sym::AddToDiagnostic | sym::DecorateLint)
|
2022-06-10 09:50:06 -05:00
|
|
|
{
|
|
|
|
found_impl = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!(?found_impl);
|
2022-08-31 06:06:22 -05:00
|
|
|
if !found_parent_with_attr && !found_impl {
|
2022-06-10 09:50:06 -05:00
|
|
|
cx.struct_span_lint(DIAGNOSTIC_OUTSIDE_OF_IMPL, span, |lint| {
|
2022-06-27 09:27:41 -05:00
|
|
|
lint.build(fluent::lint::diag_out_of_impl).emit();
|
2022-06-10 09:50:06 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut found_diagnostic_message = false;
|
|
|
|
for ty in substs.types() {
|
|
|
|
debug!(?ty);
|
|
|
|
if let Some(adt_def) = ty.ty_adt_def() &&
|
|
|
|
let Some(name) = cx.tcx.get_diagnostic_name(adt_def.did()) &&
|
|
|
|
matches!(name, sym::DiagnosticMessage | sym::SubdiagnosticMessage)
|
|
|
|
{
|
|
|
|
found_diagnostic_message = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!(?found_diagnostic_message);
|
2022-08-31 06:06:22 -05:00
|
|
|
if !found_parent_with_attr && !found_diagnostic_message {
|
2022-06-10 09:50:06 -05:00
|
|
|
cx.struct_span_lint(UNTRANSLATABLE_DIAGNOSTIC, span, |lint| {
|
2022-06-27 09:27:41 -05:00
|
|
|
lint.build(fluent::lint::untranslatable_diag).emit();
|
2022-06-10 09:50:06 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 07:02:39 -05:00
|
|
|
|
|
|
|
declare_tool_lint! {
|
|
|
|
pub rustc::BAD_OPT_ACCESS,
|
|
|
|
Deny,
|
|
|
|
"prevent using options by field access when there is a wrapper function",
|
|
|
|
report_in_external_macro: true
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(BadOptAccess => [ BAD_OPT_ACCESS ]);
|
|
|
|
|
|
|
|
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() &&
|
|
|
|
let Some(literal) = item.literal() &&
|
|
|
|
let ast::LitKind::Str(val, _) = literal.kind
|
|
|
|
{
|
|
|
|
cx.struct_span_lint(BAD_OPT_ACCESS, expr.span, |lint| {
|
|
|
|
lint.build(val.as_str()).emit(); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|