2020-10-28 22:36:07 +00:00
|
|
|
use crate::consts::{constant_simple, Constant};
|
2019-06-08 11:32:43 +00:00
|
|
|
use crate::utils::{
|
2020-09-24 12:49:22 +00:00
|
|
|
is_expn_of, match_def_path, match_qpath, match_type, method_calls, path_to_res, paths, qpath_res, run_lints,
|
|
|
|
snippet, span_lint, span_lint_and_help, span_lint_and_sugg, SpanlessEq,
|
2019-06-08 11:32:43 +00:00
|
|
|
};
|
2018-11-27 20:14:15 +00:00
|
|
|
use if_chain::if_chain;
|
2020-05-08 11:57:01 +00:00
|
|
|
use rustc_ast::ast::{Crate as AstCrate, ItemKind, LitKind, NodeId};
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::visit::FnKind;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2019-06-08 11:32:43 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2020-04-22 18:51:58 +00:00
|
|
|
use rustc_hir::hir_id::CRATE_HIR_ID;
|
|
|
|
use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
|
2020-09-24 12:49:22 +00:00
|
|
|
use rustc_hir::{Crate, Expr, ExprKind, HirId, Item, MutTy, Mutability, Node, Path, StmtKind, Ty, TyKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::hir::map::Map;
|
2020-12-20 16:19:49 +00:00
|
|
|
use rustc_middle::mir::interpret::ConstValue;
|
2020-10-28 22:36:07 +00:00
|
|
|
use rustc_middle::ty;
|
2020-02-18 13:28:18 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
|
2020-01-11 20:08:27 +00:00
|
|
|
use rustc_span::source_map::{Span, Spanned};
|
2020-05-08 11:57:01 +00:00
|
|
|
use rustc_span::symbol::{Symbol, SymbolStr};
|
2020-10-28 22:36:07 +00:00
|
|
|
use rustc_typeck::hir_ty_to_ty;
|
2016-07-18 09:19:33 +00:00
|
|
|
|
2020-04-17 12:43:37 +00:00
|
|
|
use std::borrow::{Borrow, Cow};
|
|
|
|
|
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.
|
|
|
|
///
|
2019-04-08 20:43:55 +00:00
|
|
|
/// **Known problems:** Only checks for lints associated using the
|
|
|
|
/// `declare_lint_pass!`, `impl_lint_pass!`, and `lint_array!` macros.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ```rust,ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// declare_lint! { pub LINT_1, ... }
|
|
|
|
/// declare_lint! { pub LINT_2, ... }
|
|
|
|
/// declare_lint! { pub FORGOTTEN_LINT, ... }
|
|
|
|
/// // ...
|
2019-04-08 20:43:55 +00:00
|
|
|
/// declare_lint_pass!(Pass => [LINT_1, LINT_2]);
|
|
|
|
/// // missing FORGOTTEN_LINT
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
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-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:
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ```rust,ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// cx.span_lint(LINT_NAME, "message");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Good:
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ```rust,ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// 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
|
|
|
|
2019-06-08 11:32:43 +00:00
|
|
|
declare_clippy_lint! {
|
2019-08-16 16:29:30 +00:00
|
|
|
/// **What it does:** Checks for calls to `cx.outer().expn_data()` and suggests to use
|
|
|
|
/// the `cx.outer_expn_data()`
|
2019-06-08 11:32:43 +00:00
|
|
|
///
|
2019-08-16 16:29:30 +00:00
|
|
|
/// **Why is this bad?** `cx.outer_expn_data()` is faster and more concise.
|
2019-06-08 11:32:43 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// Bad:
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ```rust,ignore
|
2019-08-16 16:29:30 +00:00
|
|
|
/// expr.span.ctxt().outer().expn_data()
|
2019-06-08 11:32:43 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Good:
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ```rust,ignore
|
2019-08-16 16:29:30 +00:00
|
|
|
/// expr.span.ctxt().outer_expn_data()
|
2019-06-08 11:32:43 +00:00
|
|
|
/// ```
|
2019-08-16 16:29:30 +00:00
|
|
|
pub OUTER_EXPN_EXPN_DATA,
|
2019-06-08 11:32:43 +00:00
|
|
|
internal,
|
2019-08-16 16:29:30 +00:00
|
|
|
"using `cx.outer_expn().expn_data()` instead of `cx.outer_expn_data()`"
|
2019-06-08 11:32:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 05:09:12 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Not an actual lint. This lint is only meant for testing our customized internal compiler
|
|
|
|
/// error message by calling `panic`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** ICE in large quantities can damage your teeth
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// Bad:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// 🍦🍦🍦🍦🍦
|
|
|
|
/// ```
|
|
|
|
pub PRODUCE_ICE,
|
|
|
|
internal,
|
|
|
|
"this message should not appear anywhere as we ICE before and don't emit the lint"
|
|
|
|
}
|
|
|
|
|
2020-01-11 20:08:27 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for cases of an auto-generated lint without an updated description,
|
|
|
|
/// i.e. `default lint description`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Indicates that the lint is not finished.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// Bad:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// declare_lint! { pub COOL_LINT, nursery, "default lint description" }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Good:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// declare_lint! { pub COOL_LINT, nursery, "a great new lint" }
|
|
|
|
/// ```
|
|
|
|
pub DEFAULT_LINT,
|
|
|
|
internal,
|
|
|
|
"found 'default lint description' in a lint declaration"
|
|
|
|
}
|
|
|
|
|
2020-04-17 12:43:37 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Lints `span_lint_and_then` function calls, where the
|
|
|
|
/// closure argument has only one statement and that statement is a method
|
|
|
|
/// call to `span_suggestion`, `span_help`, `span_note` (using the same
|
|
|
|
/// span), `help` or `note`.
|
|
|
|
///
|
|
|
|
/// These usages of `span_lint_and_then` should be replaced with one of the
|
|
|
|
/// wrapper functions `span_lint_and_sugg`, span_lint_and_help`, or
|
|
|
|
/// `span_lint_and_note`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Using the wrapper `span_lint_and_*` functions, is more
|
|
|
|
/// convenient, readable and less error prone.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// *Example:**
|
|
|
|
/// Bad:
|
|
|
|
/// ```rust,ignore
|
2020-04-19 18:38:07 +00:00
|
|
|
/// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
|
|
|
|
/// diag.span_suggestion(
|
2020-04-17 12:43:37 +00:00
|
|
|
/// expr.span,
|
|
|
|
/// help_msg,
|
|
|
|
/// sugg.to_string(),
|
|
|
|
/// Applicability::MachineApplicable,
|
|
|
|
/// );
|
|
|
|
/// });
|
2020-04-19 18:38:07 +00:00
|
|
|
/// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
|
|
|
|
/// diag.span_help(expr.span, help_msg);
|
2020-04-17 12:43:37 +00:00
|
|
|
/// });
|
2020-04-19 18:38:07 +00:00
|
|
|
/// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
|
|
|
|
/// diag.help(help_msg);
|
2020-04-17 12:43:37 +00:00
|
|
|
/// });
|
2020-04-19 18:38:07 +00:00
|
|
|
/// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
|
|
|
|
/// diag.span_note(expr.span, note_msg);
|
2020-04-17 12:43:37 +00:00
|
|
|
/// });
|
2020-04-19 18:38:07 +00:00
|
|
|
/// span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |diag| {
|
|
|
|
/// diag.note(note_msg);
|
2020-04-17 12:43:37 +00:00
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Good:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// span_lint_and_sugg(
|
|
|
|
/// cx,
|
|
|
|
/// TEST_LINT,
|
|
|
|
/// expr.span,
|
|
|
|
/// lint_msg,
|
|
|
|
/// help_msg,
|
|
|
|
/// sugg.to_string(),
|
|
|
|
/// Applicability::MachineApplicable,
|
|
|
|
/// );
|
2020-04-18 10:28:29 +00:00
|
|
|
/// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg);
|
|
|
|
/// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg);
|
2020-04-18 10:29:36 +00:00
|
|
|
/// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), note_msg);
|
|
|
|
/// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, None, note_msg);
|
2020-04-17 12:43:37 +00:00
|
|
|
/// ```
|
|
|
|
pub COLLAPSIBLE_SPAN_LINT_CALLS,
|
|
|
|
internal,
|
|
|
|
"found collapsible `span_lint_and_then` calls"
|
|
|
|
}
|
|
|
|
|
2020-09-24 12:49:22 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for calls to `utils::match_type()` on a type diagnostic item
|
|
|
|
/// and suggests to use `utils::is_type_diagnostic_item()` instead.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** `utils::is_type_diagnostic_item()` does not require hardcoded paths.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// Bad:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// utils::match_type(cx, ty, &paths::VEC)
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Good:
|
|
|
|
/// ```rust,ignore
|
2020-11-05 13:29:48 +00:00
|
|
|
/// utils::is_type_diagnostic_item(cx, ty, sym::vec_type)
|
2020-09-24 12:49:22 +00:00
|
|
|
/// ```
|
|
|
|
pub MATCH_TYPE_ON_DIAGNOSTIC_ITEM,
|
|
|
|
internal,
|
|
|
|
"using `utils::match_type()` instead of `utils::is_type_diagnostic_item()`"
|
|
|
|
}
|
|
|
|
|
2020-10-28 22:36:07 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:**
|
|
|
|
/// Checks the paths module for invalid paths.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?**
|
|
|
|
/// It indicates a bug in the code.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:** None.
|
|
|
|
pub INVALID_PATHS,
|
|
|
|
internal,
|
|
|
|
"invalid path"
|
|
|
|
}
|
|
|
|
|
2020-12-20 16:19:49 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:**
|
|
|
|
/// Checks for interning symbols that have already been pre-interned and defined as constants.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?**
|
|
|
|
/// It's faster and easier to use the symbol constant.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// Bad:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let _ = sym!(f32);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Good:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let _ = sym::f32;
|
|
|
|
/// ```
|
|
|
|
pub INTERNING_DEFINED_SYMBOL,
|
|
|
|
internal,
|
|
|
|
"interning a symbol that is pre-interned and defined as a constant"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]);
|
2016-07-18 09:19:33 +00:00
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl EarlyLintPass for ClippyLintsInternal {
|
2019-05-17 23:34:52 +00:00
|
|
|
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &AstCrate) {
|
|
|
|
if let Some(utils) = krate
|
|
|
|
.module
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.find(|item| item.ident.name.as_str() == "utils")
|
|
|
|
{
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ItemKind::Mod(ref utils_mod) = utils.kind {
|
2019-05-17 23:34:52 +00:00
|
|
|
if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name.as_str() == "paths") {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ItemKind::Mod(ref paths_mod) = paths.kind {
|
2019-11-06 09:50:24 +00:00
|
|
|
let mut last_name: Option<SymbolStr> = None;
|
2019-05-13 23:34:08 +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 \
|
2020-04-19 18:40:25 +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 {
|
2020-05-08 11:57:01 +00:00
|
|
|
declared_lints: FxHashMap<Symbol, Span>,
|
|
|
|
registered_lints: FxHashSet<Symbol>,
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 20:08:27 +00:00
|
|
|
impl_lint_pass!(LintWithoutLintPass => [DEFAULT_LINT, LINT_WITHOUT_LINT_PASS]);
|
2016-09-01 07:07:37 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for LintWithoutLintPass {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2020-04-22 18:51:58 +00:00
|
|
|
if !run_lints(cx, &[DEFAULT_LINT], item.hir_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-11 20:08:27 +00:00
|
|
|
if let hir::ItemKind::Static(ref ty, Mutability::Not, body_id) = item.kind {
|
2018-10-12 06:09:04 +00:00
|
|
|
if is_lint_ref_type(cx, ty) {
|
2020-01-11 20:08:27 +00:00
|
|
|
let expr = &cx.tcx.hir().body(body_id).value;
|
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::AddrOf(_, _, ref inner_exp) = expr.kind;
|
|
|
|
if let ExprKind::Struct(_, ref fields, _) = inner_exp.kind;
|
2020-04-19 18:38:07 +00:00
|
|
|
let field = fields
|
|
|
|
.iter()
|
|
|
|
.find(|f| f.ident.as_str() == "desc")
|
|
|
|
.expect("lints must have a description field");
|
2020-01-11 20:08:27 +00:00
|
|
|
if let ExprKind::Lit(Spanned {
|
|
|
|
node: LitKind::Str(ref sym, _),
|
|
|
|
..
|
|
|
|
}) = field.expr.kind;
|
|
|
|
if sym.as_str() == "default lint description";
|
|
|
|
|
|
|
|
then {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
DEFAULT_LINT,
|
|
|
|
item.span,
|
|
|
|
&format!("the lint `{}` has the default lint description", item.ident.name),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2019-10-24 11:54:18 +00:00
|
|
|
} else if is_expn_of(item.span, "impl_lint_pass").is_some()
|
|
|
|
|| is_expn_of(item.span, "declare_lint_pass").is_some()
|
|
|
|
{
|
2020-11-22 22:46:21 +00:00
|
|
|
if let hir::ItemKind::Impl(hir::Impl {
|
2020-01-18 05:14:36 +00:00
|
|
|
of_trait: None,
|
|
|
|
items: ref impl_item_refs,
|
|
|
|
..
|
2020-11-22 22:46:21 +00:00
|
|
|
}) = item.kind
|
2020-01-18 05:14:36 +00:00
|
|
|
{
|
2019-10-24 11:54:18 +00:00
|
|
|
let mut collector = LintCollector {
|
|
|
|
output: &mut self.registered_lints,
|
|
|
|
cx,
|
|
|
|
};
|
|
|
|
let body_id = cx.tcx.hir().body_owned_by(
|
|
|
|
impl_item_refs
|
|
|
|
.iter()
|
|
|
|
.find(|iiref| iiref.ident.as_str() == "get_lints")
|
|
|
|
.expect("LintPass needs to implement get_lints")
|
|
|
|
.id
|
|
|
|
.hir_id,
|
|
|
|
);
|
|
|
|
collector.visit_expr(&cx.tcx.hir().body(body_id).value);
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_crate_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Crate<'_>) {
|
2020-04-22 18:51:58 +00:00
|
|
|
if !run_lints(cx, &[LINT_WITHOUT_LINT_PASS], CRATE_HIR_ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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!`:
|
2019-08-16 17:05:44 +00:00
|
|
|
let lint_span = lint_span.ctxt().outer_expn_data().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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +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,
|
2019-12-21 18:38:45 +00:00
|
|
|
mutbl: Mutability::Not,
|
2017-09-05 09:33:04 +00:00
|
|
|
},
|
2019-09-27 15:16:06 +00:00
|
|
|
) = ty.kind
|
2018-11-27 20:14:15 +00:00
|
|
|
{
|
2019-09-27 15:16:06 +00:00
|
|
|
if let TyKind::Path(ref path) = inner.kind {
|
2020-06-26 02:55:23 +00:00
|
|
|
if let Res::Def(DefKind::Struct, def_id) = cx.qpath_res(path, inner.hir_id) {
|
2019-05-17 21:53:54 +00:00
|
|
|
return match_def_path(cx, def_id, &paths::LINT);
|
2018-10-12 06:09:04 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
struct LintCollector<'a, 'tcx> {
|
2020-05-08 11:57:01 +00:00
|
|
|
output: &'a mut FxHashSet<Symbol>,
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for LintCollector<'a, 'tcx> {
|
2020-01-09 07:13:22 +00:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2019-12-30 04:02:10 +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
|
|
|
}
|
|
|
|
}
|
2020-04-22 18:51:58 +00:00
|
|
|
|
2020-03-15 22:41:20 +00:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
|
|
|
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
|
|
|
|
2018-09-14 10:38:09 +00:00
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct CompilerLintFunctions {
|
2019-06-06 05:12:12 +00:00
|
|
|
map: FxHashMap<&'static str, &'static str>,
|
2018-09-14 10:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CompilerLintFunctions {
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-09-14 10:38:09 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut map = FxHashMap::default();
|
2019-06-06 05:12:12 +00:00
|
|
|
map.insert("span_lint", "utils::span_lint");
|
|
|
|
map.insert("struct_span_lint", "utils::span_lint");
|
|
|
|
map.insert("lint", "utils::span_lint");
|
2020-01-27 02:26:42 +00:00
|
|
|
map.insert("span_lint_note", "utils::span_lint_and_note");
|
2020-01-27 01:56:22 +00:00
|
|
|
map.insert("span_lint_help", "utils::span_lint_and_help");
|
2018-09-14 10:38:09 +00:00
|
|
|
Self { map }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl_lint_pass!(CompilerLintFunctions => [COMPILER_LINT_FUNCTIONS]);
|
2018-09-14 10:38:09 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for CompilerLintFunctions {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2020-04-22 18:51:58 +00:00
|
|
|
if !run_lints(cx, &[COMPILER_LINT_FUNCTIONS], expr.hir_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-14 10:38:09 +00:00
|
|
|
if_chain! {
|
2020-06-09 21:44:04 +00:00
|
|
|
if let ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind;
|
2019-06-06 05:12:12 +00:00
|
|
|
let fn_name = path.ident;
|
|
|
|
if let Some(sugg) = self.map.get(&*fn_name.as_str());
|
2020-09-24 12:49:22 +00:00
|
|
|
let ty = cx.typeck_results().expr_ty(&args[0]).peel_refs();
|
2019-05-17 21:53:54 +00:00
|
|
|
if match_type(cx, ty, &paths::EARLY_CONTEXT)
|
2020-04-19 18:38:07 +00:00
|
|
|
|| match_type(cx, ty, &paths::LATE_CONTEXT);
|
2018-09-14 10:38:09 +00:00
|
|
|
then {
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2018-09-14 10:38:09 +00:00
|
|
|
cx,
|
|
|
|
COMPILER_LINT_FUNCTIONS,
|
|
|
|
path.ident.span,
|
|
|
|
"usage of a compiler lint function",
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2019-04-15 11:04:32 +00:00
|
|
|
&format!("please use the Clippy variant of this function: `{}`", sugg),
|
2018-09-14 10:38:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-08 11:32:43 +00:00
|
|
|
|
2019-10-24 11:29:51 +00:00
|
|
|
declare_lint_pass!(OuterExpnDataPass => [OUTER_EXPN_EXPN_DATA]);
|
2019-06-08 11:32:43 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for OuterExpnDataPass {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
2020-04-22 18:51:58 +00:00
|
|
|
if !run_lints(cx, &[OUTER_EXPN_EXPN_DATA], expr.hir_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-28 08:41:32 +00:00
|
|
|
let (method_names, arg_lists, spans) = method_calls(expr, 2);
|
2019-11-06 09:50:24 +00:00
|
|
|
let method_names: Vec<SymbolStr> = method_names.iter().map(|s| s.as_str()).collect();
|
|
|
|
let method_names: Vec<&str> = method_names.iter().map(|s| &**s).collect();
|
2019-06-08 11:32:43 +00:00
|
|
|
if_chain! {
|
2019-08-16 16:29:30 +00:00
|
|
|
if let ["expn_data", "outer_expn"] = method_names.as_slice();
|
2019-06-08 11:32:43 +00:00
|
|
|
let args = arg_lists[1];
|
|
|
|
if args.len() == 1;
|
|
|
|
let self_arg = &args[0];
|
2020-09-24 12:49:22 +00:00
|
|
|
let self_ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
|
2019-06-08 11:32:43 +00:00
|
|
|
if match_type(cx, self_ty, &paths::SYNTAX_CONTEXT);
|
|
|
|
then {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2019-08-16 16:29:30 +00:00
|
|
|
OUTER_EXPN_EXPN_DATA,
|
2019-08-28 08:41:32 +00:00
|
|
|
spans[1].with_hi(expr.span.hi()),
|
2019-08-16 16:29:30 +00:00
|
|
|
"usage of `outer_expn().expn_data()`",
|
2019-06-08 11:32:43 +00:00
|
|
|
"try",
|
2019-08-28 08:41:32 +00:00
|
|
|
"outer_expn_data()".to_string(),
|
2019-06-08 11:32:43 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 05:09:12 +00:00
|
|
|
|
|
|
|
declare_lint_pass!(ProduceIce => [PRODUCE_ICE]);
|
|
|
|
|
|
|
|
impl EarlyLintPass for ProduceIce {
|
2020-02-18 13:28:18 +00:00
|
|
|
fn check_fn(&mut self, _: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: NodeId) {
|
2019-09-27 05:09:12 +00:00
|
|
|
if is_trigger_fn(fn_kind) {
|
2020-02-06 19:29:58 +00:00
|
|
|
panic!("Would you like some help with that?");
|
2019-09-27 05:09:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_trigger_fn(fn_kind: FnKind<'_>) -> bool {
|
|
|
|
match fn_kind {
|
2020-02-06 19:29:58 +00:00
|
|
|
FnKind::Fn(_, ident, ..) => ident.name.as_str() == "it_looks_like_you_are_trying_to_kill_clippy",
|
2019-09-27 05:09:12 +00:00
|
|
|
FnKind::Closure(..) => false,
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 12:43:37 +00:00
|
|
|
|
|
|
|
declare_lint_pass!(CollapsibleCalls => [COLLAPSIBLE_SPAN_LINT_CALLS]);
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for CollapsibleCalls {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
2020-04-22 18:51:58 +00:00
|
|
|
if !run_lints(cx, &[COLLAPSIBLE_SPAN_LINT_CALLS], expr.hir_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-17 12:43:37 +00:00
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Call(ref func, ref and_then_args) = expr.kind;
|
|
|
|
if let ExprKind::Path(ref path) = func.kind;
|
|
|
|
if match_qpath(path, &["span_lint_and_then"]);
|
|
|
|
if and_then_args.len() == 5;
|
|
|
|
if let ExprKind::Closure(_, _, body_id, _, _) = &and_then_args[4].kind;
|
|
|
|
let body = cx.tcx.hir().body(*body_id);
|
|
|
|
if let ExprKind::Block(block, _) = &body.value.kind;
|
|
|
|
let stmts = &block.stmts;
|
|
|
|
if stmts.len() == 1 && block.expr.is_none();
|
|
|
|
if let StmtKind::Semi(only_expr) = &stmts[0].kind;
|
2020-06-09 21:44:04 +00:00
|
|
|
if let ExprKind::MethodCall(ref ps, _, ref span_call_args, _) = &only_expr.kind;
|
2020-04-17 12:43:37 +00:00
|
|
|
let and_then_snippets = get_and_then_snippets(cx, and_then_args);
|
2020-08-28 14:10:16 +00:00
|
|
|
let mut sle = SpanlessEq::new(cx).deny_side_effects();
|
2020-04-17 12:43:37 +00:00
|
|
|
then {
|
|
|
|
match &*ps.ident.as_str() {
|
|
|
|
"span_suggestion" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => {
|
|
|
|
suggest_suggestion(cx, expr, &and_then_snippets, &span_suggestion_snippets(cx, span_call_args));
|
|
|
|
},
|
|
|
|
"span_help" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => {
|
|
|
|
let help_snippet = snippet(cx, span_call_args[2].span, r#""...""#);
|
2020-04-18 10:28:29 +00:00
|
|
|
suggest_help(cx, expr, &and_then_snippets, help_snippet.borrow(), true);
|
2020-04-17 12:43:37 +00:00
|
|
|
},
|
|
|
|
"span_note" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => {
|
|
|
|
let note_snippet = snippet(cx, span_call_args[2].span, r#""...""#);
|
2020-04-18 10:29:36 +00:00
|
|
|
suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow(), true);
|
2020-04-17 12:43:37 +00:00
|
|
|
},
|
|
|
|
"help" => {
|
|
|
|
let help_snippet = snippet(cx, span_call_args[1].span, r#""...""#);
|
2020-04-18 10:28:29 +00:00
|
|
|
suggest_help(cx, expr, &and_then_snippets, help_snippet.borrow(), false);
|
2020-04-17 12:43:37 +00:00
|
|
|
}
|
|
|
|
"note" => {
|
|
|
|
let note_snippet = snippet(cx, span_call_args[1].span, r#""...""#);
|
2020-04-18 10:29:36 +00:00
|
|
|
suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow(), false);
|
2020-04-17 12:43:37 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AndThenSnippets<'a> {
|
|
|
|
cx: Cow<'a, str>,
|
|
|
|
lint: Cow<'a, str>,
|
|
|
|
span: Cow<'a, str>,
|
|
|
|
msg: Cow<'a, str>,
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn get_and_then_snippets<'a, 'hir>(cx: &LateContext<'_>, and_then_snippets: &'hir [Expr<'hir>]) -> AndThenSnippets<'a> {
|
2020-04-17 12:43:37 +00:00
|
|
|
let cx_snippet = snippet(cx, and_then_snippets[0].span, "cx");
|
|
|
|
let lint_snippet = snippet(cx, and_then_snippets[1].span, "..");
|
|
|
|
let span_snippet = snippet(cx, and_then_snippets[2].span, "span");
|
|
|
|
let msg_snippet = snippet(cx, and_then_snippets[3].span, r#""...""#);
|
|
|
|
|
|
|
|
AndThenSnippets {
|
|
|
|
cx: cx_snippet,
|
|
|
|
lint: lint_snippet,
|
|
|
|
span: span_snippet,
|
|
|
|
msg: msg_snippet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SpanSuggestionSnippets<'a> {
|
|
|
|
help: Cow<'a, str>,
|
|
|
|
sugg: Cow<'a, str>,
|
|
|
|
applicability: Cow<'a, str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn span_suggestion_snippets<'a, 'hir>(
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'_>,
|
2020-04-17 12:43:37 +00:00
|
|
|
span_call_args: &'hir [Expr<'hir>],
|
|
|
|
) -> SpanSuggestionSnippets<'a> {
|
|
|
|
let help_snippet = snippet(cx, span_call_args[2].span, r#""...""#);
|
|
|
|
let sugg_snippet = snippet(cx, span_call_args[3].span, "..");
|
|
|
|
let applicability_snippet = snippet(cx, span_call_args[4].span, "Applicability::MachineApplicable");
|
|
|
|
|
|
|
|
SpanSuggestionSnippets {
|
|
|
|
help: help_snippet,
|
|
|
|
sugg: sugg_snippet,
|
|
|
|
applicability: applicability_snippet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn suggest_suggestion(
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'_>,
|
2020-04-17 12:43:37 +00:00
|
|
|
expr: &Expr<'_>,
|
|
|
|
and_then_snippets: &AndThenSnippets<'_>,
|
|
|
|
span_suggestion_snippets: &SpanSuggestionSnippets<'_>,
|
|
|
|
) {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
COLLAPSIBLE_SPAN_LINT_CALLS,
|
|
|
|
expr.span,
|
|
|
|
"this call is collapsible",
|
|
|
|
"collapse into",
|
|
|
|
format!(
|
|
|
|
"span_lint_and_sugg({}, {}, {}, {}, {}, {}, {})",
|
|
|
|
and_then_snippets.cx,
|
|
|
|
and_then_snippets.lint,
|
|
|
|
and_then_snippets.span,
|
|
|
|
and_then_snippets.msg,
|
|
|
|
span_suggestion_snippets.help,
|
|
|
|
span_suggestion_snippets.sugg,
|
|
|
|
span_suggestion_snippets.applicability
|
|
|
|
),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-18 10:28:29 +00:00
|
|
|
fn suggest_help(
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'_>,
|
2020-04-18 10:28:29 +00:00
|
|
|
expr: &Expr<'_>,
|
|
|
|
and_then_snippets: &AndThenSnippets<'_>,
|
|
|
|
help: &str,
|
|
|
|
with_span: bool,
|
|
|
|
) {
|
|
|
|
let option_span = if with_span {
|
|
|
|
format!("Some({})", and_then_snippets.span)
|
|
|
|
} else {
|
|
|
|
"None".to_string()
|
|
|
|
};
|
|
|
|
|
2020-04-17 12:43:37 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
COLLAPSIBLE_SPAN_LINT_CALLS,
|
|
|
|
expr.span,
|
|
|
|
"this call is collapsible",
|
|
|
|
"collapse into",
|
|
|
|
format!(
|
2020-04-18 10:28:29 +00:00
|
|
|
"span_lint_and_help({}, {}, {}, {}, {}, {})",
|
|
|
|
and_then_snippets.cx,
|
|
|
|
and_then_snippets.lint,
|
|
|
|
and_then_snippets.span,
|
|
|
|
and_then_snippets.msg,
|
|
|
|
&option_span,
|
|
|
|
help
|
2020-04-17 12:43:37 +00:00
|
|
|
),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-18 10:29:36 +00:00
|
|
|
fn suggest_note(
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'_>,
|
2020-04-18 10:29:36 +00:00
|
|
|
expr: &Expr<'_>,
|
|
|
|
and_then_snippets: &AndThenSnippets<'_>,
|
|
|
|
note: &str,
|
|
|
|
with_span: bool,
|
|
|
|
) {
|
|
|
|
let note_span = if with_span {
|
|
|
|
format!("Some({})", and_then_snippets.span)
|
|
|
|
} else {
|
|
|
|
"None".to_string()
|
|
|
|
};
|
|
|
|
|
2020-04-17 12:43:37 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
COLLAPSIBLE_SPAN_LINT_CALLS,
|
|
|
|
expr.span,
|
|
|
|
"this call is collspible",
|
|
|
|
"collapse into",
|
|
|
|
format!(
|
|
|
|
"span_lint_and_note({}, {}, {}, {}, {}, {})",
|
|
|
|
and_then_snippets.cx,
|
|
|
|
and_then_snippets.lint,
|
|
|
|
and_then_snippets.span,
|
|
|
|
and_then_snippets.msg,
|
2020-04-18 10:29:36 +00:00
|
|
|
note_span,
|
2020-04-17 12:43:37 +00:00
|
|
|
note
|
|
|
|
),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2020-09-24 12:49:22 +00:00
|
|
|
|
|
|
|
declare_lint_pass!(MatchTypeOnDiagItem => [MATCH_TYPE_ON_DIAGNOSTIC_ITEM]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for MatchTypeOnDiagItem {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
|
|
|
if !run_lints(cx, &[MATCH_TYPE_ON_DIAGNOSTIC_ITEM], expr.hir_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
|
|
|
// Check if this is a call to utils::match_type()
|
|
|
|
if let ExprKind::Call(fn_path, [context, ty, ty_path]) = expr.kind;
|
|
|
|
if let ExprKind::Path(fn_qpath) = &fn_path.kind;
|
|
|
|
if match_qpath(&fn_qpath, &["utils", "match_type"]);
|
|
|
|
// Extract the path to the matched type
|
|
|
|
if let Some(segments) = path_to_matched_type(cx, ty_path);
|
|
|
|
let segments: Vec<&str> = segments.iter().map(|sym| &**sym).collect();
|
|
|
|
if let Some(ty_did) = path_to_res(cx, &segments[..]).and_then(|res| res.opt_def_id());
|
|
|
|
// Check if the matched type is a diagnostic item
|
|
|
|
let diag_items = cx.tcx.diagnostic_items(ty_did.krate);
|
|
|
|
if let Some(item_name) = diag_items.iter().find_map(|(k, v)| if *v == ty_did { Some(k) } else { None });
|
|
|
|
then {
|
|
|
|
let cx_snippet = snippet(cx, context.span, "_");
|
|
|
|
let ty_snippet = snippet(cx, ty.span, "_");
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MATCH_TYPE_ON_DIAGNOSTIC_ITEM,
|
|
|
|
expr.span,
|
|
|
|
"usage of `utils::match_type()` on a type diagnostic item",
|
|
|
|
"try",
|
2020-11-05 13:29:48 +00:00
|
|
|
format!("utils::is_type_diagnostic_item({}, {}, sym::{})", cx_snippet, ty_snippet, item_name),
|
2020-09-24 12:49:22 +00:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_to_matched_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<Vec<SymbolStr>> {
|
|
|
|
use rustc_hir::ItemKind;
|
|
|
|
|
|
|
|
match &expr.kind {
|
|
|
|
ExprKind::AddrOf(.., expr) => return path_to_matched_type(cx, expr),
|
|
|
|
ExprKind::Path(qpath) => match qpath_res(cx, qpath, expr.hir_id) {
|
|
|
|
Res::Local(hir_id) => {
|
|
|
|
let parent_id = cx.tcx.hir().get_parent_node(hir_id);
|
|
|
|
if let Some(Node::Local(local)) = cx.tcx.hir().find(parent_id) {
|
|
|
|
if let Some(init) = local.init {
|
|
|
|
return path_to_matched_type(cx, init);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Res::Def(DefKind::Const | DefKind::Static, def_id) => {
|
|
|
|
if let Some(Node::Item(item)) = cx.tcx.hir().get_if_local(def_id) {
|
|
|
|
if let ItemKind::Const(.., body_id) | ItemKind::Static(.., body_id) = item.kind {
|
|
|
|
let body = cx.tcx.hir().body(body_id);
|
|
|
|
return path_to_matched_type(cx, &body.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
},
|
|
|
|
ExprKind::Array(exprs) => {
|
|
|
|
let segments: Vec<SymbolStr> = exprs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|expr| {
|
|
|
|
if let ExprKind::Lit(lit) = &expr.kind {
|
|
|
|
if let LitKind::Str(sym, _) = lit.node {
|
|
|
|
return Some(sym.as_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if segments.len() == exprs.len() {
|
|
|
|
return Some(segments);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2020-10-28 22:36:07 +00:00
|
|
|
|
|
|
|
// This is not a complete resolver for paths. It works on all the paths currently used in the paths
|
|
|
|
// module. That's all it does and all it needs to do.
|
|
|
|
pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool {
|
|
|
|
if path_to_res(cx, path).is_some() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some implementations can't be found by `path_to_res`, particularly inherent
|
|
|
|
// implementations of native types. Check lang items.
|
|
|
|
let path_syms: Vec<_> = path.iter().map(|p| Symbol::intern(p)).collect();
|
|
|
|
let lang_items = cx.tcx.lang_items();
|
|
|
|
for lang_item in lang_items.items() {
|
|
|
|
if let Some(def_id) = lang_item {
|
|
|
|
let lang_item_path = cx.get_def_path(*def_id);
|
|
|
|
if path_syms.starts_with(&lang_item_path) {
|
|
|
|
if let [item] = &path_syms[lang_item_path.len()..] {
|
|
|
|
for child in cx.tcx.item_children(*def_id) {
|
|
|
|
if child.ident.name == *item {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(InvalidPaths => [INVALID_PATHS]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for InvalidPaths {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
|
|
|
let local_def_id = &cx.tcx.parent_module(item.hir_id);
|
|
|
|
let mod_name = &cx.tcx.item_name(local_def_id.to_def_id());
|
|
|
|
if_chain! {
|
|
|
|
if mod_name.as_str() == "paths";
|
|
|
|
if let hir::ItemKind::Const(ty, body_id) = item.kind;
|
|
|
|
let ty = hir_ty_to_ty(cx.tcx, ty);
|
|
|
|
if let ty::Array(el_ty, _) = &ty.kind();
|
|
|
|
if let ty::Ref(_, el_ty, _) = &el_ty.kind();
|
|
|
|
if el_ty.is_str();
|
|
|
|
let body = cx.tcx.hir().body(body_id);
|
|
|
|
let typeck_results = cx.tcx.typeck_body(body_id);
|
|
|
|
if let Some(Constant::Vec(path)) = constant_simple(cx, typeck_results, &body.value);
|
|
|
|
let path: Vec<&str> = path.iter().map(|x| {
|
|
|
|
if let Constant::Str(s) = x {
|
|
|
|
s.as_str()
|
|
|
|
} else {
|
|
|
|
// We checked the type of the constant above
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
if !check_path(cx, &path[..]);
|
|
|
|
then {
|
|
|
|
span_lint(cx, CLIPPY_LINTS_INTERNAL, item.span, "invalid path");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 16:19:49 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct InterningDefinedSymbol {
|
|
|
|
// Maps the symbol value to the constant name.
|
|
|
|
symbol_map: FxHashMap<u32, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(InterningDefinedSymbol => [INTERNING_DEFINED_SYMBOL]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
|
|
|
|
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
|
|
|
if !self.symbol_map.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(Res::Def(_, def_id)) = path_to_res(cx, &paths::SYM_MODULE) {
|
|
|
|
for item in cx.tcx.item_children(def_id).iter() {
|
|
|
|
if_chain! {
|
|
|
|
if let Res::Def(DefKind::Const, item_def_id) = item.res;
|
|
|
|
let ty = cx.tcx.type_of(item_def_id);
|
|
|
|
if match_type(cx, ty, &paths::SYMBOL);
|
|
|
|
if let Ok(ConstValue::Scalar(value)) = cx.tcx.const_eval_poly(item_def_id);
|
|
|
|
if let Ok(value) = value.to_u32();
|
|
|
|
then {
|
|
|
|
self.symbol_map.insert(value, item.ident.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Call(func, [arg]) = &expr.kind;
|
|
|
|
if let ty::FnDef(def_id, _) = cx.typeck_results().expr_ty(func).kind();
|
|
|
|
if match_def_path(cx, *def_id, &paths::SYMBOL_INTERN);
|
|
|
|
if let Some(Constant::Str(arg)) = constant_simple(cx, cx.typeck_results(), arg);
|
|
|
|
let value = Symbol::intern(&arg).as_u32();
|
|
|
|
if let Some(symbol_const) = self.symbol_map.get(&value);
|
|
|
|
then {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
INTERNING_DEFINED_SYMBOL,
|
|
|
|
is_expn_of(expr.span, "sym").unwrap_or(expr.span),
|
|
|
|
"interning a defined symbol",
|
|
|
|
"try",
|
|
|
|
format!("rustc_span::symbol::sym::{}", symbol_const),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|