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;
|
2020-01-09 04:18:47 -06:00
|
|
|
use rustc_errors::Applicability;
|
2020-09-19 05:34:31 -05:00
|
|
|
use rustc_hir::def::Res;
|
2021-07-02 14:15:11 -05:00
|
|
|
use rustc_hir::{
|
|
|
|
GenericArg, HirId, Item, ItemKind, MutTy, Mutability, Node, Path, PathSegment, QPath, 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};
|
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) {
|
|
|
|
let def_id = match path.res {
|
|
|
|
Res::Def(rustc_hir::def::DefKind::Struct, id) => id,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
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-02 18:51:01 -05:00
|
|
|
let replace = if cx.tcx.is_diagnostic_item(sym::HashMap, def_id) {
|
2021-07-02 14:15:11 -05:00
|
|
|
"FxHashMap"
|
2021-10-02 18:51:01 -05:00
|
|
|
} else if cx.tcx.is_diagnostic_item(sym::HashSet, def_id) {
|
2021-07-02 14:15:11 -05:00
|
|
|
"FxHashSet"
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
cx.struct_span_lint(DEFAULT_HASH_TYPES, path.span, |lint| {
|
|
|
|
let msg = format!(
|
|
|
|
"prefer `{}` over `{}`, it has better performance",
|
|
|
|
replace,
|
|
|
|
cx.tcx.item_name(def_id)
|
|
|
|
);
|
|
|
|
lint.build(&msg)
|
|
|
|
.note(&format!("a `use rustc_data_structures::fx::{}` may be necessary", replace))
|
|
|
|
.emit();
|
|
|
|
});
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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::TY_PASS_BY_REFERENCE,
|
2019-04-24 16:22:54 -05:00
|
|
|
Allow,
|
2020-01-03 19:27:14 -06:00
|
|
|
"passing `Ty` or `TyCtxt` by reference",
|
|
|
|
report_in_external_macro: true
|
2019-04-24 16:22:54 -05: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,
|
|
|
|
TY_PASS_BY_REFERENCE,
|
|
|
|
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 {
|
|
|
|
fn check_path(&mut self, cx: &LateContext<'_>, path: &'tcx Path<'tcx>, _: HirId) {
|
2019-03-21 11:03:45 -05:00
|
|
|
let segments = path.segments.iter().rev().skip(1).rev();
|
|
|
|
|
|
|
|
if let Some(last) = segments.last() {
|
|
|
|
let span = path.span.with_hi(last.ident.span.hi());
|
|
|
|
if lint_ty_kind_usage(cx, last) {
|
2020-01-31 06:24:57 -06:00
|
|
|
cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, |lint| {
|
|
|
|
lint.build("usage of `ty::TyKind::<kind>`")
|
2020-02-01 17:47:58 -06:00
|
|
|
.span_suggestion(
|
|
|
|
span,
|
|
|
|
"try using ty::<kind> directly",
|
|
|
|
"ty".to_string(),
|
|
|
|
Applicability::MaybeIncorrect, // ty maybe needs an import
|
|
|
|
)
|
|
|
|
.emit();
|
2020-01-31 06:24:57 -06:00
|
|
|
})
|
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 {
|
2019-04-24 16:22:54 -05:00
|
|
|
TyKind::Path(qpath) => {
|
|
|
|
if let QPath::Resolved(_, path) = qpath {
|
|
|
|
if let Some(last) = path.segments.iter().last() {
|
|
|
|
if lint_ty_kind_usage(cx, last) {
|
2020-02-01 17:47:58 -06:00
|
|
|
cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, |lint| {
|
|
|
|
lint.build("usage of `ty::TyKind`")
|
|
|
|
.help("try using `Ty` instead")
|
|
|
|
.emit();
|
|
|
|
})
|
2019-04-24 16:22:54 -05:00
|
|
|
} else {
|
2019-08-10 17:08:30 -05:00
|
|
|
if ty.span.from_expansion() {
|
2019-04-24 16:22:54 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {
|
|
|
|
if path.segments.len() > 1 {
|
2020-02-01 17:47:58 -06:00
|
|
|
cx.struct_span_lint(USAGE_OF_QUALIFIED_TY, path.span, |lint| {
|
|
|
|
lint.build(&format!("usage of qualified `ty::{}`", t))
|
|
|
|
.span_suggestion(
|
|
|
|
path.span,
|
|
|
|
"try using it unqualified",
|
|
|
|
t,
|
|
|
|
// The import probably needs to be changed
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
})
|
2019-04-24 16:22:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 10:28:40 -06:00
|
|
|
TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::Not }) => {
|
2020-03-18 13:27:59 -05:00
|
|
|
if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner.to_def_id()) {
|
2019-04-24 16:22:54 -05:00
|
|
|
if cx.tcx.impl_trait_ref(impl_did).is_some() {
|
|
|
|
return;
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 16:22:54 -05:00
|
|
|
if let Some(t) = is_ty_or_ty_ctxt(cx, &inner_ty) {
|
2020-02-01 17:47:58 -06:00
|
|
|
cx.struct_span_lint(TY_PASS_BY_REFERENCE, ty.span, |lint| {
|
|
|
|
lint.build(&format!("passing `{}` by reference", t))
|
2020-01-31 06:24:57 -06:00
|
|
|
.span_suggestion(
|
|
|
|
ty.span,
|
|
|
|
"try passing by value",
|
|
|
|
t,
|
|
|
|
// Changing type of function argument
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
2020-02-01 17:47:58 -06:00
|
|
|
})
|
2019-04-24 16:22:54 -05:00
|
|
|
}
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
2019-04-24 16:22:54 -05:00
|
|
|
_ => {}
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 11:03:45 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn lint_ty_kind_usage(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> bool {
|
2019-05-19 13:16:04 -05:00
|
|
|
if let Some(res) = segment.res {
|
|
|
|
if let Some(did) = res.opt_def_id() {
|
|
|
|
return cx.tcx.is_diagnostic_item(sym::TyKind, did);
|
2019-03-21 11:03:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
2019-04-24 16:22:54 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<String> {
|
2020-03-22 07:36:56 -05:00
|
|
|
if let TyKind::Path(qpath) = &ty.kind {
|
|
|
|
if let QPath::Resolved(_, path) = qpath {
|
2020-09-19 05:34:31 -05:00
|
|
|
match path.res {
|
|
|
|
Res::Def(_, did) => {
|
|
|
|
if cx.tcx.is_diagnostic_item(sym::Ty, did) {
|
|
|
|
return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
|
|
|
|
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, did) {
|
|
|
|
return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
|
|
|
|
Res::SelfTy(None, Some((did, _))) => {
|
|
|
|
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
|
|
|
|
if cx.tcx.is_diagnostic_item(sym::Ty, adt.did) {
|
2020-09-21 13:28:52 -05:00
|
|
|
// 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.
|
2020-09-19 05:34:31 -05:00
|
|
|
return Some(format!("Ty<{}>", substs[0]));
|
|
|
|
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, adt.did) {
|
|
|
|
return Some(format!("TyCtxt<{}>", substs[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
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) {
|
|
|
|
if let ast::ItemKind::Impl(box ast::ImplKind { 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| {
|
|
|
|
lint.build("implementing `LintPass` by hand")
|
|
|
|
.help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")
|
|
|
|
.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| {
|
|
|
|
lint.build(&format!(
|
|
|
|
"Found non-existing keyword `{}` used in \
|
|
|
|
`#[doc(keyword = \"...\")]`",
|
|
|
|
v,
|
|
|
|
))
|
|
|
|
.help("only existing keywords are allowed in core/std")
|
|
|
|
.emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|