2017-07-03 13:24:52 +00:00
|
|
|
//! A group of attributes that can be attached to Rust code in order
|
|
|
|
//! to generate a clippy lint detecting said code automatically.
|
|
|
|
|
2019-05-11 06:36:49 +00:00
|
|
|
use crate::utils::{get_attr, higher};
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc::hir::map::Map;
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast::{Attribute, LitFloatType, LitKind};
|
|
|
|
use rustc_ast::walk_list;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2020-03-18 22:45:02 +00:00
|
|
|
use rustc_session::Session;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2017-07-03 13:24:52 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Generates clippy code that detects the offending pattern
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ```rust,ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// // ./tests/ui/my_lint.rs
|
|
|
|
/// fn foo() {
|
|
|
|
/// // detect the following pattern
|
|
|
|
/// #[clippy::author]
|
|
|
|
/// if x == 42 {
|
|
|
|
/// // but ignore everything from here on
|
|
|
|
/// #![clippy::author = "ignore"]
|
|
|
|
/// }
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ()
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Running `TESTNAME=ui/my_lint cargo uitest` will produce
|
|
|
|
/// a `./tests/ui/new_lint.stdout` file with the generated code:
|
|
|
|
///
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ```rust,ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// // ./tests/ui/new_lint.stdout
|
|
|
|
/// if_chain! {
|
2019-09-27 15:16:06 +00:00
|
|
|
/// if let ExprKind::If(ref cond, ref then, None) = item.kind,
|
|
|
|
/// if let ExprKind::Binary(BinOp::Eq, ref left, ref right) = cond.kind,
|
|
|
|
/// if let ExprKind::Path(ref path) = left.kind,
|
|
|
|
/// if let ExprKind::Lit(ref lit) = right.kind,
|
2019-09-27 16:01:04 +00:00
|
|
|
/// if let LitKind::Int(42, _) = lit.node,
|
2019-03-05 16:50:33 +00:00
|
|
|
/// then {
|
|
|
|
/// // report your lint here
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-07-03 13:24:52 +00:00
|
|
|
pub LINT_AUTHOR,
|
2018-03-29 11:41:53 +00:00
|
|
|
internal_warn,
|
2017-07-03 13:24:52 +00:00
|
|
|
"helper for writing lints"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(Author => [LINT_AUTHOR]);
|
2017-07-03 13:24:52 +00:00
|
|
|
|
|
|
|
fn prelude() {
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("if_chain! {{");
|
2017-07-03 13:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn done() {
|
2017-10-23 19:20:37 +00:00
|
|
|
println!(" then {{");
|
|
|
|
println!(" // report your lint here");
|
|
|
|
println!(" }}");
|
|
|
|
println!("}}");
|
2017-07-03 13:24:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Author {
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &item.attrs) {
|
2017-07-03 13:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prelude();
|
|
|
|
PrintVisitor::new("item").visit_item(item);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ImplItem<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &item.attrs) {
|
2017-07-03 13:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prelude();
|
|
|
|
PrintVisitor::new("item").visit_impl_item(item);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &item.attrs) {
|
2017-07-03 13:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prelude();
|
|
|
|
PrintVisitor::new("item").visit_trait_item(item);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, var: &'tcx hir::Variant<'_>) {
|
2019-08-15 07:59:08 +00:00
|
|
|
if !has_attr(cx.sess(), &var.attrs) {
|
2017-07-03 13:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prelude();
|
2019-08-25 06:06:32 +00:00
|
|
|
PrintVisitor::new("var").visit_variant(var, &hir::Generics::empty(), hir::DUMMY_HIR_ID);
|
2017-07-03 13:24:52 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, field: &'tcx hir::StructField<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &field.attrs) {
|
2017-07-03 13:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prelude();
|
|
|
|
PrintVisitor::new("field").visit_struct_field(field);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &expr.attrs) {
|
2017-07-03 13:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prelude();
|
|
|
|
PrintVisitor::new("expr").visit_expr(expr);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_arm(&mut self, cx: &LateContext<'a, 'tcx>, arm: &'tcx hir::Arm<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &arm.attrs) {
|
2017-07-03 13:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prelude();
|
|
|
|
PrintVisitor::new("arm").visit_arm(arm);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx hir::Stmt<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if !has_attr(cx.sess(), stmt.kind.attrs()) {
|
2017-07-03 13:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prelude();
|
|
|
|
PrintVisitor::new("stmt").visit_stmt(stmt);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_foreign_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ForeignItem<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &item.attrs) {
|
2017-07-03 13:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prelude();
|
|
|
|
PrintVisitor::new("item").visit_foreign_item(item);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PrintVisitor {
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2017-07-03 13:24:52 +00:00
|
|
|
fn new(s: &'static str) -> Self {
|
2017-08-21 11:32:12 +00:00
|
|
|
Self {
|
2018-09-11 23:34:52 +00:00
|
|
|
ids: FxHashMap::default(),
|
2017-07-03 13:24:52 +00:00
|
|
|
current: s.to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next(&mut self, s: &'static str) -> String {
|
2020-02-21 08:39:38 +00:00
|
|
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
2017-07-03 13:24:52 +00:00
|
|
|
match self.ids.entry(s) {
|
|
|
|
// already there: start numbering from `1`
|
|
|
|
Occupied(mut occ) => {
|
|
|
|
let val = occ.get_mut();
|
|
|
|
*val += 1;
|
|
|
|
format!("{}{}", s, *val)
|
|
|
|
},
|
|
|
|
// not there: insert and return name as given
|
|
|
|
Vacant(vac) => {
|
|
|
|
vac.insert(0);
|
|
|
|
s.to_owned()
|
2017-08-09 07:30:56 +00:00
|
|
|
},
|
2017-07-03 13:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-24 18:34:51 +00:00
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn print_qpath(&mut self, path: &QPath<'_>) {
|
2018-02-24 18:34:51 +00:00
|
|
|
print!(" if match_qpath({}, &[", self.current);
|
|
|
|
print_path(path, &mut true);
|
|
|
|
println!("]);");
|
|
|
|
}
|
2017-07-03 13:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PrintVisitor {
|
2017-08-09 07:30:56 +00:00
|
|
|
/// Fields are the current index that needs to be appended to pattern
|
|
|
|
/// binding names
|
2018-09-11 23:34:52 +00:00
|
|
|
ids: FxHashMap<&'static str, usize>,
|
2017-07-03 13:24:52 +00:00
|
|
|
/// the name that needs to be destructured
|
|
|
|
current: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for PrintVisitor {
|
2020-01-09 07:13:22 +00:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2019-01-13 15:19:02 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_expr(&mut self, expr: &Expr<'_>) {
|
2019-05-11 06:36:49 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2018-07-12 07:30:57 +00:00
|
|
|
print!(" if let ExprKind::");
|
2019-09-27 15:16:06 +00:00
|
|
|
let current = format!("{}.kind", self.current);
|
|
|
|
match expr.kind {
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Box(ref inner) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let inner_pat = self.next("inner");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Box(ref {}) = {};", inner_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = inner_pat;
|
|
|
|
self.visit_expr(inner);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Array(ref elements) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let elements_pat = self.next("elements");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Array(ref {}) = {};", elements_pat, current);
|
|
|
|
println!(" if {}.len() == {};", elements_pat, elements.len());
|
2017-07-03 13:24:52 +00:00
|
|
|
for (i, element) in elements.iter().enumerate() {
|
|
|
|
self.current = format!("{}[{}]", elements_pat, i);
|
|
|
|
self.visit_expr(element);
|
|
|
|
}
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Call(ref func, ref args) => {
|
2018-05-27 13:45:01 +00:00
|
|
|
let func_pat = self.next("func");
|
|
|
|
let args_pat = self.next("args");
|
|
|
|
println!("Call(ref {}, ref {}) = {};", func_pat, args_pat, current);
|
|
|
|
self.current = func_pat;
|
|
|
|
self.visit_expr(func);
|
|
|
|
println!(" if {}.len() == {};", args_pat, args.len());
|
|
|
|
for (i, arg) in args.iter().enumerate() {
|
|
|
|
self.current = format!("{}[{}]", args_pat, i);
|
|
|
|
self.visit_expr(arg);
|
|
|
|
}
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::MethodCall(ref _method_name, ref _generics, ref _args) => {
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("MethodCall(ref method_name, ref generics, ref args) = {};", current);
|
2018-07-12 07:30:57 +00:00
|
|
|
println!(" // unimplemented: `ExprKind::MethodCall` is not further destructured at the moment");
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Tup(ref elements) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let elements_pat = self.next("elements");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Tup(ref {}) = {};", elements_pat, current);
|
|
|
|
println!(" if {}.len() == {};", elements_pat, elements.len());
|
2017-07-03 13:24:52 +00:00
|
|
|
for (i, element) in elements.iter().enumerate() {
|
|
|
|
self.current = format!("{}[{}]", elements_pat, i);
|
|
|
|
self.visit_expr(element);
|
|
|
|
}
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Binary(ref op, ref left, ref right) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let op_pat = self.next("op");
|
|
|
|
let left_pat = self.next("left");
|
|
|
|
let right_pat = self.next("right");
|
2018-11-27 20:14:15 +00:00
|
|
|
println!(
|
|
|
|
"Binary(ref {}, ref {}, ref {}) = {};",
|
|
|
|
op_pat, left_pat, right_pat, current
|
|
|
|
);
|
2018-07-12 07:50:09 +00:00
|
|
|
println!(" if BinOpKind::{:?} == {}.node;", op.node, op_pat);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = left_pat;
|
|
|
|
self.visit_expr(left);
|
|
|
|
self.current = right_pat;
|
|
|
|
self.visit_expr(right);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Unary(ref op, ref inner) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let inner_pat = self.next("inner");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Unary(UnOp::{:?}, ref {}) = {};", op, inner_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = inner_pat;
|
|
|
|
self.visit_expr(inner);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Lit(ref lit) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let lit_pat = self.next("lit");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Lit(ref {}) = {};", lit_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
match lit.node {
|
2017-10-23 19:20:37 +00:00
|
|
|
LitKind::Bool(val) => println!(" if let LitKind::Bool({:?}) = {}.node;", val, lit_pat),
|
|
|
|
LitKind::Char(c) => println!(" if let LitKind::Char({:?}) = {}.node;", c, lit_pat),
|
2019-01-20 20:54:04 +00:00
|
|
|
LitKind::Err(val) => println!(" if let LitKind::Err({}) = {}.node;", val, lit_pat),
|
2017-10-23 19:20:37 +00:00
|
|
|
LitKind::Byte(b) => println!(" if let LitKind::Byte({}) = {}.node;", b, lit_pat),
|
2017-07-03 13:24:52 +00:00
|
|
|
// FIXME: also check int type
|
2017-10-23 19:20:37 +00:00
|
|
|
LitKind::Int(i, _) => println!(" if let LitKind::Int({}, _) = {}.node;", i, lit_pat),
|
2019-11-07 12:27:00 +00:00
|
|
|
LitKind::Float(_, LitFloatType::Suffixed(_)) => println!(
|
|
|
|
" if let LitKind::Float(_, LitFloatType::Suffixed(_)) = {}.node;",
|
|
|
|
lit_pat
|
|
|
|
),
|
|
|
|
LitKind::Float(_, LitFloatType::Unsuffixed) => println!(
|
|
|
|
" if let LitKind::Float(_, LitFloatType::Unsuffixed) = {}.node;",
|
|
|
|
lit_pat
|
|
|
|
),
|
2017-07-03 13:24:52 +00:00
|
|
|
LitKind::ByteStr(ref vec) => {
|
|
|
|
let vec_pat = self.next("vec");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!(" if let LitKind::ByteStr(ref {}) = {}.node;", vec_pat, lit_pat);
|
|
|
|
println!(" if let [{:?}] = **{};", vec, vec_pat);
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
|
|
|
LitKind::Str(ref text, _) => {
|
|
|
|
let str_pat = self.next("s");
|
2019-08-23 01:28:44 +00:00
|
|
|
println!(" if let LitKind::Str(ref {}, _) = {}.node;", str_pat, lit_pat);
|
2017-10-23 19:20:37 +00:00
|
|
|
println!(" if {}.as_str() == {:?}", str_pat, &*text.as_str())
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
|
|
|
}
|
2017-08-09 07:30:56 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Cast(ref expr, ref ty) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let cast_pat = self.next("expr");
|
2018-02-24 01:02:48 +00:00
|
|
|
let cast_ty = self.next("cast_ty");
|
|
|
|
let qp_label = self.next("qp");
|
|
|
|
|
|
|
|
println!("Cast(ref {}, ref {}) = {};", cast_pat, cast_ty, current);
|
2019-09-27 15:16:06 +00:00
|
|
|
if let TyKind::Path(ref qp) = ty.kind {
|
|
|
|
println!(" if let TyKind::Path(ref {}) = {}.kind;", qp_label, cast_ty);
|
2018-02-24 01:02:48 +00:00
|
|
|
self.current = qp_label;
|
2018-03-15 15:07:15 +00:00
|
|
|
self.print_qpath(qp);
|
2018-02-24 01:02:48 +00:00
|
|
|
}
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = cast_pat;
|
|
|
|
self.visit_expr(expr);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Type(ref expr, ref _ty) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let cast_pat = self.next("expr");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Type(ref {}, _) = {};", cast_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = cast_pat;
|
|
|
|
self.visit_expr(expr);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Loop(ref body, _, desugaring) => {
|
2018-01-24 12:04:06 +00:00
|
|
|
let body_pat = self.next("body");
|
|
|
|
let des = loop_desugaring_name(desugaring);
|
|
|
|
let label_pat = self.next("label");
|
|
|
|
println!("Loop(ref {}, ref {}, {}) = {};", body_pat, label_pat, des, current);
|
|
|
|
self.current = body_pat;
|
|
|
|
self.visit_block(body);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Match(ref expr, ref arms, desugaring) => {
|
2018-01-24 12:04:06 +00:00
|
|
|
let des = desugaring_name(desugaring);
|
2018-05-11 17:05:34 +00:00
|
|
|
let expr_pat = self.next("expr");
|
|
|
|
let arms_pat = self.next("arms");
|
|
|
|
println!("Match(ref {}, ref {}, {}) = {};", expr_pat, arms_pat, des, current);
|
|
|
|
self.current = expr_pat;
|
|
|
|
self.visit_expr(expr);
|
|
|
|
println!(" if {}.len() == {};", arms_pat, arms.len());
|
|
|
|
for (i, arm) in arms.iter().enumerate() {
|
|
|
|
self.current = format!("{}[{}].body", arms_pat, i);
|
|
|
|
self.visit_expr(&arm.body);
|
|
|
|
if let Some(ref guard) = arm.guard {
|
|
|
|
let guard_pat = self.next("guard");
|
2018-09-02 07:38:25 +00:00
|
|
|
println!(" if let Some(ref {}) = {}[{}].guard;", guard_pat, arms_pat, i);
|
|
|
|
match guard {
|
|
|
|
hir::Guard::If(ref if_expr) => {
|
|
|
|
let if_expr_pat = self.next("expr");
|
|
|
|
println!(" if let Guard::If(ref {}) = {};", if_expr_pat, guard_pat);
|
|
|
|
self.current = if_expr_pat;
|
|
|
|
self.visit_expr(if_expr);
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-09-02 07:38:25 +00:00
|
|
|
}
|
2018-05-11 17:05:34 +00:00
|
|
|
}
|
2019-09-25 19:00:17 +00:00
|
|
|
self.current = format!("{}[{}].pat", arms_pat, i);
|
|
|
|
self.visit_pat(&arm.pat);
|
2018-05-11 17:05:34 +00:00
|
|
|
}
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Closure(ref _capture_clause, ref _func, _, _, _) => {
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Closure(ref capture_clause, ref func, _, _, _) = {};", current);
|
2018-07-12 07:30:57 +00:00
|
|
|
println!(" // unimplemented: `ExprKind::Closure` is not further destructured at the moment");
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
2019-06-19 09:59:25 +00:00
|
|
|
ExprKind::Yield(ref sub, _) => {
|
2017-08-30 08:54:24 +00:00
|
|
|
let sub_pat = self.next("sub");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Yield(ref sub) = {};", current);
|
2017-08-30 08:54:24 +00:00
|
|
|
self.current = sub_pat;
|
|
|
|
self.visit_expr(sub);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Block(ref block, _) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let block_pat = self.next("block");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Block(ref {}) = {};", block_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = block_pat;
|
|
|
|
self.visit_block(block);
|
|
|
|
},
|
2019-12-24 04:16:04 +00:00
|
|
|
ExprKind::Assign(ref target, ref value, _) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let target_pat = self.next("target");
|
|
|
|
let value_pat = self.next("value");
|
2019-12-24 04:16:04 +00:00
|
|
|
println!(
|
|
|
|
"Assign(ref {}, ref {}, ref _span) = {};",
|
|
|
|
target_pat, value_pat, current
|
|
|
|
);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = target_pat;
|
|
|
|
self.visit_expr(target);
|
|
|
|
self.current = value_pat;
|
|
|
|
self.visit_expr(value);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::AssignOp(ref op, ref target, ref value) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let op_pat = self.next("op");
|
|
|
|
let target_pat = self.next("target");
|
|
|
|
let value_pat = self.next("value");
|
2018-11-27 20:14:15 +00:00
|
|
|
println!(
|
|
|
|
"AssignOp(ref {}, ref {}, ref {}) = {};",
|
|
|
|
op_pat, target_pat, value_pat, current
|
|
|
|
);
|
2018-07-12 07:50:09 +00:00
|
|
|
println!(" if BinOpKind::{:?} == {}.node;", op.node, op_pat);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = target_pat;
|
|
|
|
self.visit_expr(target);
|
|
|
|
self.current = value_pat;
|
|
|
|
self.visit_expr(value);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Field(ref object, ref field_ident) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let obj_pat = self.next("object");
|
|
|
|
let field_name_pat = self.next("field_name");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Field(ref {}, ref {}) = {};", obj_pat, field_name_pat, current);
|
2019-09-27 16:01:04 +00:00
|
|
|
println!(" if {}.as_str() == {:?}", field_name_pat, field_ident.as_str());
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = obj_pat;
|
|
|
|
self.visit_expr(object);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Index(ref object, ref index) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let object_pat = self.next("object");
|
|
|
|
let index_pat = self.next("index");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Index(ref {}, ref {}) = {};", object_pat, index_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = object_pat;
|
|
|
|
self.visit_expr(object);
|
|
|
|
self.current = index_pat;
|
|
|
|
self.visit_expr(index);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Path(ref path) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let path_pat = self.next("path");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Path(ref {}) = {};", path_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = path_pat;
|
2018-02-24 18:34:51 +00:00
|
|
|
self.print_qpath(path);
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
2019-11-27 22:43:22 +00:00
|
|
|
ExprKind::AddrOf(kind, mutability, ref inner) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let inner_pat = self.next("inner");
|
2019-11-25 14:20:10 +00:00
|
|
|
println!(
|
2019-11-27 22:43:22 +00:00
|
|
|
"AddrOf(BorrowKind::{:?}, Mutability::{:?}, ref {}) = {};",
|
|
|
|
kind, mutability, inner_pat, current
|
2019-11-25 14:20:10 +00:00
|
|
|
);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = inner_pat;
|
|
|
|
self.visit_expr(inner);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Break(ref _destination, ref opt_value) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let destination_pat = self.next("destination");
|
|
|
|
if let Some(ref value) = *opt_value {
|
|
|
|
let value_pat = self.next("value");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Break(ref {}, Some(ref {})) = {};", destination_pat, value_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = value_pat;
|
|
|
|
self.visit_expr(value);
|
|
|
|
} else {
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Break(ref {}, None) = {};", destination_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
}
|
|
|
|
// FIXME: implement label printing
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Continue(ref _destination) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let destination_pat = self.next("destination");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Again(ref {}) = {};", destination_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
// FIXME: implement label printing
|
|
|
|
},
|
2018-11-27 20:14:15 +00:00
|
|
|
ExprKind::Ret(ref opt_value) => {
|
|
|
|
if let Some(ref value) = *opt_value {
|
|
|
|
let value_pat = self.next("value");
|
|
|
|
println!("Ret(Some(ref {})) = {};", value_pat, current);
|
|
|
|
self.current = value_pat;
|
|
|
|
self.visit_expr(value);
|
|
|
|
} else {
|
|
|
|
println!("Ret(None) = {};", current);
|
|
|
|
}
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
2019-11-22 12:25:38 +00:00
|
|
|
ExprKind::InlineAsm(_) => {
|
|
|
|
println!("InlineAsm(_) = {};", current);
|
2018-07-12 07:30:57 +00:00
|
|
|
println!(" // unimplemented: `ExprKind::InlineAsm` is not further destructured at the moment");
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Struct(ref path, ref fields, ref opt_base) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let path_pat = self.next("path");
|
|
|
|
let fields_pat = self.next("fields");
|
|
|
|
if let Some(ref base) = *opt_base {
|
|
|
|
let base_pat = self.next("base");
|
2017-08-09 07:30:56 +00:00
|
|
|
println!(
|
2017-10-23 19:20:37 +00:00
|
|
|
"Struct(ref {}, ref {}, Some(ref {})) = {};",
|
2018-11-27 20:14:15 +00:00
|
|
|
path_pat, fields_pat, base_pat, current
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2017-07-03 13:24:52 +00:00
|
|
|
self.current = base_pat;
|
|
|
|
self.visit_expr(base);
|
|
|
|
} else {
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Struct(ref {}, ref {}, None) = {};", path_pat, fields_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
}
|
|
|
|
self.current = path_pat;
|
2018-02-24 18:34:51 +00:00
|
|
|
self.print_qpath(path);
|
2017-10-23 19:20:37 +00:00
|
|
|
println!(" if {}.len() == {};", fields_pat, fields.len());
|
2017-07-03 13:24:52 +00:00
|
|
|
println!(" // unimplemented: field checks");
|
|
|
|
},
|
|
|
|
// FIXME: compute length (needs type info)
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Repeat(ref value, _) => {
|
2017-07-03 13:24:52 +00:00
|
|
|
let value_pat = self.next("value");
|
2017-10-23 19:20:37 +00:00
|
|
|
println!("Repeat(ref {}, _) = {};", value_pat, current);
|
2017-07-03 13:24:52 +00:00
|
|
|
println!("// unimplemented: repeat count check");
|
|
|
|
self.current = value_pat;
|
|
|
|
self.visit_expr(value);
|
|
|
|
},
|
2018-12-28 11:29:34 +00:00
|
|
|
ExprKind::Err => {
|
|
|
|
println!("Err = {}", current);
|
|
|
|
},
|
2019-05-01 20:18:05 +00:00
|
|
|
ExprKind::DropTemps(ref expr) => {
|
2019-04-28 07:11:20 +00:00
|
|
|
let expr_pat = self.next("expr");
|
2019-05-01 20:52:19 +00:00
|
|
|
println!("DropTemps(ref {}) = {};", expr_pat, current);
|
2019-04-28 07:11:20 +00:00
|
|
|
self.current = expr_pat;
|
|
|
|
self.visit_expr(expr);
|
|
|
|
},
|
2017-07-03 13:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_block(&mut self, block: &Block<'_>) {
|
2019-05-19 14:52:44 +00:00
|
|
|
let trailing_pat = self.next("trailing_expr");
|
|
|
|
println!(" if let Some({}) = &{}.expr;", trailing_pat, self.current);
|
|
|
|
println!(" if {}.stmts.len() == {};", self.current, block.stmts.len());
|
|
|
|
let current = self.current.clone();
|
|
|
|
for (i, stmt) in block.stmts.iter().enumerate() {
|
|
|
|
self.current = format!("{}.stmts[{}]", current, i);
|
|
|
|
self.visit_stmt(stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-13 15:19:02 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_pat(&mut self, pat: &Pat<'_>) {
|
2018-05-11 17:05:34 +00:00
|
|
|
print!(" if let PatKind::");
|
2019-09-27 15:16:06 +00:00
|
|
|
let current = format!("{}.kind", self.current);
|
|
|
|
match pat.kind {
|
2018-05-11 17:05:34 +00:00
|
|
|
PatKind::Wild => println!("Wild = {};", current),
|
2019-02-03 07:12:07 +00:00
|
|
|
PatKind::Binding(anno, .., ident, ref sub) => {
|
2018-05-11 17:05:34 +00:00
|
|
|
let anno_pat = match anno {
|
|
|
|
BindingAnnotation::Unannotated => "BindingAnnotation::Unannotated",
|
|
|
|
BindingAnnotation::Mutable => "BindingAnnotation::Mutable",
|
|
|
|
BindingAnnotation::Ref => "BindingAnnotation::Ref",
|
|
|
|
BindingAnnotation::RefMut => "BindingAnnotation::RefMut",
|
|
|
|
};
|
|
|
|
let name_pat = self.next("name");
|
|
|
|
if let Some(ref sub) = *sub {
|
|
|
|
let sub_pat = self.next("sub");
|
2018-11-27 20:14:15 +00:00
|
|
|
println!(
|
|
|
|
"Binding({}, _, {}, Some(ref {})) = {};",
|
|
|
|
anno_pat, name_pat, sub_pat, current
|
|
|
|
);
|
2018-05-11 17:05:34 +00:00
|
|
|
self.current = sub_pat;
|
|
|
|
self.visit_pat(sub);
|
|
|
|
} else {
|
|
|
|
println!("Binding({}, _, {}, None) = {};", anno_pat, name_pat, current);
|
|
|
|
}
|
2019-09-27 16:01:04 +00:00
|
|
|
println!(" if {}.as_str() == \"{}\";", name_pat, ident.as_str());
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-05-11 17:05:34 +00:00
|
|
|
PatKind::Struct(ref path, ref fields, ignore) => {
|
|
|
|
let path_pat = self.next("path");
|
|
|
|
let fields_pat = self.next("fields");
|
2018-11-27 20:14:15 +00:00
|
|
|
println!(
|
|
|
|
"Struct(ref {}, ref {}, {}) = {};",
|
|
|
|
path_pat, fields_pat, ignore, current
|
|
|
|
);
|
2018-05-11 17:05:34 +00:00
|
|
|
self.current = path_pat;
|
|
|
|
self.print_qpath(path);
|
|
|
|
println!(" if {}.len() == {};", fields_pat, fields.len());
|
|
|
|
println!(" // unimplemented: field checks");
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2019-08-18 11:14:47 +00:00
|
|
|
PatKind::Or(ref fields) => {
|
|
|
|
let fields_pat = self.next("fields");
|
|
|
|
println!("Or(ref {}) = {};", fields_pat, current);
|
|
|
|
println!(" if {}.len() == {};", fields_pat, fields.len());
|
|
|
|
println!(" // unimplemented: field checks");
|
|
|
|
},
|
2018-05-11 17:05:34 +00:00
|
|
|
PatKind::TupleStruct(ref path, ref fields, skip_pos) => {
|
|
|
|
let path_pat = self.next("path");
|
|
|
|
let fields_pat = self.next("fields");
|
2018-11-27 20:14:15 +00:00
|
|
|
println!(
|
|
|
|
"TupleStruct(ref {}, ref {}, {:?}) = {};",
|
|
|
|
path_pat, fields_pat, skip_pos, current
|
|
|
|
);
|
2018-05-11 17:05:34 +00:00
|
|
|
self.current = path_pat;
|
|
|
|
self.print_qpath(path);
|
|
|
|
println!(" if {}.len() == {};", fields_pat, fields.len());
|
|
|
|
println!(" // unimplemented: field checks");
|
|
|
|
},
|
|
|
|
PatKind::Path(ref path) => {
|
|
|
|
let path_pat = self.next("path");
|
|
|
|
println!("Path(ref {}) = {};", path_pat, current);
|
|
|
|
self.current = path_pat;
|
|
|
|
self.print_qpath(path);
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-05-11 17:05:34 +00:00
|
|
|
PatKind::Tuple(ref fields, skip_pos) => {
|
|
|
|
let fields_pat = self.next("fields");
|
|
|
|
println!("Tuple(ref {}, {:?}) = {};", fields_pat, skip_pos, current);
|
|
|
|
println!(" if {}.len() == {};", fields_pat, fields.len());
|
|
|
|
println!(" // unimplemented: field checks");
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-05-11 17:05:34 +00:00
|
|
|
PatKind::Box(ref pat) => {
|
|
|
|
let pat_pat = self.next("pat");
|
|
|
|
println!("Box(ref {}) = {};", pat_pat, current);
|
|
|
|
self.current = pat_pat;
|
|
|
|
self.visit_pat(pat);
|
|
|
|
},
|
|
|
|
PatKind::Ref(ref pat, muta) => {
|
|
|
|
let pat_pat = self.next("pat");
|
|
|
|
println!("Ref(ref {}, Mutability::{:?}) = {};", pat_pat, muta, current);
|
|
|
|
self.current = pat_pat;
|
|
|
|
self.visit_pat(pat);
|
|
|
|
},
|
|
|
|
PatKind::Lit(ref lit_expr) => {
|
|
|
|
let lit_expr_pat = self.next("lit_expr");
|
|
|
|
println!("Lit(ref {}) = {}", lit_expr_pat, current);
|
|
|
|
self.current = lit_expr_pat;
|
|
|
|
self.visit_expr(lit_expr);
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-05-11 17:05:34 +00:00
|
|
|
PatKind::Range(ref start, ref end, end_kind) => {
|
|
|
|
let start_pat = self.next("start");
|
|
|
|
let end_pat = self.next("end");
|
2018-11-27 20:14:15 +00:00
|
|
|
println!(
|
|
|
|
"Range(ref {}, ref {}, RangeEnd::{:?}) = {};",
|
|
|
|
start_pat, end_pat, end_kind, current
|
|
|
|
);
|
2018-05-11 17:05:34 +00:00
|
|
|
self.current = start_pat;
|
2020-01-11 10:39:43 +00:00
|
|
|
walk_list!(self, visit_expr, start);
|
2018-05-11 17:05:34 +00:00
|
|
|
self.current = end_pat;
|
2020-01-11 10:39:43 +00:00
|
|
|
walk_list!(self, visit_expr, end);
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-05-11 17:05:34 +00:00
|
|
|
PatKind::Slice(ref start, ref middle, ref end) => {
|
|
|
|
let start_pat = self.next("start");
|
|
|
|
let end_pat = self.next("end");
|
|
|
|
if let Some(ref middle) = middle {
|
|
|
|
let middle_pat = self.next("middle");
|
2018-11-27 20:14:15 +00:00
|
|
|
println!(
|
|
|
|
"Slice(ref {}, Some(ref {}), ref {}) = {};",
|
|
|
|
start_pat, middle_pat, end_pat, current
|
|
|
|
);
|
2018-05-11 17:05:34 +00:00
|
|
|
self.current = middle_pat;
|
|
|
|
self.visit_pat(middle);
|
|
|
|
} else {
|
|
|
|
println!("Slice(ref {}, None, ref {}) = {};", start_pat, end_pat, current);
|
|
|
|
}
|
|
|
|
println!(" if {}.len() == {};", start_pat, start.len());
|
|
|
|
for (i, pat) in start.iter().enumerate() {
|
|
|
|
self.current = format!("{}[{}]", start_pat, i);
|
|
|
|
self.visit_pat(pat);
|
|
|
|
}
|
|
|
|
println!(" if {}.len() == {};", end_pat, end.len());
|
|
|
|
for (i, pat) in end.iter().enumerate() {
|
|
|
|
self.current = format!("{}[{}]", end_pat, i);
|
|
|
|
self.visit_pat(pat);
|
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-05-11 17:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_stmt(&mut self, s: &Stmt<'_>) {
|
2018-07-16 13:07:39 +00:00
|
|
|
print!(" if let StmtKind::");
|
2019-09-27 15:16:06 +00:00
|
|
|
let current = format!("{}.kind", self.current);
|
|
|
|
match s.kind {
|
2019-01-20 10:21:30 +00:00
|
|
|
// A local (let) binding:
|
|
|
|
StmtKind::Local(ref local) => {
|
|
|
|
let local_pat = self.next("local");
|
|
|
|
println!("Local(ref {}) = {};", local_pat, current);
|
|
|
|
if let Some(ref init) = local.init {
|
|
|
|
let init_pat = self.next("init");
|
2019-03-07 13:07:01 +00:00
|
|
|
println!(" if let Some(ref {}) = {}.init;", init_pat, local_pat);
|
2019-01-20 10:21:30 +00:00
|
|
|
self.current = init_pat;
|
|
|
|
self.visit_expr(init);
|
2018-05-11 17:05:34 +00:00
|
|
|
}
|
2019-01-20 10:21:30 +00:00
|
|
|
self.current = format!("{}.pat", local_pat);
|
|
|
|
self.visit_pat(&local.pat);
|
|
|
|
},
|
|
|
|
// An item binding:
|
|
|
|
StmtKind::Item(_) => {
|
|
|
|
println!("Item(item_id) = {};", current);
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-05-11 17:05:34 +00:00
|
|
|
|
|
|
|
// Expr without trailing semi-colon (must have unit type):
|
2019-01-20 10:21:30 +00:00
|
|
|
StmtKind::Expr(ref e) => {
|
2018-05-11 17:05:34 +00:00
|
|
|
let e_pat = self.next("e");
|
2018-07-16 13:07:39 +00:00
|
|
|
println!("Expr(ref {}, _) = {}", e_pat, current);
|
2018-05-11 17:05:34 +00:00
|
|
|
self.current = e_pat;
|
|
|
|
self.visit_expr(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Expr with trailing semi-colon (may have any type):
|
2019-01-20 10:21:30 +00:00
|
|
|
StmtKind::Semi(ref e) => {
|
2018-05-11 17:05:34 +00:00
|
|
|
let e_pat = self.next("e");
|
2018-07-16 13:07:39 +00:00
|
|
|
println!("Semi(ref {}, _) = {}", e_pat, current);
|
2018-05-11 17:05:34 +00:00
|
|
|
self.current = e_pat;
|
|
|
|
self.visit_expr(e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:41:20 +00:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
2017-07-03 13:24:52 +00:00
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 15:44:42 +00:00
|
|
|
fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool {
|
|
|
|
get_attr(sess, attrs, "author").count() > 0
|
2017-07-03 13:24:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-01-24 12:04:06 +00:00
|
|
|
fn desugaring_name(des: hir::MatchSource) -> String {
|
|
|
|
match des {
|
|
|
|
hir::MatchSource::ForLoopDesugar => "MatchSource::ForLoopDesugar".to_string(),
|
|
|
|
hir::MatchSource::TryDesugar => "MatchSource::TryDesugar".to_string(),
|
2019-07-06 17:06:49 +00:00
|
|
|
hir::MatchSource::WhileDesugar => "MatchSource::WhileDesugar".to_string(),
|
2018-01-24 12:04:06 +00:00
|
|
|
hir::MatchSource::WhileLetDesugar => "MatchSource::WhileLetDesugar".to_string(),
|
|
|
|
hir::MatchSource::Normal => "MatchSource::Normal".to_string(),
|
2018-11-27 20:14:15 +00:00
|
|
|
hir::MatchSource::IfLetDesugar { contains_else_clause } => format!(
|
|
|
|
"MatchSource::IfLetDesugar {{ contains_else_clause: {} }}",
|
|
|
|
contains_else_clause
|
|
|
|
),
|
2019-05-11 02:40:05 +00:00
|
|
|
hir::MatchSource::IfDesugar { contains_else_clause } => format!(
|
|
|
|
"MatchSource::IfDesugar {{ contains_else_clause: {} }}",
|
|
|
|
contains_else_clause
|
|
|
|
),
|
2019-05-08 04:58:37 +00:00
|
|
|
hir::MatchSource::AwaitDesugar => "MatchSource::AwaitDesugar".to_string(),
|
2018-01-24 12:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-01-24 12:04:06 +00:00
|
|
|
fn loop_desugaring_name(des: hir::LoopSource) -> &'static str {
|
|
|
|
match des {
|
|
|
|
hir::LoopSource::ForLoop => "LoopSource::ForLoop",
|
|
|
|
hir::LoopSource::Loop => "LoopSource::Loop",
|
2019-07-06 17:35:08 +00:00
|
|
|
hir::LoopSource::While => "LoopSource::While",
|
2018-01-24 12:04:06 +00:00
|
|
|
hir::LoopSource::WhileLet => "LoopSource::WhileLet",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn print_path(path: &QPath<'_>, first: &mut bool) {
|
2017-07-03 13:24:52 +00:00
|
|
|
match *path {
|
2018-11-27 20:14:15 +00:00
|
|
|
QPath::Resolved(_, ref path) => {
|
2019-12-30 04:02:10 +00:00
|
|
|
for segment in path.segments {
|
2018-11-27 20:14:15 +00:00
|
|
|
if *first {
|
|
|
|
*first = false;
|
|
|
|
} else {
|
|
|
|
print!(", ");
|
|
|
|
}
|
|
|
|
print!("{:?}", segment.ident.as_str());
|
2017-09-05 09:33:04 +00:00
|
|
|
}
|
|
|
|
},
|
2019-09-27 15:16:06 +00:00
|
|
|
QPath::TypeRelative(ref ty, ref segment) => match ty.kind {
|
2018-07-12 08:03:06 +00:00
|
|
|
hir::TyKind::Path(ref inner_path) => {
|
2017-09-05 09:33:04 +00:00
|
|
|
print_path(inner_path, first);
|
2017-08-09 07:30:56 +00:00
|
|
|
if *first {
|
|
|
|
*first = false;
|
|
|
|
} else {
|
|
|
|
print!(", ");
|
|
|
|
}
|
2018-06-28 13:46:58 +00:00
|
|
|
print!("{:?}", segment.ident.as_str());
|
2017-09-05 09:33:04 +00:00
|
|
|
},
|
|
|
|
ref other => print!("/* unimplemented: {:?}*/", other),
|
2017-07-03 13:24:52 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|