]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/utils/author.rs
Document the author lint
[rust.git] / clippy_lints / src / utils / author.rs
index c824c8906a72bc0dcdcb5a00a61c361f94526e86..9d708d637c3c93db8f9b0db61f4169769ddb2a7e 100644 (file)
@@ -5,16 +5,16 @@
 
 use rustc::lint::*;
 use rustc::hir;
-use rustc::hir::{Expr, Expr_, QPath};
+use rustc::hir::{Expr, Expr_, QPath, Ty_};
 use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
-use syntax::ast::{self, Attribute, LitKind, NodeId, DUMMY_NODE_ID};
-use syntax::codemap::Span;
+use syntax::ast::{self, Attribute, LitKind, DUMMY_NODE_ID};
 use std::collections::HashMap;
 
 /// **What it does:** Generates clippy code that detects the offending pattern
 ///
 /// **Example:**
 /// ```rust
+/// // ./tests/ui/my_lint.rs
 /// fn foo() {
 ///     // detect the following pattern
 ///     #[clippy(author)]
 /// }
 /// ```
 ///
-/// prints
+/// Running `TESTNAME=ui/my_lint cargo test --test compile-test` will produce
+/// a `./tests/ui/new_lint.stdout` file with the generated code:
 ///
-/// ```
+/// ```rust
+/// // ./tests/ui/new_lint.stdout
 /// if_chain!{
 ///     if let Expr_::ExprIf(ref cond, ref then, None) = item.node,
 ///     if let Expr_::ExprBinary(BinOp::Eq, ref left, ref right) = cond.node,
@@ -39,9 +41,9 @@
 ///     }
 /// }
 /// ```
-declare_lint! {
+declare_clippy_lint! {
     pub LINT_AUTHOR,
-    Warn,
+    internal_warn,
     "helper for writing lints"
 }
 
@@ -171,6 +173,12 @@ fn next(&mut self, s: &'static str) -> String {
             },
         }
     }
+
+    fn print_qpath(&mut self, path: &QPath) {
+        print!("    if match_qpath({}, &[", self.current);
+        print_path(path, &mut true);
+        println!("]);");
+    }
 }
 
 struct PrintVisitor {
@@ -260,9 +268,17 @@ fn visit_expr(&mut self, expr: &Expr) {
                     },
                 }
             },
-            Expr_::ExprCast(ref expr, ref _ty) => {
+            Expr_::ExprCast(ref expr, ref ty) => {
                 let cast_pat = self.next("expr");
-                println!("Cast(ref {}, _) = {};", cast_pat, current);
+                let cast_ty = self.next("cast_ty");
+                let qp_label = self.next("qp");
+
+                println!("Cast(ref {}, ref {}) = {};", cast_pat, cast_ty, current);
+                if let Ty_::TyPath(ref qp) = ty.node {
+                    println!("    if let Ty_::TyPath(ref {}) = {}.node;", qp_label, cast_ty);
+                    self.current = qp_label;
+                    self.print_qpath(qp);
+                }
                 self.current = cast_pat;
                 self.visit_expr(expr);
             },
@@ -376,7 +392,7 @@ fn visit_expr(&mut self, expr: &Expr) {
                 let path_pat = self.next("path");
                 println!("Path(ref {}) = {};", path_pat, current);
                 self.current = path_pat;
-                self.visit_qpath(path, expr.id, expr.span);
+                self.print_qpath(path);
             },
             Expr_::ExprAddrOf(mutability, ref inner) => {
                 let inner_pat = self.next("inner");
@@ -431,7 +447,7 @@ fn visit_expr(&mut self, expr: &Expr) {
                     println!("Struct(ref {}, ref {}, None) = {};", path_pat, fields_pat, current);
                 }
                 self.current = path_pat;
-                self.visit_qpath(path, expr.id, expr.span);
+                self.print_qpath(path);
                 println!("    if {}.len() == {};", fields_pat, fields.len());
                 println!("    // unimplemented: field checks");
             },
@@ -446,11 +462,6 @@ fn visit_expr(&mut self, expr: &Expr) {
         }
     }
 
-    fn visit_qpath(&mut self, path: &QPath, _: NodeId, _: Span) {
-        print!("    if match_qpath({}, &[", self.current);
-        print_path(path, &mut true);
-        println!("]);");
-    }
     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
         NestedVisitorMap::None
     }