2019-06-08 11:32:43 +00:00
|
|
|
use crate::utils::{
|
2020-01-27 01:56:22 +00:00
|
|
|
is_expn_of, match_def_path, match_type, method_calls, paths, span_lint, span_lint_and_help, span_lint_and_sugg,
|
2019-10-24 11:54:18 +00:00
|
|
|
walk_ptrs_ty,
|
2019-06-08 11:32:43 +00:00
|
|
|
};
|
2018-11-27 20:14:15 +00:00
|
|
|
use if_chain::if_chain;
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc::hir::map::Map;
|
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-01-09 07:13:22 +00:00
|
|
|
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::*;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
|
2019-12-03 23:16:03 +00:00
|
|
|
use rustc_session::declare_tool_lint;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, impl_lint_pass};
|
2020-01-11 20:08:27 +00:00
|
|
|
use rustc_span::source_map::{Span, Spanned};
|
2019-12-31 00:17:56 +00:00
|
|
|
use rustc_span::symbol::SymbolStr;
|
2019-09-27 05:09:12 +00:00
|
|
|
use syntax::ast;
|
2020-01-11 20:08:27 +00:00
|
|
|
use syntax::ast::{Crate as AstCrate, ItemKind, LitKind, Name};
|
2019-09-27 05:09:12 +00:00
|
|
|
use syntax::visit::FnKind;
|
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.
|
|
|
|
///
|
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"
|
|
|
|
}
|
|
|
|
|
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 \
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
|
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;
|
|
|
|
let field = fields.iter()
|
|
|
|
.find(|f| f.ident.as_str() == "desc")
|
|
|
|
.expect("lints must have a description field");
|
|
|
|
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-01-18 05:14:36 +00:00
|
|
|
if let hir::ItemKind::Impl {
|
|
|
|
of_trait: None,
|
|
|
|
items: ref impl_item_refs,
|
|
|
|
..
|
|
|
|
} = item.kind
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +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!`:
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +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 {
|
2019-05-04 00:03:12 +00:00
|
|
|
if let Res::Def(DefKind::Struct, def_id) = cx.tables.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> {
|
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
|
|
|
}
|
|
|
|
|
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-27 07:12:26 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
2016-09-01 07:07:37 +00:00
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
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-01-09 07:13:22 +00:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
|
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
|
|
|
|
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
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CompilerLintFunctions {
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
|
2018-09-14 10:38:09 +00:00
|
|
|
if_chain! {
|
2019-09-27 15:16:06 +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());
|
2018-09-14 10:38:09 +00:00
|
|
|
let ty = walk_ptrs_ty(cx.tables.expr_ty(&args[0]));
|
2019-05-17 21:53:54 +00:00
|
|
|
if match_type(cx, ty, &paths::EARLY_CONTEXT)
|
|
|
|
|| 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",
|
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
|
|
|
|
2019-08-16 16:29:30 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnDataPass {
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
|
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];
|
|
|
|
let self_ty = walk_ptrs_ty(cx.tables.expr_ty(self_arg));
|
|
|
|
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-06 19:29:58 +00:00
|
|
|
fn check_fn(&mut self, _: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: ast::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,
|
|
|
|
}
|
|
|
|
}
|