Start handling desugarings in author lint

This commit is contained in:
Manish Goregaokar 2019-05-10 23:36:49 -07:00
parent 0499184201
commit 19cfb84405
4 changed files with 64 additions and 1 deletions

View File

@ -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 {

10
tests/ui/author/if.rs Normal file
View File

@ -0,0 +1,10 @@
#[allow(clippy::all)]
fn main() {
#[clippy::author]
let _ = if true {
1 == 1;
} else {
2 == 2;
};
}

View File

27
tests/ui/author/if.stdout Normal file
View File

@ -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
}
}