1 //! checks for attributes
3 use crate::utils::get_attr;
4 use rustc_ast::ast::{Attribute, InlineAsmTemplatePiece};
6 use rustc_lint::{LateContext, LateLintPass, LintContext};
7 use rustc_session::Session;
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
11 /// **What it does:** Dumps every ast/hir node which has the `#[clippy::dump]`
24 /// visibility inherited from outer item
25 /// extern crate dylib source: "/path/to/foo.so"
27 pub DEEP_CODE_INSPECTION,
29 "helper to dump info about code"
32 declare_lint_pass!(DeepCodeInspector => [DEEP_CODE_INSPECTION]);
34 impl<'tcx> LateLintPass<'tcx> for DeepCodeInspector {
35 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
36 if !has_attr(cx.sess(), &item.attrs) {
42 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
43 if !has_attr(cx.sess(), &item.attrs) {
46 println!("impl item `{}`", item.ident.name);
48 hir::VisibilityKind::Public => println!("public"),
49 hir::VisibilityKind::Crate(_) => println!("visible crate wide"),
50 hir::VisibilityKind::Restricted { ref path, .. } => println!(
51 "visible in module `{}`",
52 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(path, false))
54 hir::VisibilityKind::Inherited => println!("visibility inherited from outer item"),
56 if item.defaultness.is_default() {
60 hir::ImplItemKind::Const(_, body_id) => {
61 println!("associated constant");
62 print_expr(cx, &cx.tcx.hir().body(body_id).value, 1);
64 hir::ImplItemKind::Fn(..) => println!("method"),
65 hir::ImplItemKind::TyAlias(_) => println!("associated type"),
68 // fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx
70 // if !has_attr(&item.attrs) {
75 // fn check_variant(&mut self, cx: &LateContext<'tcx>, var: &'tcx
78 // if !has_attr(&var.node.attrs) {
83 // fn check_struct_field(&mut self, cx: &LateContext<'tcx>, field: &'tcx
84 // hir::StructField) {
85 // if !has_attr(&field.attrs) {
91 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
92 if !has_attr(cx.sess(), &expr.attrs) {
95 print_expr(cx, expr, 0);
98 fn check_arm(&mut self, cx: &LateContext<'tcx>, arm: &'tcx hir::Arm<'_>) {
99 if !has_attr(cx.sess(), &arm.attrs) {
102 print_pat(cx, &arm.pat, 1);
103 if let Some(ref guard) = arm.guard {
105 print_guard(cx, guard, 1);
108 print_expr(cx, &arm.body, 1);
111 fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx hir::Stmt<'_>) {
112 if !has_attr(cx.sess(), stmt.kind.attrs(|id| cx.tcx.hir().item(id.id))) {
116 hir::StmtKind::Local(ref local) => {
117 println!("local variable of type {}", cx.typeck_results().node_type(local.hir_id));
118 println!("pattern:");
119 print_pat(cx, &local.pat, 0);
120 if let Some(ref e) = local.init {
121 println!("init expression:");
122 print_expr(cx, e, 0);
125 hir::StmtKind::Item(_) => println!("item decl"),
126 hir::StmtKind::Expr(ref e) | hir::StmtKind::Semi(ref e) => print_expr(cx, e, 0),
129 // fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx
130 // hir::ForeignItem) {
131 // if !has_attr(&item.attrs) {
138 fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool {
139 get_attr(sess, attrs, "dump").count() > 0
142 #[allow(clippy::similar_names)]
143 #[allow(clippy::too_many_lines)]
144 fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) {
145 let ind = " ".repeat(indent);
146 println!("{}+", ind);
147 println!("{}ty: {}", ind, cx.typeck_results().expr_ty(expr));
149 "{}adjustments: {:?}",
151 cx.typeck_results().adjustments().get(expr.hir_id)
154 hir::ExprKind::Box(ref e) => {
155 println!("{}Box", ind);
156 print_expr(cx, e, indent + 1);
158 hir::ExprKind::Array(v) => {
159 println!("{}Array", ind);
161 print_expr(cx, e, indent + 1);
164 hir::ExprKind::Call(ref func, args) => {
165 println!("{}Call", ind);
166 println!("{}function:", ind);
167 print_expr(cx, func, indent + 1);
168 println!("{}arguments:", ind);
170 print_expr(cx, arg, indent + 1);
173 hir::ExprKind::MethodCall(ref path, _, args, _) => {
174 println!("{}MethodCall", ind);
175 println!("{}method name: {}", ind, path.ident.name);
177 print_expr(cx, arg, indent + 1);
180 hir::ExprKind::Tup(v) => {
181 println!("{}Tup", ind);
183 print_expr(cx, e, indent + 1);
186 hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
187 println!("{}Binary", ind);
188 println!("{}op: {:?}", ind, op.node);
189 println!("{}lhs:", ind);
190 print_expr(cx, lhs, indent + 1);
191 println!("{}rhs:", ind);
192 print_expr(cx, rhs, indent + 1);
194 hir::ExprKind::Unary(op, ref inner) => {
195 println!("{}Unary", ind);
196 println!("{}op: {:?}", ind, op);
197 print_expr(cx, inner, indent + 1);
199 hir::ExprKind::Lit(ref lit) => {
200 println!("{}Lit", ind);
201 println!("{}{:?}", ind, lit);
203 hir::ExprKind::Cast(ref e, ref target) => {
204 println!("{}Cast", ind);
205 print_expr(cx, e, indent + 1);
206 println!("{}target type: {:?}", ind, target);
208 hir::ExprKind::Type(ref e, ref target) => {
209 println!("{}Type", ind);
210 print_expr(cx, e, indent + 1);
211 println!("{}target type: {:?}", ind, target);
213 hir::ExprKind::Loop(..) => {
214 println!("{}Loop", ind);
216 hir::ExprKind::Match(ref cond, _, ref source) => {
217 println!("{}Match", ind);
218 println!("{}condition:", ind);
219 print_expr(cx, cond, indent + 1);
220 println!("{}source: {:?}", ind, source);
222 hir::ExprKind::Closure(ref clause, _, _, _, _) => {
223 println!("{}Closure", ind);
224 println!("{}clause: {:?}", ind, clause);
226 hir::ExprKind::Yield(ref sub, _) => {
227 println!("{}Yield", ind);
228 print_expr(cx, sub, indent + 1);
230 hir::ExprKind::Block(_, _) => {
231 println!("{}Block", ind);
233 hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
234 println!("{}Assign", ind);
235 println!("{}lhs:", ind);
236 print_expr(cx, lhs, indent + 1);
237 println!("{}rhs:", ind);
238 print_expr(cx, rhs, indent + 1);
240 hir::ExprKind::AssignOp(ref binop, ref lhs, ref rhs) => {
241 println!("{}AssignOp", ind);
242 println!("{}op: {:?}", ind, binop.node);
243 println!("{}lhs:", ind);
244 print_expr(cx, lhs, indent + 1);
245 println!("{}rhs:", ind);
246 print_expr(cx, rhs, indent + 1);
248 hir::ExprKind::Field(ref e, ident) => {
249 println!("{}Field", ind);
250 println!("{}field name: {}", ind, ident.name);
251 println!("{}struct expr:", ind);
252 print_expr(cx, e, indent + 1);
254 hir::ExprKind::Index(ref arr, ref idx) => {
255 println!("{}Index", ind);
256 println!("{}array expr:", ind);
257 print_expr(cx, arr, indent + 1);
258 println!("{}index expr:", ind);
259 print_expr(cx, idx, indent + 1);
261 hir::ExprKind::Path(hir::QPath::Resolved(ref ty, ref path)) => {
262 println!("{}Resolved Path, {:?}", ind, ty);
263 println!("{}path: {:?}", ind, path);
265 hir::ExprKind::Path(hir::QPath::TypeRelative(ref ty, ref seg)) => {
266 println!("{}Relative Path, {:?}", ind, ty);
267 println!("{}seg: {:?}", ind, seg);
269 hir::ExprKind::Path(hir::QPath::LangItem(lang_item, ..)) => {
270 println!("{}Lang Item Path, {:?}", ind, lang_item.name());
272 hir::ExprKind::AddrOf(kind, ref muta, ref e) => {
273 println!("{}AddrOf", ind);
274 println!("kind: {:?}", kind);
275 println!("mutability: {:?}", muta);
276 print_expr(cx, e, indent + 1);
278 hir::ExprKind::Break(_, ref e) => {
279 println!("{}Break", ind);
280 if let Some(ref e) = *e {
281 print_expr(cx, e, indent + 1);
284 hir::ExprKind::Continue(_) => println!("{}Again", ind),
285 hir::ExprKind::Ret(ref e) => {
286 println!("{}Ret", ind);
287 if let Some(ref e) = *e {
288 print_expr(cx, e, indent + 1);
291 hir::ExprKind::InlineAsm(ref asm) => {
292 println!("{}InlineAsm", ind);
293 println!("{}template: {}", ind, InlineAsmTemplatePiece::to_string(asm.template));
294 println!("{}options: {:?}", ind, asm.options);
295 println!("{}operands:", ind);
296 for (op, _op_sp) in asm.operands {
298 hir::InlineAsmOperand::In { expr, .. }
299 | hir::InlineAsmOperand::InOut { expr, .. }
300 | hir::InlineAsmOperand::Const { expr }
301 | hir::InlineAsmOperand::Sym { expr } => print_expr(cx, expr, indent + 1),
302 hir::InlineAsmOperand::Out { expr, .. } => {
303 if let Some(expr) = expr {
304 print_expr(cx, expr, indent + 1);
307 hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
308 print_expr(cx, in_expr, indent + 1);
309 if let Some(out_expr) = out_expr {
310 print_expr(cx, out_expr, indent + 1);
316 hir::ExprKind::LlvmInlineAsm(ref asm) => {
317 let inputs = &asm.inputs_exprs;
318 let outputs = &asm.outputs_exprs;
319 println!("{}LlvmInlineAsm", ind);
320 println!("{}inputs:", ind);
321 for e in inputs.iter() {
322 print_expr(cx, e, indent + 1);
324 println!("{}outputs:", ind);
325 for e in outputs.iter() {
326 print_expr(cx, e, indent + 1);
329 hir::ExprKind::Struct(ref path, fields, ref base) => {
330 println!("{}Struct", ind);
331 println!("{}path: {:?}", ind, path);
332 for field in fields {
333 println!("{}field \"{}\":", ind, field.ident.name);
334 print_expr(cx, &field.expr, indent + 1);
336 if let Some(ref base) = *base {
337 println!("{}base:", ind);
338 print_expr(cx, base, indent + 1);
341 hir::ExprKind::ConstBlock(ref anon_const) => {
342 println!("{}ConstBlock", ind);
343 println!("{}anon_const:", ind);
344 print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
346 hir::ExprKind::Repeat(ref val, ref anon_const) => {
347 println!("{}Repeat", ind);
348 println!("{}value:", ind);
349 print_expr(cx, val, indent + 1);
350 println!("{}repeat count:", ind);
351 print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
353 hir::ExprKind::Err => {
354 println!("{}Err", ind);
356 hir::ExprKind::DropTemps(ref e) => {
357 println!("{}DropTemps", ind);
358 print_expr(cx, e, indent + 1);
363 fn print_item(cx: &LateContext<'_>, item: &hir::Item<'_>) {
364 let did = cx.tcx.hir().local_def_id(item.hir_id);
365 println!("item `{}`", item.ident.name);
366 match item.vis.node {
367 hir::VisibilityKind::Public => println!("public"),
368 hir::VisibilityKind::Crate(_) => println!("visible crate wide"),
369 hir::VisibilityKind::Restricted { ref path, .. } => println!(
370 "visible in module `{}`",
371 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(path, false))
373 hir::VisibilityKind::Inherited => println!("visibility inherited from outer item"),
376 hir::ItemKind::ExternCrate(ref _renamed_from) => {
377 let def_id = cx.tcx.hir().local_def_id(item.hir_id);
378 if let Some(crate_id) = cx.tcx.extern_mod_stmt_cnum(def_id) {
379 let source = cx.tcx.used_crate_source(crate_id);
380 if let Some(ref src) = source.dylib {
381 println!("extern crate dylib source: {:?}", src.0);
383 if let Some(ref src) = source.rlib {
384 println!("extern crate rlib source: {:?}", src.0);
387 println!("weird extern crate without a crate id");
390 hir::ItemKind::Use(ref path, ref kind) => println!("{:?}, {:?}", path, kind),
391 hir::ItemKind::Static(..) => println!("static item of type {:#?}", cx.tcx.type_of(did)),
392 hir::ItemKind::Const(..) => println!("const item of type {:#?}", cx.tcx.type_of(did)),
393 hir::ItemKind::Fn(..) => {
394 let item_ty = cx.tcx.type_of(did);
395 println!("function of type {:#?}", item_ty);
397 hir::ItemKind::Mod(..) => println!("module"),
398 hir::ItemKind::ForeignMod { abi, .. } => println!("foreign module with abi: {}", abi),
399 hir::ItemKind::GlobalAsm(ref asm) => println!("global asm: {:?}", asm),
400 hir::ItemKind::TyAlias(..) => {
401 println!("type alias for {:?}", cx.tcx.type_of(did));
403 hir::ItemKind::OpaqueTy(..) => {
404 println!("existential type with real type {:?}", cx.tcx.type_of(did));
406 hir::ItemKind::Enum(..) => {
407 println!("enum definition of type {:?}", cx.tcx.type_of(did));
409 hir::ItemKind::Struct(..) => {
410 println!("struct definition of type {:?}", cx.tcx.type_of(did));
412 hir::ItemKind::Union(..) => {
413 println!("union definition of type {:?}", cx.tcx.type_of(did));
415 hir::ItemKind::Trait(..) => {
416 println!("trait decl");
417 if cx.tcx.trait_is_auto(did.to_def_id()) {
418 println!("trait is auto");
420 println!("trait is not auto");
423 hir::ItemKind::TraitAlias(..) => {
424 println!("trait alias");
426 hir::ItemKind::Impl {
427 of_trait: Some(ref _trait_ref),
430 println!("trait impl");
432 hir::ItemKind::Impl { of_trait: None, .. } => {
438 #[allow(clippy::similar_names)]
439 #[allow(clippy::too_many_lines)]
440 fn print_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, indent: usize) {
441 let ind = " ".repeat(indent);
442 println!("{}+", ind);
444 hir::PatKind::Wild => println!("{}Wild", ind),
445 hir::PatKind::Binding(ref mode, .., ident, ref inner) => {
446 println!("{}Binding", ind);
447 println!("{}mode: {:?}", ind, mode);
448 println!("{}name: {}", ind, ident.name);
449 if let Some(ref inner) = *inner {
450 println!("{}inner:", ind);
451 print_pat(cx, inner, indent + 1);
454 hir::PatKind::Or(fields) => {
455 println!("{}Or", ind);
456 for field in fields {
457 print_pat(cx, field, indent + 1);
460 hir::PatKind::Struct(ref path, fields, ignore) => {
461 println!("{}Struct", ind);
465 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
467 println!("{}ignore leftover fields: {}", ind, ignore);
468 println!("{}fields:", ind);
469 for field in fields {
470 println!("{} field name: {}", ind, field.ident.name);
471 if field.is_shorthand {
472 println!("{} in shorthand notation", ind);
474 print_pat(cx, &field.pat, indent + 1);
477 hir::PatKind::TupleStruct(ref path, fields, opt_dots_position) => {
478 println!("{}TupleStruct", ind);
482 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
484 if let Some(dot_position) = opt_dots_position {
485 println!("{}dot position: {}", ind, dot_position);
487 for field in fields {
488 print_pat(cx, field, indent + 1);
491 hir::PatKind::Path(hir::QPath::Resolved(ref ty, ref path)) => {
492 println!("{}Resolved Path, {:?}", ind, ty);
493 println!("{}path: {:?}", ind, path);
495 hir::PatKind::Path(hir::QPath::TypeRelative(ref ty, ref seg)) => {
496 println!("{}Relative Path, {:?}", ind, ty);
497 println!("{}seg: {:?}", ind, seg);
499 hir::PatKind::Path(hir::QPath::LangItem(lang_item, ..)) => {
500 println!("{}Lang Item Path, {:?}", ind, lang_item.name());
502 hir::PatKind::Tuple(pats, opt_dots_position) => {
503 println!("{}Tuple", ind);
504 if let Some(dot_position) = opt_dots_position {
505 println!("{}dot position: {}", ind, dot_position);
508 print_pat(cx, field, indent + 1);
511 hir::PatKind::Box(ref inner) => {
512 println!("{}Box", ind);
513 print_pat(cx, inner, indent + 1);
515 hir::PatKind::Ref(ref inner, ref muta) => {
516 println!("{}Ref", ind);
517 println!("{}mutability: {:?}", ind, muta);
518 print_pat(cx, inner, indent + 1);
520 hir::PatKind::Lit(ref e) => {
521 println!("{}Lit", ind);
522 print_expr(cx, e, indent + 1);
524 hir::PatKind::Range(ref l, ref r, ref range_end) => {
525 println!("{}Range", ind);
526 if let Some(expr) = l {
527 print_expr(cx, expr, indent + 1);
529 if let Some(expr) = r {
530 print_expr(cx, expr, indent + 1);
533 hir::RangeEnd::Included => println!("{} end included", ind),
534 hir::RangeEnd::Excluded => println!("{} end excluded", ind),
537 hir::PatKind::Slice(first_pats, ref range, last_pats) => {
538 println!("{}Slice [a, b, ..i, y, z]", ind);
540 for pat in first_pats {
541 print_pat(cx, pat, indent + 1);
544 if let Some(ref pat) = *range {
545 print_pat(cx, pat, indent + 1);
548 for pat in last_pats {
549 print_pat(cx, pat, indent + 1);
555 fn print_guard(cx: &LateContext<'_>, guard: &hir::Guard<'_>, indent: usize) {
556 let ind = " ".repeat(indent);
557 println!("{}+", ind);
559 hir::Guard::If(expr) => {
560 println!("{}If", ind);
561 print_expr(cx, expr, indent + 1);
563 hir::Guard::IfLet(pat, expr) => {
564 println!("{}IfLet", ind);
565 print_pat(cx, pat, indent + 1);
566 print_expr(cx, expr, indent + 1);