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<'a, 'tcx> LateLintPass<'a, 'tcx> for DeepCodeInspector {
35 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item<'_>) {
36 if !has_attr(cx.sess(), &item.attrs) {
42 fn check_impl_item(&mut self, cx: &LateContext<'a, '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"),
66 hir::ImplItemKind::OpaqueTy(_) => println!("existential type"),
69 // fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx
71 // if !has_attr(&item.attrs) {
76 // fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, var: &'tcx
79 // if !has_attr(&var.node.attrs) {
84 // fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, field: &'tcx
85 // hir::StructField) {
86 // if !has_attr(&field.attrs) {
92 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
93 if !has_attr(cx.sess(), &expr.attrs) {
96 print_expr(cx, expr, 0);
99 fn check_arm(&mut self, cx: &LateContext<'a, 'tcx>, arm: &'tcx hir::Arm<'_>) {
100 if !has_attr(cx.sess(), &arm.attrs) {
103 print_pat(cx, &arm.pat, 1);
104 if let Some(ref guard) = arm.guard {
106 print_guard(cx, guard, 1);
109 print_expr(cx, &arm.body, 1);
112 fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx hir::Stmt<'_>) {
113 if !has_attr(cx.sess(), stmt.kind.attrs()) {
117 hir::StmtKind::Local(ref local) => {
118 println!("local variable of type {}", cx.tables.node_type(local.hir_id));
119 println!("pattern:");
120 print_pat(cx, &local.pat, 0);
121 if let Some(ref e) = local.init {
122 println!("init expression:");
123 print_expr(cx, e, 0);
126 hir::StmtKind::Item(_) => println!("item decl"),
127 hir::StmtKind::Expr(ref e) | hir::StmtKind::Semi(ref e) => print_expr(cx, e, 0),
130 // fn check_foreign_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx
131 // hir::ForeignItem) {
132 // if !has_attr(&item.attrs) {
139 fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool {
140 get_attr(sess, attrs, "dump").count() > 0
143 #[allow(clippy::similar_names)]
144 #[allow(clippy::too_many_lines)]
145 fn print_expr(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, indent: usize) {
146 let ind = " ".repeat(indent);
147 println!("{}+", ind);
148 println!("{}ty: {}", ind, cx.tables.expr_ty(expr));
149 println!("{}adjustments: {:?}", ind, cx.tables.adjustments().get(expr.hir_id));
151 hir::ExprKind::Box(ref e) => {
152 println!("{}Box", ind);
153 print_expr(cx, e, indent + 1);
155 hir::ExprKind::Array(v) => {
156 println!("{}Array", ind);
158 print_expr(cx, e, indent + 1);
161 hir::ExprKind::Call(ref func, args) => {
162 println!("{}Call", ind);
163 println!("{}function:", ind);
164 print_expr(cx, func, indent + 1);
165 println!("{}arguments:", ind);
167 print_expr(cx, arg, indent + 1);
170 hir::ExprKind::MethodCall(ref path, _, args, _) => {
171 println!("{}MethodCall", ind);
172 println!("{}method name: {}", ind, path.ident.name);
174 print_expr(cx, arg, indent + 1);
177 hir::ExprKind::Tup(v) => {
178 println!("{}Tup", ind);
180 print_expr(cx, e, indent + 1);
183 hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
184 println!("{}Binary", ind);
185 println!("{}op: {:?}", ind, op.node);
186 println!("{}lhs:", ind);
187 print_expr(cx, lhs, indent + 1);
188 println!("{}rhs:", ind);
189 print_expr(cx, rhs, indent + 1);
191 hir::ExprKind::Unary(op, ref inner) => {
192 println!("{}Unary", ind);
193 println!("{}op: {:?}", ind, op);
194 print_expr(cx, inner, indent + 1);
196 hir::ExprKind::Lit(ref lit) => {
197 println!("{}Lit", ind);
198 println!("{}{:?}", ind, lit);
200 hir::ExprKind::Cast(ref e, ref target) => {
201 println!("{}Cast", ind);
202 print_expr(cx, e, indent + 1);
203 println!("{}target type: {:?}", ind, target);
205 hir::ExprKind::Type(ref e, ref target) => {
206 println!("{}Type", ind);
207 print_expr(cx, e, indent + 1);
208 println!("{}target type: {:?}", ind, target);
210 hir::ExprKind::Loop(..) => {
211 println!("{}Loop", ind);
213 hir::ExprKind::Match(ref cond, _, ref source) => {
214 println!("{}Match", ind);
215 println!("{}condition:", ind);
216 print_expr(cx, cond, indent + 1);
217 println!("{}source: {:?}", ind, source);
219 hir::ExprKind::Closure(ref clause, _, _, _, _) => {
220 println!("{}Closure", ind);
221 println!("{}clause: {:?}", ind, clause);
223 hir::ExprKind::Yield(ref sub, _) => {
224 println!("{}Yield", ind);
225 print_expr(cx, sub, indent + 1);
227 hir::ExprKind::Block(_, _) => {
228 println!("{}Block", ind);
230 hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
231 println!("{}Assign", ind);
232 println!("{}lhs:", ind);
233 print_expr(cx, lhs, indent + 1);
234 println!("{}rhs:", ind);
235 print_expr(cx, rhs, indent + 1);
237 hir::ExprKind::AssignOp(ref binop, ref lhs, ref rhs) => {
238 println!("{}AssignOp", ind);
239 println!("{}op: {:?}", ind, binop.node);
240 println!("{}lhs:", ind);
241 print_expr(cx, lhs, indent + 1);
242 println!("{}rhs:", ind);
243 print_expr(cx, rhs, indent + 1);
245 hir::ExprKind::Field(ref e, ident) => {
246 println!("{}Field", ind);
247 println!("{}field name: {}", ind, ident.name);
248 println!("{}struct expr:", ind);
249 print_expr(cx, e, indent + 1);
251 hir::ExprKind::Index(ref arr, ref idx) => {
252 println!("{}Index", ind);
253 println!("{}array expr:", ind);
254 print_expr(cx, arr, indent + 1);
255 println!("{}index expr:", ind);
256 print_expr(cx, idx, indent + 1);
258 hir::ExprKind::Path(hir::QPath::Resolved(ref ty, ref path)) => {
259 println!("{}Resolved Path, {:?}", ind, ty);
260 println!("{}path: {:?}", ind, path);
262 hir::ExprKind::Path(hir::QPath::TypeRelative(ref ty, ref seg)) => {
263 println!("{}Relative Path, {:?}", ind, ty);
264 println!("{}seg: {:?}", ind, seg);
266 hir::ExprKind::AddrOf(kind, ref muta, ref e) => {
267 println!("{}AddrOf", ind);
268 println!("kind: {:?}", kind);
269 println!("mutability: {:?}", muta);
270 print_expr(cx, e, indent + 1);
272 hir::ExprKind::Break(_, ref e) => {
273 println!("{}Break", ind);
274 if let Some(ref e) = *e {
275 print_expr(cx, e, indent + 1);
278 hir::ExprKind::Continue(_) => println!("{}Again", ind),
279 hir::ExprKind::Ret(ref e) => {
280 println!("{}Ret", ind);
281 if let Some(ref e) = *e {
282 print_expr(cx, e, indent + 1);
285 hir::ExprKind::InlineAsm(ref asm) => {
286 println!("{}InlineAsm", ind);
287 println!("{}template: {}", ind, InlineAsmTemplatePiece::to_string(asm.template));
288 println!("{}options: {:?}", ind, asm.options);
289 println!("{}operands:", ind);
290 for op in asm.operands {
292 hir::InlineAsmOperand::In { expr, .. }
293 | hir::InlineAsmOperand::InOut { expr, .. }
294 | hir::InlineAsmOperand::Const { expr }
295 | hir::InlineAsmOperand::Sym { expr } => print_expr(cx, expr, indent + 1),
296 hir::InlineAsmOperand::Out { expr, .. } => {
297 if let Some(expr) = expr {
298 print_expr(cx, expr, indent + 1);
301 hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
302 print_expr(cx, in_expr, indent + 1);
303 if let Some(out_expr) = out_expr {
304 print_expr(cx, out_expr, indent + 1);
310 hir::ExprKind::LlvmInlineAsm(ref asm) => {
311 let inputs = &asm.inputs_exprs;
312 let outputs = &asm.outputs_exprs;
313 println!("{}LlvmInlineAsm", ind);
314 println!("{}inputs:", ind);
315 for e in inputs.iter() {
316 print_expr(cx, e, indent + 1);
318 println!("{}outputs:", ind);
319 for e in outputs.iter() {
320 print_expr(cx, e, indent + 1);
323 hir::ExprKind::Struct(ref path, fields, ref base) => {
324 println!("{}Struct", ind);
325 println!("{}path: {:?}", ind, path);
326 for field in fields {
327 println!("{}field \"{}\":", ind, field.ident.name);
328 print_expr(cx, &field.expr, indent + 1);
330 if let Some(ref base) = *base {
331 println!("{}base:", ind);
332 print_expr(cx, base, indent + 1);
335 hir::ExprKind::Repeat(ref val, ref anon_const) => {
336 println!("{}Repeat", ind);
337 println!("{}value:", ind);
338 print_expr(cx, val, indent + 1);
339 println!("{}repeat count:", ind);
340 print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
342 hir::ExprKind::Err => {
343 println!("{}Err", ind);
345 hir::ExprKind::DropTemps(ref e) => {
346 println!("{}DropTemps", ind);
347 print_expr(cx, e, indent + 1);
352 fn print_item(cx: &LateContext<'_, '_>, item: &hir::Item<'_>) {
353 let did = cx.tcx.hir().local_def_id(item.hir_id);
354 println!("item `{}`", item.ident.name);
355 match item.vis.node {
356 hir::VisibilityKind::Public => println!("public"),
357 hir::VisibilityKind::Crate(_) => println!("visible crate wide"),
358 hir::VisibilityKind::Restricted { ref path, .. } => println!(
359 "visible in module `{}`",
360 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(path, false))
362 hir::VisibilityKind::Inherited => println!("visibility inherited from outer item"),
365 hir::ItemKind::ExternCrate(ref _renamed_from) => {
366 let def_id = cx.tcx.hir().local_def_id(item.hir_id);
367 if let Some(crate_id) = cx.tcx.extern_mod_stmt_cnum(def_id) {
368 let source = cx.tcx.used_crate_source(crate_id);
369 if let Some(ref src) = source.dylib {
370 println!("extern crate dylib source: {:?}", src.0);
372 if let Some(ref src) = source.rlib {
373 println!("extern crate rlib source: {:?}", src.0);
376 println!("weird extern crate without a crate id");
379 hir::ItemKind::Use(ref path, ref kind) => println!("{:?}, {:?}", path, kind),
380 hir::ItemKind::Static(..) => println!("static item of type {:#?}", cx.tcx.type_of(did)),
381 hir::ItemKind::Const(..) => println!("const item of type {:#?}", cx.tcx.type_of(did)),
382 hir::ItemKind::Fn(..) => {
383 let item_ty = cx.tcx.type_of(did);
384 println!("function of type {:#?}", item_ty);
386 hir::ItemKind::Mod(..) => println!("module"),
387 hir::ItemKind::ForeignMod(ref fm) => println!("foreign module with abi: {}", fm.abi),
388 hir::ItemKind::GlobalAsm(ref asm) => println!("global asm: {:?}", asm),
389 hir::ItemKind::TyAlias(..) => {
390 println!("type alias for {:?}", cx.tcx.type_of(did));
392 hir::ItemKind::OpaqueTy(..) => {
393 println!("existential type with real type {:?}", cx.tcx.type_of(did));
395 hir::ItemKind::Enum(..) => {
396 println!("enum definition of type {:?}", cx.tcx.type_of(did));
398 hir::ItemKind::Struct(..) => {
399 println!("struct definition of type {:?}", cx.tcx.type_of(did));
401 hir::ItemKind::Union(..) => {
402 println!("union definition of type {:?}", cx.tcx.type_of(did));
404 hir::ItemKind::Trait(..) => {
405 println!("trait decl");
406 if cx.tcx.trait_is_auto(did.to_def_id()) {
407 println!("trait is auto");
409 println!("trait is not auto");
412 hir::ItemKind::TraitAlias(..) => {
413 println!("trait alias");
415 hir::ItemKind::Impl {
416 of_trait: Some(ref _trait_ref),
419 println!("trait impl");
421 hir::ItemKind::Impl { of_trait: None, .. } => {
427 #[allow(clippy::similar_names)]
428 #[allow(clippy::too_many_lines)]
429 fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat<'_>, indent: usize) {
430 let ind = " ".repeat(indent);
431 println!("{}+", ind);
433 hir::PatKind::Wild => println!("{}Wild", ind),
434 hir::PatKind::Binding(ref mode, .., ident, ref inner) => {
435 println!("{}Binding", ind);
436 println!("{}mode: {:?}", ind, mode);
437 println!("{}name: {}", ind, ident.name);
438 if let Some(ref inner) = *inner {
439 println!("{}inner:", ind);
440 print_pat(cx, inner, indent + 1);
443 hir::PatKind::Or(fields) => {
444 println!("{}Or", ind);
445 for field in fields {
446 print_pat(cx, field, indent + 1);
449 hir::PatKind::Struct(ref path, fields, ignore) => {
450 println!("{}Struct", ind);
454 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
456 println!("{}ignore leftover fields: {}", ind, ignore);
457 println!("{}fields:", ind);
458 for field in fields {
459 println!("{} field name: {}", ind, field.ident.name);
460 if field.is_shorthand {
461 println!("{} in shorthand notation", ind);
463 print_pat(cx, &field.pat, indent + 1);
466 hir::PatKind::TupleStruct(ref path, fields, opt_dots_position) => {
467 println!("{}TupleStruct", ind);
471 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
473 if let Some(dot_position) = opt_dots_position {
474 println!("{}dot position: {}", ind, dot_position);
476 for field in fields {
477 print_pat(cx, field, indent + 1);
480 hir::PatKind::Path(hir::QPath::Resolved(ref ty, ref path)) => {
481 println!("{}Resolved Path, {:?}", ind, ty);
482 println!("{}path: {:?}", ind, path);
484 hir::PatKind::Path(hir::QPath::TypeRelative(ref ty, ref seg)) => {
485 println!("{}Relative Path, {:?}", ind, ty);
486 println!("{}seg: {:?}", ind, seg);
488 hir::PatKind::Tuple(pats, opt_dots_position) => {
489 println!("{}Tuple", ind);
490 if let Some(dot_position) = opt_dots_position {
491 println!("{}dot position: {}", ind, dot_position);
494 print_pat(cx, field, indent + 1);
497 hir::PatKind::Box(ref inner) => {
498 println!("{}Box", ind);
499 print_pat(cx, inner, indent + 1);
501 hir::PatKind::Ref(ref inner, ref muta) => {
502 println!("{}Ref", ind);
503 println!("{}mutability: {:?}", ind, muta);
504 print_pat(cx, inner, indent + 1);
506 hir::PatKind::Lit(ref e) => {
507 println!("{}Lit", ind);
508 print_expr(cx, e, indent + 1);
510 hir::PatKind::Range(ref l, ref r, ref range_end) => {
511 println!("{}Range", ind);
512 if let Some(expr) = l {
513 print_expr(cx, expr, indent + 1);
515 if let Some(expr) = r {
516 print_expr(cx, expr, indent + 1);
519 hir::RangeEnd::Included => println!("{} end included", ind),
520 hir::RangeEnd::Excluded => println!("{} end excluded", ind),
523 hir::PatKind::Slice(first_pats, ref range, last_pats) => {
524 println!("{}Slice [a, b, ..i, y, z]", ind);
526 for pat in first_pats {
527 print_pat(cx, pat, indent + 1);
530 if let Some(ref pat) = *range {
531 print_pat(cx, pat, indent + 1);
534 for pat in last_pats {
535 print_pat(cx, pat, indent + 1);
541 fn print_guard(cx: &LateContext<'_, '_>, guard: &hir::Guard<'_>, indent: usize) {
542 let ind = " ".repeat(indent);
543 println!("{}+", ind);
545 hir::Guard::If(expr) => {
546 println!("{}If", ind);
547 print_expr(cx, expr, indent + 1);