Skip to content

Fix lint_without_lint_pass #3378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use toml;

// Currently, categories "style", "correctness", "complexity" and "perf" are enabled by default,
// as said in the README.md of this repository. If this changes, please update README.md.
#[macro_export]
macro_rules! declare_clippy_lint {
{ pub $name:tt, style, $description:tt } => {
declare_tool_lint! { pub clippy::$name, Warn, $description, report_in_external_macro: true }
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/literal_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ impl LintPass for LiteralDigitGrouping {
lint_array!(
UNREADABLE_LITERAL,
INCONSISTENT_DIGIT_GROUPING,
LARGE_DIGIT_GROUPS
LARGE_DIGIT_GROUPS,
MISTYPED_LITERAL_SUFFIXES,
)
}
}
Expand Down
35 changes: 18 additions & 17 deletions clippy_lints/src/utils/internal_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@


use crate::utils::{
match_qpath, match_type, paths, span_help_and_lint, span_lint, span_lint_and_sugg, walk_ptrs_ty,
match_def_path, match_type, paths, span_help_and_lint, span_lint, span_lint_and_sugg, walk_ptrs_ty,
};
use if_chain::if_chain;
use crate::rustc::hir;
use crate::rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use crate::rustc::hir::*;
use crate::rustc::hir::def::Def;
use crate::rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc_data_structures::fx::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -160,15 +161,21 @@ impl LintPass for LintWithoutLintPass {

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
if let hir::ItemKind::Static(ref ty, MutImmutable, body_id) = item.node {
if is_lint_ref_type(ty) {
if let hir::ItemKind::Static(ref ty, MutImmutable, _) = item.node {
if is_lint_ref_type(cx, ty) {
self.declared_lints.insert(item.name, item.span);
} else if is_lint_array_type(ty) && item.name == "ARRAY" {
if let VisibilityKind::Inherited = item.vis.node {
}
} 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 {
let mut collector = LintCollector {
output: &mut self.registered_lints,
cx,
};
let body_id = cx.tcx.hir.body_owned_by(impl_item_refs[0].id.node_id);
collector.visit_expr(&cx.tcx.hir.body(body_id).value);
}
}
Expand Down Expand Up @@ -203,28 +210,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
}
}

fn is_lint_ref_type(ty: &Ty) -> bool {
fn is_lint_ref_type<'tcx>(cx: &LateContext<'_, 'tcx>, ty: &Ty) -> bool {
if let TyKind::Rptr(
_,
MutTy {
ty: ref inner,
mutbl: MutImmutable,
},
) = ty.node
{
) = ty.node {
if let TyKind::Path(ref path) = inner.node {
return match_qpath(path, &paths::LINT);
if let Def::Struct(def_id) = cx.tables.qpath_def(path, inner.hir_id) {
return match_def_path(cx.tcx, def_id, &paths::LINT);
}
}
}
false
}

fn is_lint_array_type(ty: &Ty) -> bool {
if let TyKind::Path(ref path) = ty.node {
match_qpath(path, &paths::LINT_ARRAY)
} else {
false
}
false
}

struct LintCollector<'a, 'tcx: 'a> {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub const ITERATOR: [&str; 4] = ["core", "iter", "iterator", "Iterator"];
pub const LATE_CONTEXT: [&str; 4] = ["rustc", "lint", "context", "LateContext"];
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
pub const LINT: [&str; 3] = ["rustc", "lint", "Lint"];
pub const LINT_ARRAY: [&str; 3] = ["rustc", "lint", "LintArray"];
pub const LINT_PASS: [&str; 3] = ["rustc", "lint", "LintPass"];
pub const MEM_DISCRIMINANT: [&str; 3] = ["core", "mem", "discriminant"];
pub const MEM_FORGET: [&str; 3] = ["core", "mem", "forget"];
pub const MEM_REPLACE: [&str; 3] = ["core", "mem", "replace"];
Expand Down
32 changes: 32 additions & 0 deletions tests/ui/lint_without_lint_pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![deny(clippy::internal)]

#![feature(rustc_private)]

#[macro_use]
extern crate rustc;
use rustc::lint;

#[macro_use]
extern crate clippy_lints;

declare_clippy_lint! {
pub TEST_LINT,
correctness,
""
}

declare_clippy_lint! {
pub TEST_LINT_REGISTERED,
correctness,
""
}

pub struct Pass;
impl lint::LintPass for Pass {
fn get_lints(&self) -> lint::LintArray {
lint_array!(TEST_LINT_REGISTERED)
}
}

fn main() {
}
20 changes: 20 additions & 0 deletions tests/ui/lint_without_lint_pass.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: the lint `TEST_LINT` is not added to any `LintPass`
--> $DIR/lint_without_lint_pass.rs:12:1
|
12 | / declare_clippy_lint! {
13 | | pub TEST_LINT,
14 | | correctness,
15 | | ""
16 | | }
| |_^
|
note: lint level defined here
--> $DIR/lint_without_lint_pass.rs:1:9
|
1 | #![deny(clippy::internal)]
| ^^^^^^^^^^^^^^^^
= note: #[deny(clippy::lint_without_lint_pass)] implied by #[deny(clippy::internal)]
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error