|
1 | 1 | //! A group of attributes that can be attached to Rust code in order
|
2 | 2 | //! to generate a clippy lint detecting said code automatically.
|
3 | 3 |
|
4 |
| -use crate::utils::get_attr; |
| 4 | +use crate::utils::{get_attr, higher}; |
5 | 5 | use rustc::hir;
|
6 | 6 | use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
|
7 | 7 | use rustc::hir::{BindingAnnotation, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind};
|
@@ -187,6 +187,32 @@ struct PrintVisitor {
|
187 | 187 | impl<'tcx> Visitor<'tcx> for PrintVisitor {
|
188 | 188 | #[allow(clippy::too_many_lines)]
|
189 | 189 | 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 | + |
190 | 216 | print!(" if let ExprKind::");
|
191 | 217 | let current = format!("{}.node", self.current);
|
192 | 218 | match expr.node {
|
@@ -296,25 +322,6 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
|
296 | 322 | self.current = cast_pat;
|
297 | 323 | self.visit_expr(expr);
|
298 | 324 | },
|
299 |
| - ExprKind::If(ref cond, ref then, ref opt_else) => { |
300 |
| - let cond_pat = self.next("cond"); |
301 |
| - let then_pat = self.next("then"); |
302 |
| - if let Some(ref else_) = *opt_else { |
303 |
| - let else_pat = self.next("else_"); |
304 |
| - println!( |
305 |
| - "If(ref {}, ref {}, Some(ref {})) = {};", |
306 |
| - cond_pat, then_pat, else_pat, current |
307 |
| - ); |
308 |
| - self.current = else_pat; |
309 |
| - self.visit_expr(else_); |
310 |
| - } else { |
311 |
| - println!("If(ref {}, ref {}, None) = {};", cond_pat, then_pat, current); |
312 |
| - } |
313 |
| - self.current = cond_pat; |
314 |
| - self.visit_expr(cond); |
315 |
| - self.current = then_pat; |
316 |
| - self.visit_expr(then); |
317 |
| - }, |
318 | 325 | ExprKind::While(ref cond, ref body, _) => {
|
319 | 326 | let cond_pat = self.next("cond");
|
320 | 327 | let body_pat = self.next("body");
|
@@ -684,6 +691,10 @@ fn desugaring_name(des: hir::MatchSource) -> String {
|
684 | 691 | "MatchSource::IfLetDesugar {{ contains_else_clause: {} }}",
|
685 | 692 | contains_else_clause
|
686 | 693 | ),
|
| 694 | + hir::MatchSource::IfDesugar { contains_else_clause } => format!( |
| 695 | + "MatchSource::IfDesugar {{ contains_else_clause: {} }}", |
| 696 | + contains_else_clause |
| 697 | + ), |
687 | 698 | hir::MatchSource::AwaitDesugar => "MatchSource::AwaitDesugar".to_string(),
|
688 | 699 | }
|
689 | 700 | }
|
|
0 commit comments