2016-07-18 09:19:33 +00:00
|
|
|
use rustc::lint::*;
|
2016-09-01 07:07:37 +00:00
|
|
|
use rustc::hir::*;
|
2016-12-06 10:32:21 +00:00
|
|
|
use rustc::hir::intravisit::{Visitor, walk_expr, NestedVisitorMap};
|
2016-09-01 07:07:37 +00:00
|
|
|
use utils::{paths, match_path, span_lint};
|
2016-11-23 20:19:03 +00:00
|
|
|
use syntax::symbol::InternedString;
|
2016-09-01 07:07:37 +00:00
|
|
|
use syntax::ast::{Name, NodeId, ItemKind, Crate as AstCrate};
|
|
|
|
use syntax::codemap::Span;
|
|
|
|
use std::collections::{HashSet, HashMap};
|
|
|
|
|
2016-07-18 09:19:33 +00:00
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for various things we like to keep tidy in clippy.
|
2016-07-18 09:19:33 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** We like to pretend we're an example of tidy code.
|
2016-07-18 09:19:33 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Example:** Wrong ordering of the util::paths constants.
|
2016-07-18 09:19:33 +00:00
|
|
|
declare_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub CLIPPY_LINTS_INTERNAL,
|
|
|
|
Allow,
|
|
|
|
"various things that will negatively affect your clippy experience"
|
2016-07-18 09:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-01 07:07:37 +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
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
declare_lint! {
|
|
|
|
pub LINT_WITHOUT_LINT_PASS,
|
|
|
|
Warn,
|
|
|
|
"declaring a lint without associating it in a LintPass"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for Clippy {
|
2016-09-01 07:07:37 +00:00
|
|
|
fn check_crate(&mut self, cx: &EarlyContext, krate: &AstCrate) {
|
2016-11-23 20:19:03 +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 {
|
2016-11-23 20:19:03 +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 {
|
|
|
|
let mut last_name: Option<InternedString> = None;
|
|
|
|
for item in &paths_mod.items {
|
|
|
|
let name = item.ident.name.as_str();
|
|
|
|
if let Some(ref last_name) = last_name {
|
|
|
|
if **last_name > *name {
|
|
|
|
span_lint(cx,
|
|
|
|
CLIPPY_LINTS_INTERNAL,
|
|
|
|
item.span,
|
2016-12-20 17:21:30 +00:00
|
|
|
"this constant should be before the previous constant due to lexical \
|
|
|
|
ordering");
|
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 {
|
|
|
|
declared_lints: HashMap<Name, Span>,
|
|
|
|
registered_lints: HashSet<Name>,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl LintPass for LintWithoutLintPass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(LINT_WITHOUT_LINT_PASS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2017-01-04 22:18:11 +00:00
|
|
|
if let ItemStatic(ref ty, MutImmutable, body_id) = item.node {
|
2016-09-01 07:07:37 +00:00
|
|
|
if is_lint_ref_type(ty) {
|
|
|
|
self.declared_lints.insert(item.name, item.span);
|
2016-11-23 20:19:03 +00:00
|
|
|
} else if is_lint_array_type(ty) && item.vis == Visibility::Inherited && item.name == "ARRAY" {
|
2016-12-20 17:21:30 +00:00
|
|
|
let mut collector = LintCollector {
|
|
|
|
output: &mut self.registered_lints,
|
|
|
|
cx: cx,
|
|
|
|
};
|
2017-01-04 22:18:11 +00:00
|
|
|
collector.visit_expr(&cx.tcx.map.body(body_id).value);
|
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 {
|
|
|
|
// When using the `declare_lint!` macro, the original `lint_span`'s
|
|
|
|
// 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
|
|
|
|
// actual span that invoked `declare_lint!`:
|
|
|
|
let lint_span = cx.sess().codemap().source_callsite(lint_span);
|
|
|
|
|
|
|
|
if !self.registered_lints.contains(lint_name) {
|
|
|
|
span_lint(cx,
|
|
|
|
LINT_WITHOUT_LINT_PASS,
|
|
|
|
lint_span,
|
|
|
|
&format!("the lint `{}` is not added to any `LintPass`", lint_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn is_lint_ref_type(ty: &Ty) -> bool {
|
|
|
|
if let TyRptr(Some(_), MutTy { ty: ref inner, mutbl: MutImmutable }) = ty.node {
|
2016-12-01 21:31:56 +00:00
|
|
|
if let TyPath(ref path) = inner.node {
|
2016-09-01 07:07:37 +00:00
|
|
|
return match_path(path, &paths::LINT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn is_lint_array_type(ty: &Ty) -> bool {
|
2016-12-01 21:31:56 +00:00
|
|
|
if let TyPath(ref path) = ty.node {
|
2016-09-01 07:07:37 +00:00
|
|
|
match_path(path, &paths::LINT_ARRAY)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
struct LintCollector<'a, 'tcx: 'a> {
|
2016-09-01 07:07:37 +00:00
|
|
|
output: &'a mut HashSet<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);
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
fn visit_path(&mut self, path: &'tcx Path, _: NodeId) {
|
2016-09-01 07:07:37 +00:00
|
|
|
if path.segments.len() == 1 {
|
|
|
|
self.output.insert(path.segments[0].name);
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 10:32:21 +00:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::All(&self.cx.tcx.map)
|
|
|
|
}
|
2016-09-01 07:07:37 +00:00
|
|
|
}
|