about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2019-05-10 23:36:49 -0700
committerManish Goregaokar <manishsmail@gmail.com>2019-05-10 23:43:58 -0700
commit19cfb84405d4891ae4baf5cc6b9cec3c9d29619d (patch)
treea618fe591066924cdc16ae2483a4b15b59a45bf0
parent049918420150fb42a7b774c1f4680ee7ab7f7627 (diff)
downloadrust-19cfb84405d4891ae4baf5cc6b9cec3c9d29619d.tar.gz
rust-19cfb84405d4891ae4baf5cc6b9cec3c9d29619d.zip
Start handling desugarings in author lint
-rw-r--r--clippy_lints/src/utils/author.rs28
-rw-r--r--tests/ui/author/if.rs10
-rw-r--r--tests/ui/author/if.stderr0
-rw-r--r--tests/ui/author/if.stdout27
4 files changed, 64 insertions, 1 deletions
diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs
index 6ea154efc10..5b88494ae7b 100644
--- a/clippy_lints/src/utils/author.rs
+++ b/clippy_lints/src/utils/author.rs
@@ -1,7 +1,7 @@
 //! A group of attributes that can be attached to Rust code in order
 //! to generate a clippy lint detecting said code automatically.
 
-use crate::utils::get_attr;
+use crate::utils::{get_attr, higher};
 use rustc::hir;
 use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
 use rustc::hir::{BindingAnnotation, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind};
@@ -187,6 +187,32 @@ struct PrintVisitor {
 impl<'tcx> Visitor<'tcx> for PrintVisitor {
     #[allow(clippy::too_many_lines)]
     fn visit_expr(&mut self, expr: &Expr) {
+        // handle if desugarings
+        // TODO add more desugarings here
+        if let Some((cond, then, opt_else)) = higher::if_block(&expr) {
+            let cond_pat = self.next("cond");
+            let then_pat = self.next("then");
+            if let Some(else_) = opt_else {
+                let else_pat = self.next("else_");
+                println!(
+                    "    if let Some((ref {}, ref {}, Some({}))) = higher::if_block(&{});",
+                    cond_pat, then_pat, else_pat, self.current
+                );
+                self.current = else_pat;
+                self.visit_expr(else_);
+            } else {
+                println!(
+                    "    if let Some((ref {}, ref {}, None)) = higher::if_block(&{});",
+                    cond_pat, then_pat, self.current
+                );
+            }
+            self.current = cond_pat;
+            self.visit_expr(cond);
+            self.current = then_pat;
+            self.visit_expr(then);
+            return;
+        }
+
         print!("    if let ExprKind::");
         let current = format!("{}.node", self.current);
         match expr.node {
diff --git a/tests/ui/author/if.rs b/tests/ui/author/if.rs
new file mode 100644
index 00000000000..2e9cb1466d0
--- /dev/null
+++ b/tests/ui/author/if.rs
@@ -0,0 +1,10 @@
+#[allow(clippy::all)]
+
+fn main() {
+    #[clippy::author]
+    let _ = if true {
+        1 == 1;
+    } else {
+        2 == 2;
+    };
+}
diff --git a/tests/ui/author/if.stderr b/tests/ui/author/if.stderr
new file mode 100644
index 00000000000..e69de29bb2d
--- /dev/null
+++ b/tests/ui/author/if.stderr
diff --git a/tests/ui/author/if.stdout b/tests/ui/author/if.stdout
new file mode 100644
index 00000000000..bff6546a931
--- /dev/null
+++ b/tests/ui/author/if.stdout
@@ -0,0 +1,27 @@
+if_chain! {
+    if let StmtKind::Local(ref local) = stmt.node;
+    if let Some(ref init) = local.init;
+    if let Some((ref cond, ref then, Some(else_))) = higher::if_block(&init);
+    if let ExprKind::Block(ref block) = else_.node;
+    if let StmtKind::Semi(ref e, _) = block.node
+    if let ExprKind::Binary(ref op, ref left, ref right) = e.node;
+    if BinOpKind::Eq == op.node;
+    if let ExprKind::Lit(ref lit) = left.node;
+    if let LitKind::Int(2, _) = lit.node;
+    if let ExprKind::Lit(ref lit1) = right.node;
+    if let LitKind::Int(2, _) = lit1.node;
+    if let ExprKind::Lit(ref lit2) = cond.node;
+    if let LitKind::Bool(true) = lit2.node;
+    if let ExprKind::Block(ref block1) = then.node;
+    if let StmtKind::Semi(ref e1, _) = block1.node
+    if let ExprKind::Binary(ref op1, ref left1, ref right1) = e1.node;
+    if BinOpKind::Eq == op1.node;
+    if let ExprKind::Lit(ref lit3) = left1.node;
+    if let LitKind::Int(1, _) = lit3.node;
+    if let ExprKind::Lit(ref lit4) = right1.node;
+    if let LitKind::Int(1, _) = lit4.node;
+    if let PatKind::Wild = local.pat.node;
+    then {
+        // report your lint here
+    }
+}