2018-11-27 20:14:15 +00:00
|
|
|
use crate::utils::{
|
|
|
|
match_def_path, match_type, paths, span_help_and_lint, span_lint, span_lint_and_sugg, walk_ptrs_ty,
|
|
|
|
};
|
|
|
|
use if_chain::if_chain;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::def::Def;
|
|
|
|
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc::{declare_tool_lint, lint_array};
|
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use syntax::ast::{Crate as AstCrate, Ident, ItemKind, Name};
|
|
|
|
use syntax::source_map::Span;
|
|
|
|
use syntax::symbol::LocalInternedString;
|
2016-07-18 09:19:33 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for various things we like to keep tidy in clippy.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** We like to pretend we're an example of tidy code.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:** Wrong ordering of the util::paths constants.
|
2016-08-06 08:18:36 +00:00
|
|
|
pub CLIPPY_LINTS_INTERNAL,
|
2018-03-28 13:24:26 +00:00
|
|
|
internal,
|
2016-08-06 08:18:36 +00:00
|
|
|
"various things that will negatively affect your clippy experience"
|
2016-07-18 09:19:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Ensures every lint is associated to a `LintPass`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The compiler only knows lints via a `LintPass`. Without
|
|
|
|
/// putting a lint to a `LintPass::get_lints()`'s return, the compiler will not
|
|
|
|
/// know the name of the lint.
|
|
|
|
///
|
|
|
|
/// **Known problems:** Only checks for lints associated using the `lint_array!`
|
|
|
|
/// macro.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// declare_lint! { pub LINT_1, ... }
|
|
|
|
/// declare_lint! { pub LINT_2, ... }
|
|
|
|
/// declare_lint! { pub FORGOTTEN_LINT, ... }
|
|
|
|
/// // ...
|
|
|
|
/// pub struct Pass;
|
|
|
|
/// impl LintPass for Pass {
|
|
|
|
/// fn get_lints(&self) -> LintArray {
|
|
|
|
/// lint_array![LINT_1, LINT_2]
|
|
|
|
/// // missing FORGOTTEN_LINT
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-09-01 07:07:37 +00:00
|
|
|
pub LINT_WITHOUT_LINT_PASS,
|
2018-03-28 13:24:26 +00:00
|
|
|
internal,
|
2016-09-01 07:07:37 +00:00
|
|
|
"declaring a lint without associating it in a LintPass"
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:58:23 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for the presence of the default hash types "HashMap" or "HashSet"
|
|
|
|
/// and recommends the FxHash* variants.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The FxHash variants have better performance
|
|
|
|
/// and we don't need any collision prevention in clippy.
|
2018-08-10 17:58:23 +00:00
|
|
|
pub DEFAULT_HASH_TYPES,
|
|
|
|
internal,
|
|
|
|
"forbid HashMap and HashSet and suggest the FxHash* variants"
|
|
|
|
}
|
|
|
|
|
2018-09-14 10:38:09 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for calls to `cx.span_lint*` and suggests to use the `utils::*`
|
|
|
|
/// variant of the function.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The `utils::*` variants also add a link to the Clippy documentation to the
|
|
|
|
/// warning/error messages.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// Bad:
|
|
|
|
/// ```rust
|
|
|
|
/// cx.span_lint(LINT_NAME, "message");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Good:
|
|
|
|
/// ```rust
|
|
|
|
/// utils::span_lint(cx, LINT_NAME, "message");
|
|
|
|
/// ```
|
2018-09-14 10:38:09 +00:00
|
|
|
pub COMPILER_LINT_FUNCTIONS,
|
|
|
|
internal,
|
|
|
|
"usage of the lint functions of the compiler instead of the utils::* variant"
|
|
|
|
}
|
2018-08-10 17:58:23 +00:00
|
|
|
|
2016-07-18 09:19:33 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Clippy;
|
|
|
|
|
|
|
|
impl LintPass for Clippy {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(CLIPPY_LINTS_INTERNAL)
|
|
|
|
}
|
2019-01-26 19:40:55 +00:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"ClippyLintsInternal"
|
|
|
|
}
|
2016-07-18 09:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for Clippy {
|
2018-07-23 11:01:12 +00:00
|
|
|
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &AstCrate) {
|
2018-11-27 20:14:15 +00:00
|
|
|
if let Some(utils) = krate.module.items.iter().find(|item| item.ident.name == "utils") {
|
2016-07-18 09:19:33 +00:00
|
|
|
if let ItemKind::Mod(ref utils_mod) = utils.node {
|
2018-11-27 20:14:15 +00:00
|
|
|
if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name == "paths") {
|
2016-07-18 09:19:33 +00:00
|
|
|
if let ItemKind::Mod(ref paths_mod) = paths.node {
|
2018-04-28 10:56:31 +00:00
|
|
|
let mut last_name: Option<LocalInternedString> = None;
|
2016-07-18 09:19:33 +00:00
|
|
|
for item in &paths_mod.items {
|
2018-06-28 13:46:58 +00:00
|
|
|
let name = item.ident.as_str();
|
2016-07-18 09:19:33 +00:00
|
|
|
if let Some(ref last_name) = last_name {
|
|
|
|
if **last_name > *name {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
CLIPPY_LINTS_INTERNAL,
|
|
|
|
item.span,
|
|
|
|
"this constant should be before the previous constant due to lexical \
|
2017-09-05 09:33:04 +00:00
|
|
|
ordering",
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2016-07-18 09:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
last_name = Some(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 07:07:37 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct LintWithoutLintPass {
|
2018-09-11 23:34:52 +00:00
|
|
|
declared_lints: FxHashMap<Name, Span>,
|
|
|
|
registered_lints: FxHashSet<Name>,
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for LintWithoutLintPass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(LINT_WITHOUT_LINT_PASS)
|
|
|
|
}
|
2019-01-26 19:40:55 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"LintWithoutLintPass"
|
|
|
|
}
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
|
2018-10-29 19:37:47 +00:00
|
|
|
if let hir::ItemKind::Static(ref ty, MutImmutable, _) = item.node {
|
2018-10-12 06:09:04 +00:00
|
|
|
if is_lint_ref_type(cx, ty) {
|
2018-12-30 00:09:24 +00:00
|
|
|
self.declared_lints.insert(item.ident.name, item.span);
|
2018-10-29 19:37:47 +00:00
|
|
|
}
|
|
|
|
} else if let hir::ItemKind::Impl(.., Some(ref trait_ref), _, ref impl_item_refs) = item.node {
|
|
|
|
if_chain! {
|
|
|
|
if let hir::TraitRef{path, ..} = trait_ref;
|
|
|
|
if let Def::Trait(def_id) = path.def;
|
|
|
|
if match_def_path(cx.tcx, def_id, &paths::LINT_PASS);
|
|
|
|
then {
|
2018-07-14 22:00:27 +00:00
|
|
|
let mut collector = LintCollector {
|
|
|
|
output: &mut self.registered_lints,
|
|
|
|
cx,
|
|
|
|
};
|
2019-03-01 12:26:06 +00:00
|
|
|
let node_id = cx.tcx.hir().hir_to_node_id(impl_item_refs[0].id.hir_id);
|
|
|
|
let body_id = cx.tcx.hir().body_owned_by(node_id);
|
2018-12-08 00:56:03 +00:00
|
|
|
collector.visit_expr(&cx.tcx.hir().body(body_id).value);
|
2018-07-14 22:00:27 +00:00
|
|
|
}
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
fn check_crate_post(&mut self, cx: &LateContext<'a, 'tcx>, _: &'tcx Crate) {
|
2016-09-01 07:07:37 +00:00
|
|
|
for (lint_name, &lint_span) in &self.declared_lints {
|
2018-07-29 09:04:40 +00:00
|
|
|
// When using the `declare_tool_lint!` macro, the original `lint_span`'s
|
2016-09-01 07:07:37 +00:00
|
|
|
// file points to "<rustc macros>".
|
|
|
|
// `compiletest-rs` thinks that's an error in a different file and
|
|
|
|
// just ignores it. This causes the test in compile-fail/lint_pass
|
|
|
|
// not able to capture the error.
|
|
|
|
// Therefore, we need to climb the macro expansion tree and find the
|
2018-07-29 09:04:40 +00:00
|
|
|
// actual span that invoked `declare_tool_lint!`:
|
2017-08-09 07:30:56 +00:00
|
|
|
let lint_span = lint_span
|
2017-08-31 12:47:45 +00:00
|
|
|
.ctxt()
|
2017-08-09 07:30:56 +00:00
|
|
|
.outer()
|
|
|
|
.expn_info()
|
|
|
|
.map(|ei| ei.call_site)
|
|
|
|
.expect("unable to get call_site");
|
2016-09-01 07:07:37 +00:00
|
|
|
|
|
|
|
if !self.registered_lints.contains(lint_name) {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
LINT_WITHOUT_LINT_PASS,
|
|
|
|
lint_span,
|
|
|
|
&format!("the lint `{}` is not added to any `LintPass`", lint_name),
|
|
|
|
);
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 06:09:04 +00:00
|
|
|
fn is_lint_ref_type<'tcx>(cx: &LateContext<'_, 'tcx>, ty: &Ty) -> bool {
|
2018-07-12 08:03:06 +00:00
|
|
|
if let TyKind::Rptr(
|
2017-10-31 17:03:54 +00:00
|
|
|
_,
|
2017-09-05 09:33:04 +00:00
|
|
|
MutTy {
|
|
|
|
ty: ref inner,
|
|
|
|
mutbl: MutImmutable,
|
|
|
|
},
|
2018-11-27 20:14:15 +00:00
|
|
|
) = ty.node
|
|
|
|
{
|
2018-07-12 08:03:06 +00:00
|
|
|
if let TyKind::Path(ref path) = inner.node {
|
2018-10-12 06:09:04 +00:00
|
|
|
if let Def::Struct(def_id) = cx.tables.qpath_def(path, inner.hir_id) {
|
|
|
|
return match_def_path(cx.tcx, def_id, &paths::LINT);
|
|
|
|
}
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 06:09:04 +00:00
|
|
|
|
2016-09-01 07:07:37 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
struct LintCollector<'a, 'tcx: 'a> {
|
2018-09-11 23:34:52 +00:00
|
|
|
output: &'a mut FxHashSet<Name>,
|
2016-12-06 10:32:21 +00:00
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
impl<'a, 'tcx: 'a> Visitor<'tcx> for LintCollector<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr) {
|
2016-09-01 07:07:37 +00:00
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
2018-08-08 06:00:23 +00:00
|
|
|
fn visit_path(&mut self, path: &'tcx Path, _: HirId) {
|
2016-09-01 07:07:37 +00:00
|
|
|
if path.segments.len() == 1 {
|
2018-06-28 13:46:58 +00:00
|
|
|
self.output.insert(path.segments[0].ident.name);
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 10:32:21 +00:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-08 00:56:03 +00:00
|
|
|
NestedVisitorMap::All(&self.cx.tcx.hir())
|
2016-12-06 10:32:21 +00:00
|
|
|
}
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
2018-08-10 17:58:23 +00:00
|
|
|
|
|
|
|
pub struct DefaultHashTypes {
|
|
|
|
map: FxHashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DefaultHashTypes {
|
2018-08-14 08:25:09 +00:00
|
|
|
pub fn default() -> Self {
|
2018-08-10 17:58:23 +00:00
|
|
|
let mut map = FxHashMap::default();
|
2018-09-14 10:39:15 +00:00
|
|
|
map.insert("HashMap".to_string(), "FxHashMap".to_string());
|
|
|
|
map.insert("HashSet".to_string(), "FxHashSet".to_string());
|
2018-08-10 17:58:23 +00:00
|
|
|
Self { map }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for DefaultHashTypes {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(DEFAULT_HASH_TYPES)
|
|
|
|
}
|
2019-01-26 19:40:55 +00:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"DefaultHashType"
|
|
|
|
}
|
2018-08-10 17:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for DefaultHashTypes {
|
|
|
|
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
|
|
|
|
let ident_string = ident.to_string();
|
|
|
|
if let Some(replace) = self.map.get(&ident_string) {
|
2018-11-27 20:14:15 +00:00
|
|
|
let msg = format!(
|
|
|
|
"Prefer {} over {}, it has better performance \
|
|
|
|
and we don't need any collision prevention in clippy",
|
|
|
|
replace, ident_string
|
|
|
|
);
|
2018-09-14 10:39:15 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
DEFAULT_HASH_TYPES,
|
|
|
|
ident.span,
|
|
|
|
&msg,
|
|
|
|
"use",
|
|
|
|
replace.to_string(),
|
2018-11-27 14:13:57 +00:00
|
|
|
Applicability::MaybeIncorrect, // FxHashMap, ... needs another import
|
2018-09-14 10:39:15 +00:00
|
|
|
);
|
2018-08-10 17:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 10:38:09 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct CompilerLintFunctions {
|
|
|
|
map: FxHashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CompilerLintFunctions {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut map = FxHashMap::default();
|
|
|
|
map.insert("span_lint".to_string(), "utils::span_lint".to_string());
|
|
|
|
map.insert("struct_span_lint".to_string(), "utils::span_lint".to_string());
|
|
|
|
map.insert("lint".to_string(), "utils::span_lint".to_string());
|
|
|
|
map.insert("span_lint_note".to_string(), "utils::span_note_and_lint".to_string());
|
|
|
|
map.insert("span_lint_help".to_string(), "utils::span_help_and_lint".to_string());
|
|
|
|
Self { map }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for CompilerLintFunctions {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(COMPILER_LINT_FUNCTIONS)
|
|
|
|
}
|
2019-01-26 19:40:55 +00:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"CompileLintFunctions"
|
|
|
|
}
|
2018-09-14 10:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CompilerLintFunctions {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::MethodCall(ref path, _, ref args) = expr.node;
|
|
|
|
let fn_name = path.ident.as_str().to_string();
|
|
|
|
if let Some(sugg) = self.map.get(&fn_name);
|
|
|
|
let ty = walk_ptrs_ty(cx.tables.expr_ty(&args[0]));
|
|
|
|
if match_type(cx, ty, &paths::EARLY_CONTEXT)
|
|
|
|
|| match_type(cx, ty, &paths::LATE_CONTEXT);
|
|
|
|
then {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
COMPILER_LINT_FUNCTIONS,
|
|
|
|
path.ident.span,
|
|
|
|
"usage of a compiler lint function",
|
|
|
|
&format!("Please use the Clippy variant of this function: `{}`", sugg),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|