]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/author.rs
Merge pull request #2821 from mati865/rust-2018-migration
[rust.git] / clippy_lints / src / utils / author.rs
1 //! A group of attributes that can be attached to Rust code in order
2 //! to generate a clippy lint detecting said code automatically.
3
4 #![allow(print_stdout, use_debug)]
5
6 use rustc::lint::*;
7 use rustc::hir;
8 use rustc::hir::{Expr, Expr_, QPath, Ty_, Pat, PatKind, BindingAnnotation, StmtSemi, StmtExpr, StmtDecl, Decl_, Stmt};
9 use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
10 use syntax::ast::{Attribute, LitKind, DUMMY_NODE_ID};
11 use std::collections::HashMap;
12 use crate::utils::get_attr;
13
14 /// **What it does:** Generates clippy code that detects the offending pattern
15 ///
16 /// **Example:**
17 /// ```rust
18 /// // ./tests/ui/my_lint.rs
19 /// fn foo() {
20 ///     // detect the following pattern
21 ///     #[clippy::author]
22 ///     if x == 42 {
23 ///         // but ignore everything from here on
24 ///         #![clippy::author = "ignore"]
25 ///     }
26 /// }
27 /// ```
28 ///
29 /// Running `TESTNAME=ui/my_lint cargo test --test compile-test` will produce
30 /// a `./tests/ui/new_lint.stdout` file with the generated code:
31 ///
32 /// ```rust
33 /// // ./tests/ui/new_lint.stdout
34 /// if_chain!{
35 ///     if let Expr_::ExprIf(ref cond, ref then, None) = item.node,
36 ///     if let Expr_::ExprBinary(BinOp::Eq, ref left, ref right) = cond.node,
37 ///     if let Expr_::ExprPath(ref path) = left.node,
38 ///     if let Expr_::ExprLit(ref lit) = right.node,
39 ///     if let LitKind::Int(42, _) = lit.node,
40 ///     then {
41 ///         // report your lint here
42 ///     }
43 /// }
44 /// ```
45 declare_clippy_lint! {
46     pub LINT_AUTHOR,
47     internal_warn,
48     "helper for writing lints"
49 }
50
51 pub struct Pass;
52
53 impl LintPass for Pass {
54     fn get_lints(&self) -> LintArray {
55         lint_array!(LINT_AUTHOR)
56     }
57 }
58
59 fn prelude() {
60     println!("if_chain! {{");
61 }
62
63 fn done() {
64     println!("    then {{");
65     println!("        // report your lint here");
66     println!("    }}");
67     println!("}}");
68 }
69
70 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
71     fn check_item(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item) {
72         if !has_attr(&item.attrs) {
73             return;
74         }
75         prelude();
76         PrintVisitor::new("item").visit_item(item);
77         done();
78     }
79
80     fn check_impl_item(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ImplItem) {
81         if !has_attr(&item.attrs) {
82             return;
83         }
84         prelude();
85         PrintVisitor::new("item").visit_impl_item(item);
86         done();
87     }
88
89     fn check_trait_item(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem) {
90         if !has_attr(&item.attrs) {
91             return;
92         }
93         prelude();
94         PrintVisitor::new("item").visit_trait_item(item);
95         done();
96     }
97
98     fn check_variant(&mut self, _cx: &LateContext<'a, 'tcx>, var: &'tcx hir::Variant, generics: &hir::Generics) {
99         if !has_attr(&var.node.attrs) {
100             return;
101         }
102         prelude();
103         PrintVisitor::new("var").visit_variant(var, generics, DUMMY_NODE_ID);
104         done();
105     }
106
107     fn check_struct_field(&mut self, _cx: &LateContext<'a, 'tcx>, field: &'tcx hir::StructField) {
108         if !has_attr(&field.attrs) {
109             return;
110         }
111         prelude();
112         PrintVisitor::new("field").visit_struct_field(field);
113         done();
114     }
115
116     fn check_expr(&mut self, _cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
117         if !has_attr(&expr.attrs) {
118             return;
119         }
120         prelude();
121         PrintVisitor::new("expr").visit_expr(expr);
122         done();
123     }
124
125     fn check_arm(&mut self, _cx: &LateContext<'a, 'tcx>, arm: &'tcx hir::Arm) {
126         if !has_attr(&arm.attrs) {
127             return;
128         }
129         prelude();
130         PrintVisitor::new("arm").visit_arm(arm);
131         done();
132     }
133
134     fn check_stmt(&mut self, _cx: &LateContext<'a, 'tcx>, stmt: &'tcx hir::Stmt) {
135         if !has_attr(stmt.node.attrs()) {
136             return;
137         }
138         prelude();
139         PrintVisitor::new("stmt").visit_stmt(stmt);
140         done();
141     }
142
143     fn check_foreign_item(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ForeignItem) {
144         if !has_attr(&item.attrs) {
145             return;
146         }
147         prelude();
148         PrintVisitor::new("item").visit_foreign_item(item);
149         done();
150     }
151 }
152
153 impl PrintVisitor {
154     fn new(s: &'static str) -> Self {
155         Self {
156             ids: HashMap::new(),
157             current: s.to_owned(),
158         }
159     }
160
161     fn next(&mut self, s: &'static str) -> String {
162         use std::collections::hash_map::Entry::*;
163         match self.ids.entry(s) {
164             // already there: start numbering from `1`
165             Occupied(mut occ) => {
166                 let val = occ.get_mut();
167                 *val += 1;
168                 format!("{}{}", s, *val)
169             },
170             // not there: insert and return name as given
171             Vacant(vac) => {
172                 vac.insert(0);
173                 s.to_owned()
174             },
175         }
176     }
177
178     fn print_qpath(&mut self, path: &QPath) {
179         print!("    if match_qpath({}, &[", self.current);
180         print_path(path, &mut true);
181         println!("]);");
182     }
183 }
184
185 struct PrintVisitor {
186     /// Fields are the current index that needs to be appended to pattern
187     /// binding names
188     ids: HashMap<&'static str, usize>,
189     /// the name that needs to be destructured
190     current: String,
191 }
192
193 impl<'tcx> Visitor<'tcx> for PrintVisitor {
194     fn visit_expr(&mut self, expr: &Expr) {
195         print!("    if let Expr_::Expr");
196         let current = format!("{}.node", self.current);
197         match expr.node {
198             Expr_::ExprBox(ref inner) => {
199                 let inner_pat = self.next("inner");
200                 println!("Box(ref {}) = {};", inner_pat, current);
201                 self.current = inner_pat;
202                 self.visit_expr(inner);
203             },
204             Expr_::ExprArray(ref elements) => {
205                 let elements_pat = self.next("elements");
206                 println!("Array(ref {}) = {};", elements_pat, current);
207                 println!("    if {}.len() == {};", elements_pat, elements.len());
208                 for (i, element) in elements.iter().enumerate() {
209                     self.current = format!("{}[{}]", elements_pat, i);
210                     self.visit_expr(element);
211                 }
212             },
213             Expr_::ExprCall(ref _func, ref _args) => {
214                 println!("Call(ref func, ref args) = {};", current);
215                 println!("    // unimplemented: `ExprCall` is not further destructured at the moment");
216             },
217             Expr_::ExprMethodCall(ref _method_name, ref _generics, ref _args) => {
218                 println!("MethodCall(ref method_name, ref generics, ref args) = {};", current);
219                 println!("    // unimplemented: `ExprMethodCall` is not further destructured at the moment");
220             },
221             Expr_::ExprTup(ref elements) => {
222                 let elements_pat = self.next("elements");
223                 println!("Tup(ref {}) = {};", elements_pat, current);
224                 println!("    if {}.len() == {};", elements_pat, elements.len());
225                 for (i, element) in elements.iter().enumerate() {
226                     self.current = format!("{}[{}]", elements_pat, i);
227                     self.visit_expr(element);
228                 }
229             },
230             Expr_::ExprBinary(ref op, ref left, ref right) => {
231                 let op_pat = self.next("op");
232                 let left_pat = self.next("left");
233                 let right_pat = self.next("right");
234                 println!("Binary(ref {}, ref {}, ref {}) = {};", op_pat, left_pat, right_pat, current);
235                 println!("    if BinOp_::{:?} == {}.node;", op.node, op_pat);
236                 self.current = left_pat;
237                 self.visit_expr(left);
238                 self.current = right_pat;
239                 self.visit_expr(right);
240             },
241             Expr_::ExprUnary(ref op, ref inner) => {
242                 let inner_pat = self.next("inner");
243                 println!("Unary(UnOp::{:?}, ref {}) = {};", op, inner_pat, current);
244                 self.current = inner_pat;
245                 self.visit_expr(inner);
246             },
247             Expr_::ExprLit(ref lit) => {
248                 let lit_pat = self.next("lit");
249                 println!("Lit(ref {}) = {};", lit_pat, current);
250                 match lit.node {
251                     LitKind::Bool(val) => println!("    if let LitKind::Bool({:?}) = {}.node;", val, lit_pat),
252                     LitKind::Char(c) => println!("    if let LitKind::Char({:?}) = {}.node;", c, lit_pat),
253                     LitKind::Byte(b) => println!("    if let LitKind::Byte({}) = {}.node;", b, lit_pat),
254                     // FIXME: also check int type
255                     LitKind::Int(i, _) => println!("    if let LitKind::Int({}, _) = {}.node;", i, lit_pat),
256                     LitKind::Float(..) => println!("    if let LitKind::Float(..) = {}.node;", lit_pat),
257                     LitKind::FloatUnsuffixed(_) => {
258                         println!("    if let LitKind::FloatUnsuffixed(_) = {}.node;", lit_pat)
259                     },
260                     LitKind::ByteStr(ref vec) => {
261                         let vec_pat = self.next("vec");
262                         println!("    if let LitKind::ByteStr(ref {}) = {}.node;", vec_pat, lit_pat);
263                         println!("    if let [{:?}] = **{};", vec, vec_pat);
264                     },
265                     LitKind::Str(ref text, _) => {
266                         let str_pat = self.next("s");
267                         println!("    if let LitKind::Str(ref {}) = {}.node;", str_pat, lit_pat);
268                         println!("    if {}.as_str() == {:?}", str_pat, &*text.as_str())
269                     },
270                 }
271             },
272             Expr_::ExprCast(ref expr, ref ty) => {
273                 let cast_pat = self.next("expr");
274                 let cast_ty = self.next("cast_ty");
275                 let qp_label = self.next("qp");
276
277                 println!("Cast(ref {}, ref {}) = {};", cast_pat, cast_ty, current);
278                 if let Ty_::TyPath(ref qp) = ty.node {
279                     println!("    if let Ty_::TyPath(ref {}) = {}.node;", qp_label, cast_ty);
280                     self.current = qp_label;
281                     self.print_qpath(qp);
282                 }
283                 self.current = cast_pat;
284                 self.visit_expr(expr);
285             },
286             Expr_::ExprType(ref expr, ref _ty) => {
287                 let cast_pat = self.next("expr");
288                 println!("Type(ref {}, _) = {};", cast_pat, current);
289                 self.current = cast_pat;
290                 self.visit_expr(expr);
291             },
292             Expr_::ExprIf(ref cond, ref then, ref opt_else) => {
293                 let cond_pat = self.next("cond");
294                 let then_pat = self.next("then");
295                 if let Some(ref else_) = *opt_else {
296                     let else_pat = self.next("else_");
297                     println!("If(ref {}, ref {}, Some(ref {})) = {};", cond_pat, then_pat, else_pat, current);
298                     self.current = else_pat;
299                     self.visit_expr(else_);
300                 } else {
301                     println!("If(ref {}, ref {}, None) = {};", cond_pat, then_pat, current);
302                 }
303                 self.current = cond_pat;
304                 self.visit_expr(cond);
305                 self.current = then_pat;
306                 self.visit_expr(then);
307             },
308             Expr_::ExprWhile(ref cond, ref body, _) => {
309                 let cond_pat = self.next("cond");
310                 let body_pat = self.next("body");
311                 let label_pat = self.next("label");
312                 println!("While(ref {}, ref {}, ref {}) = {};", cond_pat, body_pat, label_pat, current);
313                 self.current = cond_pat;
314                 self.visit_expr(cond);
315                 self.current = body_pat;
316                 self.visit_block(body);
317             },
318             Expr_::ExprLoop(ref body, _, desugaring) => {
319                 let body_pat = self.next("body");
320                 let des = loop_desugaring_name(desugaring);
321                 let label_pat = self.next("label");
322                 println!("Loop(ref {}, ref {}, {}) = {};", body_pat, label_pat, des, current);
323                 self.current = body_pat;
324                 self.visit_block(body);
325             },
326             Expr_::ExprMatch(ref expr, ref arms, desugaring) => {
327                 let des = desugaring_name(desugaring);
328                 let expr_pat = self.next("expr");
329                 let arms_pat = self.next("arms");
330                 println!("Match(ref {}, ref {}, {}) = {};", expr_pat, arms_pat, des, current);
331                 self.current = expr_pat;
332                 self.visit_expr(expr);
333                 println!("    if {}.len() == {};", arms_pat, arms.len());
334                 for (i, arm) in arms.iter().enumerate() {
335                     self.current = format!("{}[{}].body", arms_pat, i);
336                     self.visit_expr(&arm.body);
337                     if let Some(ref guard) = arm.guard {
338                         let guard_pat = self.next("guard");
339                         println!("    if let Some(ref {}) = {}[{}].guard", guard_pat, arms_pat, i);
340                         self.current = guard_pat;
341                         self.visit_expr(guard);
342                     }
343                     println!("    if {}[{}].pats.len() == {};", arms_pat, i, arm.pats.len());
344                     for (j, pat) in arm.pats.iter().enumerate() {
345                         self.current = format!("{}[{}].pats[{}]", arms_pat, i, j);
346                         self.visit_pat(pat);
347                     }
348                 }
349             },
350             Expr_::ExprClosure(ref _capture_clause, ref _func, _, _, _) => {
351                 println!("Closure(ref capture_clause, ref func, _, _, _) = {};", current);
352                 println!("    // unimplemented: `ExprClosure` is not further destructured at the moment");
353             },
354             Expr_::ExprYield(ref sub) => {
355                 let sub_pat = self.next("sub");
356                 println!("Yield(ref sub) = {};", current);
357                 self.current = sub_pat;
358                 self.visit_expr(sub);
359             },
360             Expr_::ExprBlock(ref block, _) => {
361                 let block_pat = self.next("block");
362                 println!("Block(ref {}) = {};", block_pat, current);
363                 self.current = block_pat;
364                 self.visit_block(block);
365             },
366             Expr_::ExprAssign(ref target, ref value) => {
367                 let target_pat = self.next("target");
368                 let value_pat = self.next("value");
369                 println!("Assign(ref {}, ref {}) = {};", target_pat, value_pat, current);
370                 self.current = target_pat;
371                 self.visit_expr(target);
372                 self.current = value_pat;
373                 self.visit_expr(value);
374             },
375             Expr_::ExprAssignOp(ref op, ref target, ref value) => {
376                 let op_pat = self.next("op");
377                 let target_pat = self.next("target");
378                 let value_pat = self.next("value");
379                 println!("AssignOp(ref {}, ref {}, ref {}) = {};", op_pat, target_pat, value_pat, current);
380                 println!("    if BinOp_::{:?} == {}.node;", op.node, op_pat);
381                 self.current = target_pat;
382                 self.visit_expr(target);
383                 self.current = value_pat;
384                 self.visit_expr(value);
385             },
386             Expr_::ExprField(ref object, ref field_ident) => {
387                 let obj_pat = self.next("object");
388                 let field_name_pat = self.next("field_name");
389                 println!("Field(ref {}, ref {}) = {};", obj_pat, field_name_pat, current);
390                 println!("    if {}.node.as_str() == {:?}", field_name_pat, field_ident.name.as_str());
391                 self.current = obj_pat;
392                 self.visit_expr(object);
393             },
394             Expr_::ExprIndex(ref object, ref index) => {
395                 let object_pat = self.next("object");
396                 let index_pat = self.next("index");
397                 println!("Index(ref {}, ref {}) = {};", object_pat, index_pat, current);
398                 self.current = object_pat;
399                 self.visit_expr(object);
400                 self.current = index_pat;
401                 self.visit_expr(index);
402             },
403             Expr_::ExprPath(ref path) => {
404                 let path_pat = self.next("path");
405                 println!("Path(ref {}) = {};", path_pat, current);
406                 self.current = path_pat;
407                 self.print_qpath(path);
408             },
409             Expr_::ExprAddrOf(mutability, ref inner) => {
410                 let inner_pat = self.next("inner");
411                 println!("AddrOf({:?}, ref {}) = {};", mutability, inner_pat, current);
412                 self.current = inner_pat;
413                 self.visit_expr(inner);
414             },
415             Expr_::ExprBreak(ref _destination, ref opt_value) => {
416                 let destination_pat = self.next("destination");
417                 if let Some(ref value) = *opt_value {
418                     let value_pat = self.next("value");
419                     println!("Break(ref {}, Some(ref {})) = {};", destination_pat, value_pat, current);
420                     self.current = value_pat;
421                     self.visit_expr(value);
422                 } else {
423                     println!("Break(ref {}, None) = {};", destination_pat, current);
424                 }
425                 // FIXME: implement label printing
426             },
427             Expr_::ExprAgain(ref _destination) => {
428                 let destination_pat = self.next("destination");
429                 println!("Again(ref {}) = {};", destination_pat, current);
430                 // FIXME: implement label printing
431             },
432             Expr_::ExprRet(ref opt_value) => if let Some(ref value) = *opt_value {
433                 let value_pat = self.next("value");
434                 println!("Ret(Some(ref {})) = {};", value_pat, current);
435                 self.current = value_pat;
436                 self.visit_expr(value);
437             } else {
438                 println!("Ret(None) = {};", current);
439             },
440             Expr_::ExprInlineAsm(_, ref _input, ref _output) => {
441                 println!("InlineAsm(_, ref input, ref output) = {};", current);
442                 println!("    // unimplemented: `ExprInlineAsm` is not further destructured at the moment");
443             },
444             Expr_::ExprStruct(ref path, ref fields, ref opt_base) => {
445                 let path_pat = self.next("path");
446                 let fields_pat = self.next("fields");
447                 if let Some(ref base) = *opt_base {
448                     let base_pat = self.next("base");
449                     println!(
450                         "Struct(ref {}, ref {}, Some(ref {})) = {};",
451                         path_pat,
452                         fields_pat,
453                         base_pat,
454                         current
455                     );
456                     self.current = base_pat;
457                     self.visit_expr(base);
458                 } else {
459                     println!("Struct(ref {}, ref {}, None) = {};", path_pat, fields_pat, current);
460                 }
461                 self.current = path_pat;
462                 self.print_qpath(path);
463                 println!("    if {}.len() == {};", fields_pat, fields.len());
464                 println!("    // unimplemented: field checks");
465             },
466             // FIXME: compute length (needs type info)
467             Expr_::ExprRepeat(ref value, _) => {
468                 let value_pat = self.next("value");
469                 println!("Repeat(ref {}, _) = {};", value_pat, current);
470                 println!("// unimplemented: repeat count check");
471                 self.current = value_pat;
472                 self.visit_expr(value);
473             },
474         }
475     }
476
477     fn visit_pat(&mut self, pat: &Pat) {
478         print!("    if let PatKind::");
479         let current = format!("{}.node", self.current);
480         match pat.node {
481             PatKind::Wild => println!("Wild = {};", current),
482             PatKind::Binding(anno, _, name, ref sub) => {
483                 let anno_pat = match anno {
484                     BindingAnnotation::Unannotated => "BindingAnnotation::Unannotated",
485                     BindingAnnotation::Mutable => "BindingAnnotation::Mutable",
486                     BindingAnnotation::Ref => "BindingAnnotation::Ref",
487                     BindingAnnotation::RefMut => "BindingAnnotation::RefMut",
488                 };
489                 let name_pat = self.next("name");
490                 if let Some(ref sub) = *sub {
491                     let sub_pat = self.next("sub");
492                     println!("Binding({}, _, {}, Some(ref {})) = {};", anno_pat, name_pat, sub_pat, current);
493                     self.current = sub_pat;
494                     self.visit_pat(sub);
495                 } else {
496                     println!("Binding({}, _, {}, None) = {};", anno_pat, name_pat, current);
497                 }
498                 println!("    if {}.node.as_str() == \"{}\";", name_pat, name.node.as_str());
499             }
500             PatKind::Struct(ref path, ref fields, ignore) => {
501                 let path_pat = self.next("path");
502                 let fields_pat = self.next("fields");
503                 println!("Struct(ref {}, ref {}, {}) = {};", path_pat, fields_pat, ignore, current);
504                 self.current = path_pat;
505                 self.print_qpath(path);
506                 println!("    if {}.len() == {};", fields_pat, fields.len());
507                 println!("    // unimplemented: field checks");
508             }
509             PatKind::TupleStruct(ref path, ref fields, skip_pos) => {
510                 let path_pat = self.next("path");
511                 let fields_pat = self.next("fields");
512                 println!("TupleStruct(ref {}, ref {}, {:?}) = {};", path_pat, fields_pat, skip_pos, current);
513                 self.current = path_pat;
514                 self.print_qpath(path);
515                 println!("    if {}.len() == {};", fields_pat, fields.len());
516                 println!("    // unimplemented: field checks");
517             },
518             PatKind::Path(ref path) => {
519                 let path_pat = self.next("path");
520                 println!("Path(ref {}) = {};", path_pat, current);
521                 self.current = path_pat;
522                 self.print_qpath(path);
523             }
524             PatKind::Tuple(ref fields, skip_pos) => {
525                 let fields_pat = self.next("fields");
526                 println!("Tuple(ref {}, {:?}) = {};", fields_pat, skip_pos, current);
527                 println!("    if {}.len() == {};", fields_pat, fields.len());
528                 println!("    // unimplemented: field checks");
529             }
530             PatKind::Box(ref pat) => {
531                 let pat_pat = self.next("pat");
532                 println!("Box(ref {}) = {};", pat_pat, current);
533                 self.current = pat_pat;
534                 self.visit_pat(pat);
535             },
536             PatKind::Ref(ref pat, muta) => {
537                 let pat_pat = self.next("pat");
538                 println!("Ref(ref {}, Mutability::{:?}) = {};", pat_pat, muta, current);
539                 self.current = pat_pat;
540                 self.visit_pat(pat);
541             },
542             PatKind::Lit(ref lit_expr) => {
543                 let lit_expr_pat = self.next("lit_expr");
544                 println!("Lit(ref {}) = {}", lit_expr_pat, current);
545                 self.current = lit_expr_pat;
546                 self.visit_expr(lit_expr);
547             }
548             PatKind::Range(ref start, ref end, end_kind) => {
549                 let start_pat = self.next("start");
550                 let end_pat = self.next("end");
551                 println!("Range(ref {}, ref {}, RangeEnd::{:?}) = {};", start_pat, end_pat, end_kind, current);
552                 self.current = start_pat;
553                 self.visit_expr(start);
554                 self.current = end_pat;
555                 self.visit_expr(end);
556             }
557             PatKind::Slice(ref start, ref middle, ref end) => {
558                 let start_pat = self.next("start");
559                 let end_pat = self.next("end");
560                 if let Some(ref middle) = middle {
561                     let middle_pat = self.next("middle");
562                     println!("Slice(ref {}, Some(ref {}), ref {}) = {};", start_pat, middle_pat, end_pat, current);
563                     self.current = middle_pat;
564                     self.visit_pat(middle);
565                 } else {
566                     println!("Slice(ref {}, None, ref {}) = {};", start_pat, end_pat, current);
567                 }
568                 println!("    if {}.len() == {};", start_pat, start.len());
569                 for (i, pat) in start.iter().enumerate() {
570                     self.current = format!("{}[{}]", start_pat, i);
571                     self.visit_pat(pat);
572                 }
573                 println!("    if {}.len() == {};", end_pat, end.len());
574                 for (i, pat) in end.iter().enumerate() {
575                     self.current = format!("{}[{}]", end_pat, i);
576                     self.visit_pat(pat);
577                 }
578             }
579         }
580     }
581
582     fn visit_stmt(&mut self, s: &Stmt) {
583         print!("    if let Stmt_::");
584         let current = format!("{}.node", self.current);
585         match s.node {
586             // Could be an item or a local (let) binding:
587             StmtDecl(ref decl, _) => {
588                 let decl_pat = self.next("decl");
589                 println!("StmtDecl(ref {}, _) = {}", decl_pat, current);
590                 print!("    if let Decl_::");
591                 let current = format!("{}.node", decl_pat);
592                 match decl.node {
593                     // A local (let) binding:
594                     Decl_::DeclLocal(ref local) => {
595                         let local_pat = self.next("local");
596                         println!("DeclLocal(ref {}) = {};", local_pat, current);
597                         if let Some(ref init) = local.init {
598                             let init_pat = self.next("init");
599                             println!("    if let Some(ref {}) = {}.init", init_pat, local_pat);
600                             self.current = init_pat;
601                             self.visit_expr(init);
602                         }
603                         self.current = format!("{}.pat", local_pat);
604                         self.visit_pat(&local.pat);
605                     },
606                     // An item binding:
607                     Decl_::DeclItem(_) => {
608                         println!("DeclItem(item_id) = {};", current);
609                     },
610                 }
611             }
612
613             // Expr without trailing semi-colon (must have unit type):
614             StmtExpr(ref e, _) => {
615                 let e_pat = self.next("e");
616                 println!("StmtExpr(ref {}, _) = {}", e_pat, current);
617                 self.current = e_pat;
618                 self.visit_expr(e);
619             },
620
621             // Expr with trailing semi-colon (may have any type):
622             StmtSemi(ref e, _) => {
623                 let e_pat = self.next("e");
624                 println!("StmtSemi(ref {}, _) = {}", e_pat, current);
625                 self.current = e_pat;
626                 self.visit_expr(e);
627             },
628         }
629     }
630
631     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
632         NestedVisitorMap::None
633     }
634 }
635
636 fn has_attr(attrs: &[Attribute]) -> bool {
637     get_attr(attrs, "author").count() > 0
638 }
639
640 fn desugaring_name(des: hir::MatchSource) -> String {
641     match des {
642         hir::MatchSource::ForLoopDesugar => "MatchSource::ForLoopDesugar".to_string(),
643         hir::MatchSource::TryDesugar => "MatchSource::TryDesugar".to_string(),
644         hir::MatchSource::WhileLetDesugar => "MatchSource::WhileLetDesugar".to_string(),
645         hir::MatchSource::Normal => "MatchSource::Normal".to_string(),
646         hir::MatchSource::IfLetDesugar { contains_else_clause } => format!("MatchSource::IfLetDesugar {{ contains_else_clause: {} }}", contains_else_clause),
647     }
648 }
649
650 fn loop_desugaring_name(des: hir::LoopSource) -> &'static str {
651     match des {
652         hir::LoopSource::ForLoop => "LoopSource::ForLoop",
653         hir::LoopSource::Loop => "LoopSource::Loop",
654         hir::LoopSource::WhileLet => "LoopSource::WhileLet",
655     }
656 }
657
658 fn print_path(path: &QPath, first: &mut bool) {
659     match *path {
660         QPath::Resolved(_, ref path) => for segment in &path.segments {
661             if *first {
662                 *first = false;
663             } else {
664                 print!(", ");
665             }
666             print!("{:?}", segment.name.as_str());
667         },
668         QPath::TypeRelative(ref ty, ref segment) => match ty.node {
669             hir::Ty_::TyPath(ref inner_path) => {
670                 print_path(inner_path, first);
671                 if *first {
672                     *first = false;
673                 } else {
674                     print!(", ");
675                 }
676                 print!("{:?}", segment.name.as_str());
677             },
678             ref other => print!("/* unimplemented: {:?}*/", other),
679         },
680     }
681 }