Skip to content

Commit 19cfb84

Browse files
committed
Start handling desugarings in author lint
1 parent 0499184 commit 19cfb84

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

clippy_lints/src/utils/author.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A group of attributes that can be attached to Rust code in order
22
//! to generate a clippy lint detecting said code automatically.
33
4-
use crate::utils::get_attr;
4+
use crate::utils::{get_attr, higher};
55
use rustc::hir;
66
use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
77
use rustc::hir::{BindingAnnotation, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind};
@@ -187,6 +187,32 @@ struct PrintVisitor {
187187
impl<'tcx> Visitor<'tcx> for PrintVisitor {
188188
#[allow(clippy::too_many_lines)]
189189
fn visit_expr(&mut self, expr: &Expr) {
190+
// handle if desugarings
191+
// TODO add more desugarings here
192+
if let Some((cond, then, opt_else)) = higher::if_block(&expr) {
193+
let cond_pat = self.next("cond");
194+
let then_pat = self.next("then");
195+
if let Some(else_) = opt_else {
196+
let else_pat = self.next("else_");
197+
println!(
198+
" if let Some((ref {}, ref {}, Some({}))) = higher::if_block(&{});",
199+
cond_pat, then_pat, else_pat, self.current
200+
);
201+
self.current = else_pat;
202+
self.visit_expr(else_);
203+
} else {
204+
println!(
205+
" if let Some((ref {}, ref {}, None)) = higher::if_block(&{});",
206+
cond_pat, then_pat, self.current
207+
);
208+
}
209+
self.current = cond_pat;
210+
self.visit_expr(cond);
211+
self.current = then_pat;
212+
self.visit_expr(then);
213+
return;
214+
}
215+
190216
print!(" if let ExprKind::");
191217
let current = format!("{}.node", self.current);
192218
match expr.node {

tests/ui/author/if.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[allow(clippy::all)]
2+
3+
fn main() {
4+
#[clippy::author]
5+
let _ = if true {
6+
1 == 1;
7+
} else {
8+
2 == 2;
9+
};
10+
}

tests/ui/author/if.stderr

Whitespace-only changes.

tests/ui/author/if.stdout

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
if_chain! {
2+
if let StmtKind::Local(ref local) = stmt.node;
3+
if let Some(ref init) = local.init;
4+
if let Some((ref cond, ref then, Some(else_))) = higher::if_block(&init);
5+
if let ExprKind::Block(ref block) = else_.node;
6+
if let StmtKind::Semi(ref e, _) = block.node
7+
if let ExprKind::Binary(ref op, ref left, ref right) = e.node;
8+
if BinOpKind::Eq == op.node;
9+
if let ExprKind::Lit(ref lit) = left.node;
10+
if let LitKind::Int(2, _) = lit.node;
11+
if let ExprKind::Lit(ref lit1) = right.node;
12+
if let LitKind::Int(2, _) = lit1.node;
13+
if let ExprKind::Lit(ref lit2) = cond.node;
14+
if let LitKind::Bool(true) = lit2.node;
15+
if let ExprKind::Block(ref block1) = then.node;
16+
if let StmtKind::Semi(ref e1, _) = block1.node
17+
if let ExprKind::Binary(ref op1, ref left1, ref right1) = e1.node;
18+
if BinOpKind::Eq == op1.node;
19+
if let ExprKind::Lit(ref lit3) = left1.node;
20+
if let LitKind::Int(1, _) = lit3.node;
21+
if let ExprKind::Lit(ref lit4) = right1.node;
22+
if let LitKind::Int(1, _) = lit4.node;
23+
if let PatKind::Wild = local.pat.node;
24+
then {
25+
// report your lint here
26+
}
27+
}

0 commit comments

Comments
 (0)