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(), cx.tcx.hir().attrs(item.hir_id())) {
42 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
43 if !has_attr(cx.sess(), cx.tcx.hir().attrs(item.hir_id())) {
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(), cx.tcx.hir().attrs(expr.hir_id)) {
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(), cx.tcx.hir().attrs(arm.hir_id)) {
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(), cx.tcx.hir().attrs(stmt.hir_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::If(ref cond, _, ref else_opt) => {
217 println!("{}If", ind);
218 println!("{}condition:", ind);
219 print_expr(cx, cond, indent + 1);
220 if let Some(ref els) = *else_opt {
221 println!("{}else:", ind);
222 print_expr(cx, els, indent + 1);
225 hir::ExprKind::Match(ref cond, _, ref source) => {
226 println!("{}Match", ind);
227 println!("{}condition:", ind);
228 print_expr(cx, cond, indent + 1);
229 println!("{}source: {:?}", ind, source);
231 hir::ExprKind::Closure(ref clause, _, _, _, _) => {
232 println!("{}Closure", ind);
233 println!("{}clause: {:?}", ind, clause);
235 hir::ExprKind::Yield(ref sub, _) => {
236 println!("{}Yield", ind);
237 print_expr(cx, sub, indent + 1);
239 hir::ExprKind::Block(_, _) => {
240 println!("{}Block", ind);
242 hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
243 println!("{}Assign", ind);
244 println!("{}lhs:", ind);
245 print_expr(cx, lhs, indent + 1);
246 println!("{}rhs:", ind);
247 print_expr(cx, rhs, indent + 1);
249 hir::ExprKind::AssignOp(ref binop, ref lhs, ref rhs) => {
250 println!("{}AssignOp", ind);
251 println!("{}op: {:?}", ind, binop.node);
252 println!("{}lhs:", ind);
253 print_expr(cx, lhs, indent + 1);
254 println!("{}rhs:", ind);
255 print_expr(cx, rhs, indent + 1);
257 hir::ExprKind::Field(ref e, ident) => {
258 println!("{}Field", ind);
259 println!("{}field name: {}", ind, ident.name);
260 println!("{}struct expr:", ind);
261 print_expr(cx, e, indent + 1);
263 hir::ExprKind::Index(ref arr, ref idx) => {
264 println!("{}Index", ind);
265 println!("{}array expr:", ind);
266 print_expr(cx, arr, indent + 1);
267 println!("{}index expr:", ind);
268 print_expr(cx, idx, indent + 1);
270 hir::ExprKind::Path(hir::QPath::Resolved(ref ty, ref path)) => {
271 println!("{}Resolved Path, {:?}", ind, ty);
272 println!("{}path: {:?}", ind, path);
274 hir::ExprKind::Path(hir::QPath::TypeRelative(ref ty, ref seg)) => {
275 println!("{}Relative Path, {:?}", ind, ty);
276 println!("{}seg: {:?}", ind, seg);
278 hir::ExprKind::Path(hir::QPath::LangItem(lang_item, ..)) => {
279 println!("{}Lang Item Path, {:?}", ind, lang_item.name());
281 hir::ExprKind::AddrOf(kind, ref muta, ref e) => {
282 println!("{}AddrOf", ind);
283 println!("kind: {:?}", kind);
284 println!("mutability: {:?}", muta);
285 print_expr(cx, e, indent + 1);
287 hir::ExprKind::Break(_, ref e) => {
288 println!("{}Break", ind);
289 if let Some(ref e) = *e {
290 print_expr(cx, e, indent + 1);
293 hir::ExprKind::Continue(_) => println!("{}Again", ind),
294 hir::ExprKind::Ret(ref e) => {
295 println!("{}Ret", ind);
296 if let Some(ref e) = *e {
297 print_expr(cx, e, indent + 1);
300 hir::ExprKind::InlineAsm(ref asm) => {
301 println!("{}InlineAsm", ind);
302 println!("{}template: {}", ind, InlineAsmTemplatePiece::to_string(asm.template));
303 println!("{}options: {:?}", ind, asm.options);
304 println!("{}operands:", ind);
305 for (op, _op_sp) in asm.operands {
307 hir::InlineAsmOperand::In { expr, .. }
308 | hir::InlineAsmOperand::InOut { expr, .. }
309 | hir::InlineAsmOperand::Const { expr }
310 | hir::InlineAsmOperand::Sym { expr } => print_expr(cx, expr, indent + 1),
311 hir::InlineAsmOperand::Out { expr, .. } => {
312 if let Some(expr) = expr {
313 print_expr(cx, expr, indent + 1);
316 hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
317 print_expr(cx, in_expr, indent + 1);
318 if let Some(out_expr) = out_expr {
319 print_expr(cx, out_expr, indent + 1);
325 hir::ExprKind::LlvmInlineAsm(ref asm) => {
326 let inputs = &asm.inputs_exprs;
327 let outputs = &asm.outputs_exprs;
328 println!("{}LlvmInlineAsm", ind);
329 println!("{}inputs:", ind);
330 for e in inputs.iter() {
331 print_expr(cx, e, indent + 1);
333 println!("{}outputs:", ind);
334 for e in outputs.iter() {
335 print_expr(cx, e, indent + 1);
338 hir::ExprKind::Struct(ref path, fields, ref base) => {
339 println!("{}Struct", ind);
340 println!("{}path: {:?}", ind, path);
341 for field in fields {
342 println!("{}field \"{}\":", ind, field.ident.name);
343 print_expr(cx, &field.expr, indent + 1);
345 if let Some(ref base) = *base {
346 println!("{}base:", ind);
347 print_expr(cx, base, indent + 1);
350 hir::ExprKind::ConstBlock(ref anon_const) => {
351 println!("{}ConstBlock", ind);
352 println!("{}anon_const:", ind);
353 print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
355 hir::ExprKind::Repeat(ref val, ref anon_const) => {
356 println!("{}Repeat", ind);
357 println!("{}value:", ind);
358 print_expr(cx, val, indent + 1);
359 println!("{}repeat count:", ind);
360 print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
362 hir::ExprKind::Err => {
363 println!("{}Err", ind);
365 hir::ExprKind::DropTemps(ref e) => {
366 println!("{}DropTemps", ind);
367 print_expr(cx, e, indent + 1);
372 fn print_item(cx: &LateContext<'_>, item: &hir::Item<'_>) {
373 let did = item.def_id;
374 println!("item `{}`", item.ident.name);
375 match item.vis.node {
376 hir::VisibilityKind::Public => println!("public"),
377 hir::VisibilityKind::Crate(_) => println!("visible crate wide"),
378 hir::VisibilityKind::Restricted { ref path, .. } => println!(
379 "visible in module `{}`",
380 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(path, false))
382 hir::VisibilityKind::Inherited => println!("visibility inherited from outer item"),
385 hir::ItemKind::ExternCrate(ref _renamed_from) => {
386 if let Some(crate_id) = cx.tcx.extern_mod_stmt_cnum(did) {
387 let source = cx.tcx.used_crate_source(crate_id);
388 if let Some(ref src) = source.dylib {
389 println!("extern crate dylib source: {:?}", src.0);
391 if let Some(ref src) = source.rlib {
392 println!("extern crate rlib source: {:?}", src.0);
395 println!("weird extern crate without a crate id");
398 hir::ItemKind::Use(ref path, ref kind) => println!("{:?}, {:?}", path, kind),
399 hir::ItemKind::Static(..) => println!("static item of type {:#?}", cx.tcx.type_of(did)),
400 hir::ItemKind::Const(..) => println!("const item of type {:#?}", cx.tcx.type_of(did)),
401 hir::ItemKind::Fn(..) => {
402 let item_ty = cx.tcx.type_of(did);
403 println!("function of type {:#?}", item_ty);
405 hir::ItemKind::Mod(..) => println!("module"),
406 hir::ItemKind::ForeignMod { abi, .. } => println!("foreign module with abi: {}", abi),
407 hir::ItemKind::GlobalAsm(ref asm) => println!("global asm: {:?}", asm),
408 hir::ItemKind::TyAlias(..) => {
409 println!("type alias for {:?}", cx.tcx.type_of(did));
411 hir::ItemKind::OpaqueTy(..) => {
412 println!("existential type with real type {:?}", cx.tcx.type_of(did));
414 hir::ItemKind::Enum(..) => {
415 println!("enum definition of type {:?}", cx.tcx.type_of(did));
417 hir::ItemKind::Struct(..) => {
418 println!("struct definition of type {:?}", cx.tcx.type_of(did));
420 hir::ItemKind::Union(..) => {
421 println!("union definition of type {:?}", cx.tcx.type_of(did));
423 hir::ItemKind::Trait(..) => {
424 println!("trait decl");
425 if cx.tcx.trait_is_auto(did.to_def_id()) {
426 println!("trait is auto");
428 println!("trait is not auto");
431 hir::ItemKind::TraitAlias(..) => {
432 println!("trait alias");
434 hir::ItemKind::Impl(hir::Impl {
435 of_trait: Some(ref _trait_ref),
438 println!("trait impl");
440 hir::ItemKind::Impl(hir::Impl { of_trait: None, .. }) => {
446 #[allow(clippy::similar_names)]
447 #[allow(clippy::too_many_lines)]
448 fn print_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, indent: usize) {
449 let ind = " ".repeat(indent);
450 println!("{}+", ind);
452 hir::PatKind::Wild => println!("{}Wild", ind),
453 hir::PatKind::Binding(ref mode, .., ident, ref inner) => {
454 println!("{}Binding", ind);
455 println!("{}mode: {:?}", ind, mode);
456 println!("{}name: {}", ind, ident.name);
457 if let Some(ref inner) = *inner {
458 println!("{}inner:", ind);
459 print_pat(cx, inner, indent + 1);
462 hir::PatKind::Or(fields) => {
463 println!("{}Or", ind);
464 for field in fields {
465 print_pat(cx, field, indent + 1);
468 hir::PatKind::Struct(ref path, fields, ignore) => {
469 println!("{}Struct", ind);
473 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
475 println!("{}ignore leftover fields: {}", ind, ignore);
476 println!("{}fields:", ind);
477 for field in fields {
478 println!("{} field name: {}", ind, field.ident.name);
479 if field.is_shorthand {
480 println!("{} in shorthand notation", ind);
482 print_pat(cx, &field.pat, indent + 1);
485 hir::PatKind::TupleStruct(ref path, fields, opt_dots_position) => {
486 println!("{}TupleStruct", ind);
490 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
492 if let Some(dot_position) = opt_dots_position {
493 println!("{}dot position: {}", ind, dot_position);
495 for field in fields {
496 print_pat(cx, field, indent + 1);
499 hir::PatKind::Path(hir::QPath::Resolved(ref ty, ref path)) => {
500 println!("{}Resolved Path, {:?}", ind, ty);
501 println!("{}path: {:?}", ind, path);
503 hir::PatKind::Path(hir::QPath::TypeRelative(ref ty, ref seg)) => {
504 println!("{}Relative Path, {:?}", ind, ty);
505 println!("{}seg: {:?}", ind, seg);
507 hir::PatKind::Path(hir::QPath::LangItem(lang_item, ..)) => {
508 println!("{}Lang Item Path, {:?}", ind, lang_item.name());
510 hir::PatKind::Tuple(pats, opt_dots_position) => {
511 println!("{}Tuple", ind);
512 if let Some(dot_position) = opt_dots_position {
513 println!("{}dot position: {}", ind, dot_position);
516 print_pat(cx, field, indent + 1);
519 hir::PatKind::Box(ref inner) => {
520 println!("{}Box", ind);
521 print_pat(cx, inner, indent + 1);
523 hir::PatKind::Ref(ref inner, ref muta) => {
524 println!("{}Ref", ind);
525 println!("{}mutability: {:?}", ind, muta);
526 print_pat(cx, inner, indent + 1);
528 hir::PatKind::Lit(ref e) => {
529 println!("{}Lit", ind);
530 print_expr(cx, e, indent + 1);
532 hir::PatKind::Range(ref l, ref r, ref range_end) => {
533 println!("{}Range", ind);
534 if let Some(expr) = l {
535 print_expr(cx, expr, indent + 1);
537 if let Some(expr) = r {
538 print_expr(cx, expr, indent + 1);
541 hir::RangeEnd::Included => println!("{} end included", ind),
542 hir::RangeEnd::Excluded => println!("{} end excluded", ind),
545 hir::PatKind::Slice(first_pats, ref range, last_pats) => {
546 println!("{}Slice [a, b, ..i, y, z]", ind);
548 for pat in first_pats {
549 print_pat(cx, pat, indent + 1);
552 if let Some(ref pat) = *range {
553 print_pat(cx, pat, indent + 1);
556 for pat in last_pats {
557 print_pat(cx, pat, indent + 1);
563 fn print_guard(cx: &LateContext<'_>, guard: &hir::Guard<'_>, indent: usize) {
564 let ind = " ".repeat(indent);
565 println!("{}+", ind);
567 hir::Guard::If(expr) => {
568 println!("{}If", ind);
569 print_expr(cx, expr, indent + 1);
571 hir::Guard::IfLet(pat, expr) => {
572 println!("{}IfLet", ind);
573 print_pat(cx, pat, indent + 1);
574 print_expr(cx, expr, indent + 1);