From ff3efc759ef08873ca3b2d5f1cf0f3b5b0b79020 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 18 Oct 2016 16:57:39 +0200 Subject: [PATCH 1/5] initial implementation of the code inspector lint --- clippy_lints/src/lib.rs | 1 + clippy_lints/src/utils/inspector.rs | 179 ++++++++++++++++++++++++++++ clippy_lints/src/utils/mod.rs | 1 + 3 files changed, 181 insertions(+) create mode 100644 clippy_lints/src/utils/inspector.rs diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index d3700216b912..a1dfbbc4e092 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -172,6 +172,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { reg.register_late_lint_pass(box serde::Serde); reg.register_early_lint_pass(box utils::internal_lints::Clippy); reg.register_late_lint_pass(box utils::internal_lints::LintWithoutLintPass::default()); + reg.register_late_lint_pass(box utils::inspector::Pass); reg.register_late_lint_pass(box types::TypePass); reg.register_late_lint_pass(box booleans::NonminimalBool); reg.register_late_lint_pass(box eq_op::EqOp); diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs new file mode 100644 index 000000000000..9bd658acd289 --- /dev/null +++ b/clippy_lints/src/utils/inspector.rs @@ -0,0 +1,179 @@ +//! checks for attributes + +use rustc::lint::*; +use rustc::hir; +use syntax::ast::{Attribute, MetaItemKind}; + +/// **What it does:** Dumps every ast/hir node which has the `#[inspect]` attribute +/// +/// **Why is this bad?** 😈 +/// +/// **Known problems:** ∅ +/// +/// **Example:** +/// ```rust +/// #[inspect] +/// extern crate foo; +/// ``` +declare_lint! { + pub DEEP_CODE_INSPECTION, + Warn, + "helper to dump info about code" +} + +pub struct Pass; + +impl LintPass for Pass { + fn get_lints(&self) -> LintArray { + lint_array!(DEEP_CODE_INSPECTION) + } +} + +#[allow(print_stdout, use_debug)] +impl LateLintPass for Pass { + fn check_item(&mut self, cx: &LateContext, item: &hir::Item) { + if !has_inspect_attr(&item.attrs) { + return; + } + let did = cx.tcx.map.local_def_id(item.id); + println!("item `{}`", item.name); + match item.vis { + hir::Visibility::Public => println!("public"), + hir::Visibility::Crate => println!("visible crate wide"), + hir::Visibility::Restricted { ref path, .. } => println!("visible in module `{}`", path), + hir::Visibility::Inherited => println!("visibility inherited from outer item"), + } + match item.node { + hir::ItemExternCrate(ref _renamed_from) => { + if let Some(crate_id) = cx.tcx.sess.cstore.extern_mod_stmt_cnum(item.id) { + let source = cx.tcx.sess.cstore.used_crate_source(crate_id); + if let Some(src) = source.dylib { + println!("extern crate dylib source: {:?}", src.0); + } + if let Some(src) = source.rlib { + println!("extern crate rlib source: {:?}", src.0); + } + } else { + println!("weird extern crate without a crate id"); + } + } + hir::ItemUse(ref path) => println!("{:?}", path.node), + hir::ItemStatic(..) => println!("static item: {:#?}", cx.tcx.opt_lookup_item_type(did)), + hir::ItemConst(..) => println!("const item: {:#?}", cx.tcx.opt_lookup_item_type(did)), + hir::ItemFn(..) => { + let item_ty = cx.tcx.opt_lookup_item_type(did); + println!("function: {:#?}", item_ty); + }, + hir::ItemMod(..) => println!("module"), + hir::ItemForeignMod(ref fm) => println!("foreign module with abi: {}", fm.abi), + hir::ItemTy(..) => { + println!("type alias: {:?}", cx.tcx.opt_lookup_item_type(did)); + }, + hir::ItemEnum(..) => { + println!("enum definition: {:?}", cx.tcx.opt_lookup_item_type(did)); + }, + hir::ItemStruct(..) => { + println!("struct definition: {:?}", cx.tcx.opt_lookup_item_type(did)); + }, + hir::ItemUnion(..) => { + println!("union definition: {:?}", cx.tcx.opt_lookup_item_type(did)); + }, + hir::ItemTrait(..) => { + println!("trait decl"); + if cx.tcx.trait_has_default_impl(did) { + println!("trait has a default impl"); + } else { + println!("trait has no default impl"); + } + }, + hir::ItemDefaultImpl(_, ref trait_ref) => { + let trait_did = cx.tcx.map.local_def_id(trait_ref.ref_id); + println!("default impl for `{:?}`", cx.tcx.item_path_str(trait_did)); + }, + hir::ItemImpl(_, _, _, Some(ref trait_ref), _, _) => { + let trait_did = cx.tcx.map.local_def_id(trait_ref.ref_id); + println!("impl of trait `{:?}`", cx.tcx.item_path_str(trait_did)); + }, + hir::ItemImpl(_, _, _, None, _, _) => { + println!("impl"); + }, + } + } + +/* + fn check_impl_item(&mut self, cx: &LateContext, item: &hir::ImplItem) { + if !has_inspect_attr(&item.attrs) { + return; + } + } + + fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) { + if !has_inspect_attr(&item.attrs) { + return; + } + } + + fn check_variant(&mut self, cx: &LateContext, var: &hir::Variant, _: &hir::Generics) { + if !has_inspect_attr(&var.node.attrs) { + return; + } + } + + fn check_struct_field(&mut self, cx: &LateContext, field: &hir::StructField) { + if !has_inspect_attr(&field.attrs) { + return; + } + } +*/ + + fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) { + if !has_inspect_attr(&expr.attrs) { + return; + } + println!("expression type: {}", cx.tcx.node_id_to_type(expr.id)); + } + + fn check_decl(&mut self, cx: &LateContext, decl: &hir::Decl) { + if !has_inspect_attr(decl.node.attrs()) { + return; + } + match decl.node { + hir::DeclLocal(ref local) => { + println!("local variable of type {}", cx.tcx.node_id_to_type(local.id)); + }, + hir::DeclItem(_) => println!("item decl"), + } + } +/* + fn check_arm(&mut self, cx: &LateContext, arm: &hir::Arm) { + if !has_inspect_attr(&arm.attrs) { + return; + } + } + + fn check_stmt(&mut self, cx: &LateContext, stmt: &hir::Stmt) { + if !has_inspect_attr(stmt.node.attrs()) { + return; + } + } + + fn check_local(&mut self, cx: &LateContext, local: &hir::Local) { + if !has_inspect_attr(&local.attrs) { + return; + } + } + + fn check_foreign_item(&mut self, cx: &LateContext, item: &hir::ForeignItem) { + if !has_inspect_attr(&item.attrs) { + return; + } + } +*/ +} + +fn has_inspect_attr(attrs: &[Attribute]) -> bool { + attrs.iter().any(|attr| match attr.node.value.node { + MetaItemKind::Word(ref word) => word == "inspect", + _ => false, + }) +} diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 6c83d328a004..903215c591cf 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -25,6 +25,7 @@ pub mod constants; mod hir; pub mod paths; pub mod sugg; +pub mod inspector; pub mod internal_lints; pub use self::hir::{SpanlessEq, SpanlessHash}; From a177d85681d0c8eeaf641f2b1b76c6f755971a72 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 18 Oct 2016 17:29:01 +0200 Subject: [PATCH 2/5] rename inspection attribute to `clippy_dump` --- clippy_lints/src/utils/inspector.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index 9bd658acd289..4fc6a7958c2e 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -4,7 +4,7 @@ use rustc::lint::*; use rustc::hir; use syntax::ast::{Attribute, MetaItemKind}; -/// **What it does:** Dumps every ast/hir node which has the `#[inspect]` attribute +/// **What it does:** Dumps every ast/hir node which has the `#[clippy_dump]` attribute /// /// **Why is this bad?** 😈 /// @@ -32,7 +32,7 @@ impl LintPass for Pass { #[allow(print_stdout, use_debug)] impl LateLintPass for Pass { fn check_item(&mut self, cx: &LateContext, item: &hir::Item) { - if !has_inspect_attr(&item.attrs) { + if !has_attr(&item.attrs) { return; } let did = cx.tcx.map.local_def_id(item.id); @@ -102,39 +102,39 @@ impl LateLintPass for Pass { /* fn check_impl_item(&mut self, cx: &LateContext, item: &hir::ImplItem) { - if !has_inspect_attr(&item.attrs) { + if !has_attr(&item.attrs) { return; } } fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) { - if !has_inspect_attr(&item.attrs) { + if !has_attr(&item.attrs) { return; } } fn check_variant(&mut self, cx: &LateContext, var: &hir::Variant, _: &hir::Generics) { - if !has_inspect_attr(&var.node.attrs) { + if !has_attr(&var.node.attrs) { return; } } fn check_struct_field(&mut self, cx: &LateContext, field: &hir::StructField) { - if !has_inspect_attr(&field.attrs) { + if !has_attr(&field.attrs) { return; } } */ fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) { - if !has_inspect_attr(&expr.attrs) { + if !has_attr(&expr.attrs) { return; } println!("expression type: {}", cx.tcx.node_id_to_type(expr.id)); } fn check_decl(&mut self, cx: &LateContext, decl: &hir::Decl) { - if !has_inspect_attr(decl.node.attrs()) { + if !has_attr(decl.node.attrs()) { return; } match decl.node { @@ -146,34 +146,34 @@ impl LateLintPass for Pass { } /* fn check_arm(&mut self, cx: &LateContext, arm: &hir::Arm) { - if !has_inspect_attr(&arm.attrs) { + if !has_attr(&arm.attrs) { return; } } fn check_stmt(&mut self, cx: &LateContext, stmt: &hir::Stmt) { - if !has_inspect_attr(stmt.node.attrs()) { + if !has_attr(stmt.node.attrs()) { return; } } fn check_local(&mut self, cx: &LateContext, local: &hir::Local) { - if !has_inspect_attr(&local.attrs) { + if !has_attr(&local.attrs) { return; } } fn check_foreign_item(&mut self, cx: &LateContext, item: &hir::ForeignItem) { - if !has_inspect_attr(&item.attrs) { + if !has_attr(&item.attrs) { return; } } */ } -fn has_inspect_attr(attrs: &[Attribute]) -> bool { +fn has_attr(attrs: &[Attribute]) -> bool { attrs.iter().any(|attr| match attr.node.value.node { - MetaItemKind::Word(ref word) => word == "inspect", + MetaItemKind::Word(ref word) => word == "clippy_dump", _ => false, }) } From 1248159f0dbbb00fe8731345deb347997fc44d69 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 24 Oct 2016 14:28:58 +0200 Subject: [PATCH 3/5] add expression and pattern dump --- clippy_lints/src/lib.rs | 1 + clippy_lints/src/utils/inspector.rs | 291 +++++++++++++++++++++++++++- 2 files changed, 291 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index a1dfbbc4e092..4c97c5d465f0 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -7,6 +7,7 @@ #![feature(rustc_private)] #![feature(slice_patterns)] #![feature(stmt_expr_attributes)] +#![feature(repeat_str)] #![allow(indexing_slicing, shadow_reuse, unknown_lints, missing_docs_in_private_items)] diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index 4fc6a7958c2e..fc9f398f16c2 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -130,7 +130,7 @@ impl LateLintPass for Pass { if !has_attr(&expr.attrs) { return; } - println!("expression type: {}", cx.tcx.node_id_to_type(expr.id)); + print_expr(cx, expr, 0); } fn check_decl(&mut self, cx: &LateContext, decl: &hir::Decl) { @@ -149,6 +149,15 @@ impl LateLintPass for Pass { if !has_attr(&arm.attrs) { return; } + for pat in &arm.pats { + print_pat(cx, pat, 1); + } + if let Some(ref guard) = arm.guard { + println!("guard:"); + print_expr(cx, guard, 1); + } + println!("body:"); + print_expr(cx, &arm.body, 1); } fn check_stmt(&mut self, cx: &LateContext, stmt: &hir::Stmt) { @@ -177,3 +186,283 @@ fn has_attr(attrs: &[Attribute]) -> bool { _ => false, }) } + +fn print_decl(cx: &LateContext, decl: &hir::Decl) { + match decl.node { + hir::DeclLocal(ref local) => { + println!("local variable of type {}", cx.tcx.node_id_to_type(local.id)); + println!("pattern:"); + print_pat(cx, &local.pat, 0); + if let Some(ref e) = local.init { + println!("init expression:"); + print_expr(cx, e, 0); + } + }, + hir::DeclItem(_) => println!("item decl"), + } +} + +fn print_expr(cx: &LateContext, expr: &hir::Expr, indent: usize) { + let ind = " ".repeat(indent); + let ty = cx.tcx.node_id_to_type(expr.id); + println!("{}+", ind); + match expr.node { + hir::ExprBox(ref e) => { + println!("{}Box, {}", ind, ty); + print_expr(cx, e, indent + 1); + }, + hir::ExprArray(ref v) => { + println!("{}Array, {}", ind, ty); + for e in v { + print_expr(cx, e, indent + 1); + } + }, + hir::ExprCall(ref func, ref args) => { + println!("{}Call, {}", ind, ty); + println!("{}function:", ind); + print_expr(cx, func, indent + 1); + println!("{}arguments:", ind); + for arg in args { + print_expr(cx, arg, indent + 1); + } + }, + hir::ExprMethodCall(ref name, _, ref args) => { + println!("{}MethodCall, {}", ind, ty); + println!("{}method name: {}", ind, name.node); + for arg in args { + print_expr(cx, arg, indent + 1); + } + }, + hir::ExprTup(ref v) => { + println!("{}Tup, {}", ind, ty); + for e in v { + print_expr(cx, e, indent + 1); + } + }, + hir::ExprBinary(op, ref lhs, ref rhs) => { + println!("{}Binary, {}", ind, ty); + println!("{}op: {:?}", ind, op.node); + println!("{}lhs:", ind); + print_expr(cx, lhs, indent + 1); + println!("{}rhs:", ind); + print_expr(cx, rhs, indent + 1); + }, + hir::ExprUnary(op, ref inner) => { + println!("{}Unary, {}", ind, ty); + println!("{}op: {:?}", ind, op); + print_expr(cx, inner, indent + 1); + }, + hir::ExprLit(ref lit) => { + println!("{}Lit, {}", ind, ty); + println!("{}{:?}", ind, lit); + }, + hir::ExprCast(ref e, ref target) => { + println!("{}Cast, {}", ind, ty); + print_expr(cx, e, indent + 1); + println!("{}target type: {:?}", ind, target); + }, + hir::ExprType(ref e, ref target) => { + println!("{}Type, {}", ind, ty); + print_expr(cx, e, indent + 1); + println!("{}target type: {:?}", ind, target); + }, + hir::ExprIf(ref e, _, ref els) => { + println!("{}If, {}", ind, ty); + println!("{}condition:", ind); + print_expr(cx, e, indent + 1); + if let Some(ref els) = *els { + println!("{}else:", ind); + print_expr(cx, els, indent + 1); + } + }, + hir::ExprWhile(ref cond, _, _) => { + println!("{}While, {}", ind, ty); + println!("{}condition:", ind); + print_expr(cx, cond, indent + 1); + }, + hir::ExprLoop(..) => { + println!("{}Loop, {}", ind, ty); + }, + hir::ExprMatch(ref cond, _, ref source) => { + println!("{}Match, {}", ind, ty); + println!("{}condition:", ind); + print_expr(cx, cond, indent + 1); + println!("{}source: {:?}", ind, source); + }, + hir::ExprClosure(ref clause, _, _, _) => { + println!("{}Closure, {}", ind, ty); + println!("{}clause: {:?}", ind, clause); + }, + hir::ExprBlock(_) => { + println!("{}Block, {}", ind, ty); + }, + hir::ExprAssign(ref lhs, ref rhs) => { + println!("{}Assign, {}", ind, ty); + println!("{}lhs:", ind); + print_expr(cx, lhs, indent + 1); + println!("{}rhs:", ind); + print_expr(cx, rhs, indent + 1); + }, + hir::ExprAssignOp(ref binop, ref lhs, ref rhs) => { + println!("{}AssignOp, {}", ind, ty); + println!("{}op: {:?}", ind, binop.node); + println!("{}lhs:", ind); + print_expr(cx, lhs, indent + 1); + println!("{}rhs:", ind); + print_expr(cx, rhs, indent + 1); + }, + hir::ExprField(ref e, ref name) => { + println!("{}Field, {}", ind, ty); + println!("{}field name: {}", ind, name.node); + println!("{}struct expr:", ind); + print_expr(cx, e, indent + 1); + }, + hir::ExprTupField(ref e, ref idx) => { + println!("{}TupField, {}", ind, ty); + println!("{}field index: {}", ind, idx.node); + println!("{}tuple expr:", ind); + print_expr(cx, e, indent + 1); + }, + hir::ExprIndex(ref arr, ref idx) => { + println!("{}Index, {}", ind, ty); + println!("{}array expr:", ind); + print_expr(cx, arr, indent + 1); + println!("{}index expr:", ind); + print_expr(cx, idx, indent + 1); + }, + hir::ExprPath(ref sel, ref path) => { + println!("{}Path, {}", ind, ty); + println!("{}self: {:?}", ind, sel); + println!("{}path: {:?}", ind, path); + }, + hir::ExprAddrOf(ref muta, ref e) => { + println!("{}AddrOf, {}", ind, ty); + println!("mutability: {:?}", muta); + print_expr(cx, e, indent + 1); + }, + hir::ExprBreak(_) => println!("{}Break, {}", ind, ty), + hir::ExprAgain(_) => println!("{}Again, {}", ind, ty), + hir::ExprRet(ref e) => { + println!("{}Ret, {}", ind, ty); + if let Some(ref e) = *e { + print_expr(cx, e, indent + 1); + } + }, + hir::ExprInlineAsm(_, ref input, ref output) => { + println!("{}InlineAsm, {}", ind, ty); + println!("{}inputs:", ind); + for e in input { + print_expr(cx, e, indent + 1); + } + println!("{}outputs:", ind); + for e in output { + print_expr(cx, e, indent + 1); + } + }, + hir::ExprStruct(ref path, ref fields, ref base) => { + println!("{}Struct, {}", ind, ty); + println!("{}path: {:?}", ind, path); + for field in fields { + println!("{}field \"{}\":", ind, field.name.node); + print_expr(cx, &field.expr, indent + 1); + } + if let Some(ref base) = *base { + println!("{}base:", ind); + print_expr(cx, base, indent + 1); + } + }, + hir::ExprRepeat(ref val, ref n) => { + println!("{}Repeat, {}", ind, ty); + println!("{}value:", ind); + print_expr(cx, val, indent + 1); + println!("{}repeat count:", ind); + print_expr(cx, n, indent + 1); + }, + } +} +fn print_pat(cx: &LateContext, pat: &hir::Pat, indent: usize) { + let ind = " ".repeat(indent); + println!("{}+", ind); + match pat.node { + hir::PatKind::Wild => println!("{}Wild", ind), + hir::PatKind::Binding(ref mode, ref name, ref inner) => { + println!("{}Binding", ind); + println!("{}mode: {:?}", ind, mode); + println!("{}name: {}", ind, name.node); + if let Some(ref inner) = *inner { + println!("{}inner:", ind); + print_pat(cx, inner, indent + 1); + } + }, + hir::PatKind::Struct(ref path, ref fields, ignore) => { + println!("{}Struct", ind); + println!("{}name: {}", ind, path); + println!("{}ignore leftover fields: {}", ind, ignore); + println!("{}fields:", ind); + for field in fields { + println!("{} field name: {}", ind, field.node.name); + if field.node.is_shorthand { + println!("{} in shorthand notation", ind); + } + print_pat(cx, &field.node.pat, indent + 1); + } + }, + hir::PatKind::TupleStruct(ref path, ref pats, opt_dots_position) => { + println!("{}TupleStruct", ind); + println!("{}path: {}", ind, path); + if let Some(dot_position) = opt_dots_position { + println!("{}dot position: {}", ind, dot_position); + } + for field in pats { + print_pat(cx, &field, indent + 1); + } + }, + hir::PatKind::Path(ref sel, ref path) => { + println!("{}Path", ind); + println!("{}self: {:?}", ind, sel); + println!("{}path: {:?}", ind, path); + }, + hir::PatKind::Tuple(ref pats, opt_dots_position) => { + println!("{}Tuple", ind); + if let Some(dot_position) = opt_dots_position { + println!("{}dot position: {}", ind, dot_position); + } + for field in pats { + print_pat(cx, &field, indent + 1); + } + }, + hir::PatKind::Box(ref inner) => { + println!("{}Box", ind); + print_pat(cx, inner, indent + 1); + }, + hir::PatKind::Ref(ref inner, ref muta) => { + println!("{}Ref", ind); + println!("{}mutability: {:?}", ind, muta); + print_pat(cx, inner, indent + 1); + }, + hir::PatKind::Lit(ref e) => { + println!("{}Lit", ind); + print_expr(cx, e, indent + 1); + }, + hir::PatKind::Range(ref l, ref r) => { + println!("{}Range", ind); + print_expr(cx, l, indent + 1); + print_expr(cx, r, indent + 1); + }, + hir::PatKind::Slice(ref start, ref range, ref end) => { + println!("{}Slice [a, b, ..i, y, z]", ind); + println!("[a, b]:"); + for pat in start { + print_pat(cx, pat, indent + 1); + } + println!("i:"); + if let Some(ref pat) = *range { + print_pat(cx, pat, indent + 1); + } + println!("[y, z]:"); + for pat in end { + print_pat(cx, pat, indent + 1); + } + }, + } +} From 6a6dfa59daa8be46df37a594ad0ffeeeccee97cb Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 24 Oct 2016 14:29:09 +0200 Subject: [PATCH 4/5] factor out decl dumping --- clippy_lints/src/utils/inspector.rs | 165 +++++++++++++++------------- 1 file changed, 87 insertions(+), 78 deletions(-) diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index fc9f398f16c2..2707e694a84d 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -35,78 +35,33 @@ impl LateLintPass for Pass { if !has_attr(&item.attrs) { return; } - let did = cx.tcx.map.local_def_id(item.id); - println!("item `{}`", item.name); + print_item(cx, item); + } + + fn check_impl_item(&mut self, cx: &LateContext, item: &hir::ImplItem) { + if !has_attr(&item.attrs) { + return; + } + println!("impl item `{}`", item.name); match item.vis { hir::Visibility::Public => println!("public"), hir::Visibility::Crate => println!("visible crate wide"), hir::Visibility::Restricted { ref path, .. } => println!("visible in module `{}`", path), hir::Visibility::Inherited => println!("visibility inherited from outer item"), } + if item.defaultness.is_default() { + println!("default"); + } match item.node { - hir::ItemExternCrate(ref _renamed_from) => { - if let Some(crate_id) = cx.tcx.sess.cstore.extern_mod_stmt_cnum(item.id) { - let source = cx.tcx.sess.cstore.used_crate_source(crate_id); - if let Some(src) = source.dylib { - println!("extern crate dylib source: {:?}", src.0); - } - if let Some(src) = source.rlib { - println!("extern crate rlib source: {:?}", src.0); - } - } else { - println!("weird extern crate without a crate id"); - } - } - hir::ItemUse(ref path) => println!("{:?}", path.node), - hir::ItemStatic(..) => println!("static item: {:#?}", cx.tcx.opt_lookup_item_type(did)), - hir::ItemConst(..) => println!("const item: {:#?}", cx.tcx.opt_lookup_item_type(did)), - hir::ItemFn(..) => { - let item_ty = cx.tcx.opt_lookup_item_type(did); - println!("function: {:#?}", item_ty); - }, - hir::ItemMod(..) => println!("module"), - hir::ItemForeignMod(ref fm) => println!("foreign module with abi: {}", fm.abi), - hir::ItemTy(..) => { - println!("type alias: {:?}", cx.tcx.opt_lookup_item_type(did)); - }, - hir::ItemEnum(..) => { - println!("enum definition: {:?}", cx.tcx.opt_lookup_item_type(did)); - }, - hir::ItemStruct(..) => { - println!("struct definition: {:?}", cx.tcx.opt_lookup_item_type(did)); - }, - hir::ItemUnion(..) => { - println!("union definition: {:?}", cx.tcx.opt_lookup_item_type(did)); - }, - hir::ItemTrait(..) => { - println!("trait decl"); - if cx.tcx.trait_has_default_impl(did) { - println!("trait has a default impl"); - } else { - println!("trait has no default impl"); - } - }, - hir::ItemDefaultImpl(_, ref trait_ref) => { - let trait_did = cx.tcx.map.local_def_id(trait_ref.ref_id); - println!("default impl for `{:?}`", cx.tcx.item_path_str(trait_did)); - }, - hir::ItemImpl(_, _, _, Some(ref trait_ref), _, _) => { - let trait_did = cx.tcx.map.local_def_id(trait_ref.ref_id); - println!("impl of trait `{:?}`", cx.tcx.item_path_str(trait_did)); - }, - hir::ItemImpl(_, _, _, None, _, _) => { - println!("impl"); + hir::ImplItemKind::Const(_, ref e) => { + println!("associated constant"); + print_expr(cx, e, 1); }, + hir::ImplItemKind::Method(..) => println!("method"), + hir::ImplItemKind::Type(_) => println!("associated type"), } } - /* - fn check_impl_item(&mut self, cx: &LateContext, item: &hir::ImplItem) { - if !has_attr(&item.attrs) { - return; - } - } - fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) { if !has_attr(&item.attrs) { return; @@ -133,18 +88,6 @@ impl LateLintPass for Pass { print_expr(cx, expr, 0); } - fn check_decl(&mut self, cx: &LateContext, decl: &hir::Decl) { - if !has_attr(decl.node.attrs()) { - return; - } - match decl.node { - hir::DeclLocal(ref local) => { - println!("local variable of type {}", cx.tcx.node_id_to_type(local.id)); - }, - hir::DeclItem(_) => println!("item decl"), - } - } -/* fn check_arm(&mut self, cx: &LateContext, arm: &hir::Arm) { if !has_attr(&arm.attrs) { return; @@ -164,13 +107,12 @@ impl LateLintPass for Pass { if !has_attr(stmt.node.attrs()) { return; } - } - - fn check_local(&mut self, cx: &LateContext, local: &hir::Local) { - if !has_attr(&local.attrs) { - return; + match stmt.node { + hir::StmtDecl(ref decl, _) => print_decl(cx, decl), + hir::StmtExpr(ref e, _) | hir::StmtSemi(ref e, _) => print_expr(cx, e, 0), } } +/* fn check_foreign_item(&mut self, cx: &LateContext, item: &hir::ForeignItem) { if !has_attr(&item.attrs) { @@ -380,6 +322,73 @@ fn print_expr(cx: &LateContext, expr: &hir::Expr, indent: usize) { }, } } + +fn print_item(cx: &LateContext, item: &hir::Item) { + let did = cx.tcx.map.local_def_id(item.id); + println!("item `{}`", item.name); + match item.vis { + hir::Visibility::Public => println!("public"), + hir::Visibility::Crate => println!("visible crate wide"), + hir::Visibility::Restricted { ref path, .. } => println!("visible in module `{}`", path), + hir::Visibility::Inherited => println!("visibility inherited from outer item"), + } + match item.node { + hir::ItemExternCrate(ref _renamed_from) => { + if let Some(crate_id) = cx.tcx.sess.cstore.extern_mod_stmt_cnum(item.id) { + let source = cx.tcx.sess.cstore.used_crate_source(crate_id); + if let Some(src) = source.dylib { + println!("extern crate dylib source: {:?}", src.0); + } + if let Some(src) = source.rlib { + println!("extern crate rlib source: {:?}", src.0); + } + } else { + println!("weird extern crate without a crate id"); + } + } + hir::ItemUse(ref path) => println!("{:?}", path.node), + hir::ItemStatic(..) => println!("static item: {:#?}", cx.tcx.opt_lookup_item_type(did)), + hir::ItemConst(..) => println!("const item: {:#?}", cx.tcx.opt_lookup_item_type(did)), + hir::ItemFn(..) => { + let item_ty = cx.tcx.opt_lookup_item_type(did); + println!("function: {:#?}", item_ty); + }, + hir::ItemMod(..) => println!("module"), + hir::ItemForeignMod(ref fm) => println!("foreign module with abi: {}", fm.abi), + hir::ItemTy(..) => { + println!("type alias: {:?}", cx.tcx.opt_lookup_item_type(did)); + }, + hir::ItemEnum(..) => { + println!("enum definition: {:?}", cx.tcx.opt_lookup_item_type(did)); + }, + hir::ItemStruct(..) => { + println!("struct definition: {:?}", cx.tcx.opt_lookup_item_type(did)); + }, + hir::ItemUnion(..) => { + println!("union definition: {:?}", cx.tcx.opt_lookup_item_type(did)); + }, + hir::ItemTrait(..) => { + println!("trait decl"); + if cx.tcx.trait_has_default_impl(did) { + println!("trait has a default impl"); + } else { + println!("trait has no default impl"); + } + }, + hir::ItemDefaultImpl(_, ref trait_ref) => { + let trait_did = cx.tcx.map.local_def_id(trait_ref.ref_id); + println!("default impl for `{:?}`", cx.tcx.item_path_str(trait_did)); + }, + hir::ItemImpl(_, _, _, Some(ref trait_ref), _, _) => { + let trait_did = cx.tcx.map.local_def_id(trait_ref.ref_id); + println!("impl of trait `{:?}`", cx.tcx.item_path_str(trait_did)); + }, + hir::ItemImpl(_, _, _, None, _, _) => { + println!("impl"); + }, + } +} + fn print_pat(cx: &LateContext, pat: &hir::Pat, indent: usize) { let ind = " ".repeat(indent); println!("{}+", ind); From ed50cb133dd338d0e4a5ead4c0903b59d5a42dac Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 24 Oct 2016 15:16:21 +0200 Subject: [PATCH 5/5] dogfood --- clippy_lints/src/utils/inspector.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index 2707e694a84d..0ad216ba04dc 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -1,3 +1,5 @@ +#![allow(print_stdout, use_debug)] + //! checks for attributes use rustc::lint::*; @@ -29,7 +31,6 @@ impl LintPass for Pass { } } -#[allow(print_stdout, use_debug)] impl LateLintPass for Pass { fn check_item(&mut self, cx: &LateContext, item: &hir::Item) { if !has_attr(&item.attrs) { @@ -416,14 +417,14 @@ fn print_pat(cx: &LateContext, pat: &hir::Pat, indent: usize) { print_pat(cx, &field.node.pat, indent + 1); } }, - hir::PatKind::TupleStruct(ref path, ref pats, opt_dots_position) => { + hir::PatKind::TupleStruct(ref path, ref fields, opt_dots_position) => { println!("{}TupleStruct", ind); println!("{}path: {}", ind, path); if let Some(dot_position) = opt_dots_position { println!("{}dot position: {}", ind, dot_position); } - for field in pats { - print_pat(cx, &field, indent + 1); + for field in fields { + print_pat(cx, field, indent + 1); } }, hir::PatKind::Path(ref sel, ref path) => { @@ -437,7 +438,7 @@ fn print_pat(cx: &LateContext, pat: &hir::Pat, indent: usize) { println!("{}dot position: {}", ind, dot_position); } for field in pats { - print_pat(cx, &field, indent + 1); + print_pat(cx, field, indent + 1); } }, hir::PatKind::Box(ref inner) => { @@ -458,10 +459,10 @@ fn print_pat(cx: &LateContext, pat: &hir::Pat, indent: usize) { print_expr(cx, l, indent + 1); print_expr(cx, r, indent + 1); }, - hir::PatKind::Slice(ref start, ref range, ref end) => { + hir::PatKind::Slice(ref first_pats, ref range, ref last_pats) => { println!("{}Slice [a, b, ..i, y, z]", ind); println!("[a, b]:"); - for pat in start { + for pat in first_pats { print_pat(cx, pat, indent + 1); } println!("i:"); @@ -469,7 +470,7 @@ fn print_pat(cx: &LateContext, pat: &hir::Pat, indent: usize) { print_pat(cx, pat, indent + 1); } println!("[y, z]:"); - for pat in end { + for pat in last_pats { print_pat(cx, pat, indent + 1); } },