diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs index 6ea154efc107..5b88494ae7b7 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 000000000000..2e9cb1466d0b --- /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 000000000000..e69de29bb2d1 diff --git a/tests/ui/author/if.stdout b/tests/ui/author/if.stdout new file mode 100644 index 000000000000..bff6546a9314 --- /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 + } +}