1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use syntax::ast::Crate;

use crate::command::{CommandState, Registry};
use crate::matcher::replace_expr;
use crate::transform::Transform;
use crate::RefactorCtxt;


/// # `wrapping_arith_to_normal` Command
/// 
/// Usage: `wrapping_arith_to_normal`
/// 
/// Replace all uses of wrapping arithmetic methods with ordinary arithmetic
/// operators.  For example, replace `x.wrapping_add(y)` with `x + y`.
pub struct WrappingToNormal;

impl Transform for WrappingToNormal {
    fn transform(&self, krate: &mut Crate, st: &CommandState, cx: &RefactorCtxt) {
        replace_expr(st, cx, krate,
                     "$x:Expr.wrapping_add($y:Expr)",
                     "$x + $y");
        replace_expr(st, cx, krate,
                     "$x:Expr.wrapping_sub($y:Expr)",
                     "$x - $y");
        replace_expr(st, cx, krate,
                     "$x:Expr.wrapping_mul($y:Expr)",
                     "$x * $y");
        replace_expr(st, cx, krate,
                     "$x:Expr.wrapping_div($y:Expr)",
                     "$x / $y");
        replace_expr(st, cx, krate,
                     "$x:Expr.wrapping_rem($y:Expr)",
                     "$x % $y");
        replace_expr(st, cx, krate,
                     "$x:Expr.wrapping_neg()",
                     "-$x");
        replace_expr(st, cx, krate,
                     "$x:Expr.wrapping_shl($y:Expr)",
                     "$x << $y");
        replace_expr(st, cx, krate,
                     "$x:Expr.wrapping_shr($y:Expr)",
                     "$x >> $y");
        replace_expr(st, cx, krate,
                     "$x:Expr.wrapping_abs()",
                     "$x:Expr.abs()");
    }
}


pub fn register_commands(reg: &mut Registry) {
    use super::mk;

    reg.register("wrapping_arith_to_normal", |_args| mk(WrappingToNormal));
}