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};
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::ast::{Ident, Item, ItemKind};
|
2018-12-06 06:50:04 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-01-09 04:18:47 -06:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir::{GenericArg, HirId, MutTy, Mutability, Path, PathSegment, QPath, Ty, TyKind};
|
2020-01-05 03:07:26 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
|
2020-01-20 14:22:36 -06:00
|
|
|
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
2020-01-01 12:30:57 -06:00
|
|
|
use rustc_span::symbol::{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
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DefaultHashTypes {
|
2019-05-14 08:58:22 -05:00
|
|
|
map: FxHashMap<Symbol, Symbol>,
|
2018-12-06 06:50:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DefaultHashTypes {
|
2019-05-14 08:58:22 -05:00
|
|
|
// we are allowed to use `HashMap` and `HashSet` as identifiers for implementing the lint itself
|
2019-08-11 11:55:14 -05:00
|
|
|
#[allow(rustc::default_hash_types)]
|
2018-12-06 06:50:04 -06:00
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut map = FxHashMap::default();
|
2019-05-14 08:58:22 -05:00
|
|
|
map.insert(sym::HashMap, sym::FxHashMap);
|
|
|
|
map.insert(sym::HashSet, sym::FxHashSet);
|
2018-12-06 06:50:04 -06:00
|
|
|
Self { map }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 12:23:11 -05:00
|
|
|
impl_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
|
2018-12-06 06:50:04 -06:00
|
|
|
|
|
|
|
impl EarlyLintPass for DefaultHashTypes {
|
|
|
|
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
|
2019-05-14 08:58:22 -05:00
|
|
|
if let Some(replace) = self.map.get(&ident.name) {
|
2020-01-31 06:24:57 -06:00
|
|
|
cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, |lint| {
|
2020-02-05 09:27:46 -06:00
|
|
|
// FIXME: We can avoid a copy here. Would require us to take String instead of &str.
|
|
|
|
let msg = format!("Prefer {} over {}, it has better performance", replace, ident);
|
2020-01-31 06:24:57 -06:00
|
|
|
lint.build(&msg)
|
|
|
|
.span_suggestion(
|
|
|
|
ident.span,
|
|
|
|
"use",
|
|
|
|
replace.to_string(),
|
|
|
|
Applicability::MaybeIncorrect, // FxHashMap, ... needs another import
|
|
|
|
)
|
2020-02-01 17:47:58 -06:00
|
|
|
.note(&format!(
|
|
|
|
"a `use rustc_data_structures::fx::{}` may be necessary",
|
|
|
|
replace
|
|
|
|
))
|
2020-01-31 06:24:57 -06:00
|
|
|
.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
|
|
|
|
2019-04-24 16:22:54 -05:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
|
2019-11-30 10:46:46 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 10:46:46 -06: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
|
|
|
|
2019-11-30 10:46:46 -06: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
|
|
|
|
2019-11-30 10:46:46 -06: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 {
|
|
|
|
let did = path.res.opt_def_id()?;
|
|
|
|
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())));
|
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 {
|
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
2020-01-13 22:30:20 -06:00
|
|
|
if let ItemKind::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;
|
2020-01-20 14:22:36 -06: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-12-22 16:42:04 -06:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|