1 //! A group of attributes that can be attached to Rust code in order
2 //! to generate a clippy lint detecting said code automatically.
4 #![allow(print_stdout, use_debug)]
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;
14 /// **What it does:** Generates clippy code that detects the offending pattern
18 /// // ./tests/ui/my_lint.rs
20 /// // detect the following pattern
23 /// // but ignore everything from here on
24 /// #![clippy::author = "ignore"]
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:
33 /// // ./tests/ui/new_lint.stdout
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,
41 /// // report your lint here
45 declare_clippy_lint! {
48 "helper for writing lints"
53 impl LintPass for Pass {
54 fn get_lints(&self) -> LintArray {
55 lint_array!(LINT_AUTHOR)
60 println!("if_chain! {{");
65 println!(" // report your lint here");
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) {
76 PrintVisitor::new("item").visit_item(item);
80 fn check_impl_item(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ImplItem) {
81 if !has_attr(&item.attrs) {
85 PrintVisitor::new("item").visit_impl_item(item);
89 fn check_trait_item(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem) {
90 if !has_attr(&item.attrs) {
94 PrintVisitor::new("item").visit_trait_item(item);
98 fn check_variant(&mut self, _cx: &LateContext<'a, 'tcx>, var: &'tcx hir::Variant, generics: &hir::Generics) {
99 if !has_attr(&var.node.attrs) {
103 PrintVisitor::new("var").visit_variant(var, generics, DUMMY_NODE_ID);
107 fn check_struct_field(&mut self, _cx: &LateContext<'a, 'tcx>, field: &'tcx hir::StructField) {
108 if !has_attr(&field.attrs) {
112 PrintVisitor::new("field").visit_struct_field(field);
116 fn check_expr(&mut self, _cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
117 if !has_attr(&expr.attrs) {
121 PrintVisitor::new("expr").visit_expr(expr);
125 fn check_arm(&mut self, _cx: &LateContext<'a, 'tcx>, arm: &'tcx hir::Arm) {
126 if !has_attr(&arm.attrs) {
130 PrintVisitor::new("arm").visit_arm(arm);
134 fn check_stmt(&mut self, _cx: &LateContext<'a, 'tcx>, stmt: &'tcx hir::Stmt) {
135 if !has_attr(stmt.node.attrs()) {
139 PrintVisitor::new("stmt").visit_stmt(stmt);
143 fn check_foreign_item(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ForeignItem) {
144 if !has_attr(&item.attrs) {
148 PrintVisitor::new("item").visit_foreign_item(item);
154 fn new(s: &'static str) -> Self {
157 current: s.to_owned(),
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();
168 format!("{}{}", s, *val)
170 // not there: insert and return name as given
178 fn print_qpath(&mut self, path: &QPath) {
179 print!(" if match_qpath({}, &[", self.current);
180 print_path(path, &mut true);
185 struct PrintVisitor {
186 /// Fields are the current index that needs to be appended to pattern
188 ids: HashMap<&'static str, usize>,
189 /// the name that needs to be destructured
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);
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);
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);
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");
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");
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);
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);
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);
247 Expr_::ExprLit(ref lit) => {
248 let lit_pat = self.next("lit");
249 println!("Lit(ref {}) = {};", lit_pat, current);
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)
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);
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())
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");
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);
283 self.current = cast_pat;
284 self.visit_expr(expr);
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);
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_);
301 println!("If(ref {}, ref {}, None) = {};", cond_pat, then_pat, current);
303 self.current = cond_pat;
304 self.visit_expr(cond);
305 self.current = then_pat;
306 self.visit_expr(then);
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);
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);
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);
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);
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");
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);
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);
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);
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);
386 Expr_::ExprField(ref object, ref field_name) => {
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_name.node.as_str());
391 self.current = obj_pat;
392 self.visit_expr(object);
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);
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);
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);
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);
423 println!("Break(ref {}, None) = {};", destination_pat, current);
425 // FIXME: implement label printing
427 Expr_::ExprAgain(ref _destination) => {
428 let destination_pat = self.next("destination");
429 println!("Again(ref {}) = {};", destination_pat, current);
430 // FIXME: implement label printing
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);
438 println!("Ret(None) = {};", current);
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");
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");
450 "Struct(ref {}, ref {}, Some(ref {})) = {};",
456 self.current = base_pat;
457 self.visit_expr(base);
459 println!("Struct(ref {}, ref {}, None) = {};", path_pat, fields_pat, current);
461 self.current = path_pat;
462 self.print_qpath(path);
463 println!(" if {}.len() == {};", fields_pat, fields.len());
464 println!(" // unimplemented: field checks");
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);
477 fn visit_pat(&mut self, pat: &Pat) {
478 print!(" if let PatKind::");
479 let current = format!("{}.node", self.current);
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",
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;
496 println!("Binding({}, _, {}, None) = {};", anno_pat, name_pat, current);
498 println!(" if {}.node.as_str() == \"{}\";", name_pat, name.node.as_str());
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");
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");
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);
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");
530 PatKind::Box(ref pat) => {
531 let pat_pat = self.next("pat");
532 println!("Box(ref {}) = {};", pat_pat, current);
533 self.current = pat_pat;
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;
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);
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);
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);
566 println!("Slice(ref {}, None, ref {}) = {};", start_pat, end_pat, current);
568 println!(" if {}.len() == {};", start_pat, start.len());
569 for (i, pat) in start.iter().enumerate() {
570 self.current = format!("{}[{}]", start_pat, i);
573 println!(" if {}.len() == {};", end_pat, end.len());
574 for (i, pat) in end.iter().enumerate() {
575 self.current = format!("{}[{}]", end_pat, i);
582 fn visit_stmt(&mut self, s: &Stmt) {
583 print!(" if let Stmt_::");
584 let current = format!("{}.node", self.current);
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);
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);
603 self.current = format!("{}.pat", local_pat);
604 self.visit_pat(&local.pat);
607 Decl_::DeclItem(_) => {
608 println!("DeclItem(item_id) = {};", current);
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;
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;
631 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
632 NestedVisitorMap::None
636 fn has_attr(attrs: &[Attribute]) -> bool {
637 get_attr(attrs, "author").count() > 0
640 fn desugaring_name(des: hir::MatchSource) -> String {
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),
650 fn loop_desugaring_name(des: hir::LoopSource) -> &'static str {
652 hir::LoopSource::ForLoop => "LoopSource::ForLoop",
653 hir::LoopSource::Loop => "LoopSource::Loop",
654 hir::LoopSource::WhileLet => "LoopSource::WhileLet",
658 fn print_path(path: &QPath, first: &mut bool) {
660 QPath::Resolved(_, ref path) => for segment in &path.segments {
666 print!("{:?}", segment.name.as_str());
668 QPath::TypeRelative(ref ty, ref segment) => match ty.node {
669 hir::Ty_::TyPath(ref inner_path) => {
670 print_path(inner_path, first);
676 print!("{:?}", segment.name.as_str());
678 ref other => print!("/* unimplemented: {:?}*/", other),