@@ -51,38 +51,62 @@ impl LintPass for UseLast {
51
51
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UseLast {
52
52
fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr ) {
53
53
if_chain ! {
54
+ // let _ = println!("Starting UseLast");
54
55
// Is a method call
55
56
if let ExprKind :: MethodCall ( ref path, _, ref args) = expr. node;
57
+ // let _ = println!("It is a MethodCall");
56
58
57
59
// Method name is "get"
58
60
if path. ident. name == "get" ;
61
+ // let _ = println!("The name is get");
59
62
60
63
// Argument 0 (the struct we're calling the method on) is a vector
61
64
if let Some ( struct_calling_on) = args. get( 0 ) ;
65
+ // let _ = println!("It had an argument");
62
66
let struct_ty = cx. tables. expr_ty( struct_calling_on) ;
63
67
if match_type( cx, struct_ty, & paths:: VEC ) ;
68
+ // let _ = println!("It was a vector");
64
69
65
70
// Argument to "get" is a binary operation
66
71
if let Some ( get_index_arg) = args. get( 1 ) ;
72
+ // let _ = println!("It had an argument");
67
73
if let rustc:: hir:: ExprKind :: Binary ( ref op, ref lhs, ref rhs) = get_index_arg. node;
74
+ // let _ = println!("It was a vector");
68
75
69
76
// Binary operation is a subtraction
70
77
if op. node == rustc:: hir:: BinOpKind :: Sub ;
78
+ // let _ = println!("It was a subtraction");
71
79
72
80
// LHS of subtraction is "x.len()"
73
81
if let ExprKind :: MethodCall ( ref arg_lhs_path, _, ref lhs_args) = lhs. node;
82
+ // let _ = println!("LHS of sub is a method call");
74
83
if arg_lhs_path. ident. name == "len" ;
75
- if let Some ( arg_lhs_struct) = lhs_args. get( 0 ) ;
84
+ // let _ = println!("LHS of sub was method named len");
85
+ // if let Some(arg_lhs_struct) = lhs_args.get(0);
86
+ // let _ = println!("LHS of sub method has an arg");
76
87
// TODO: Is this a valid way to check if they reference the same vector?
77
- if arg_lhs_struct. hir_id == struct_calling_on. hir_id;
88
+ // if arg_lhs_struct.hir_id == struct_calling_on.hir_id;
89
+ // let _ = println!("The vector in .get and .len were the same");
78
90
79
91
// RHS of subtraction is 1
80
92
if let ExprKind :: Lit ( ref rhs_lit) = rhs. node;
93
+ // let _ = println!("RHS of sub was literal");
81
94
if let LitKind :: Int ( rhs_value, ..) = rhs_lit. node;
95
+ // let _ = println!("RHS of sub was int");
82
96
if rhs_value == 1 ;
97
+ // let _ = println!("RHS of sub was 1");
83
98
84
99
// TODO: Figure out how to get name of variable for lint message
100
+ // Can't do this (cannot move out of borrowed content (context?))
101
+ // if let ExprKind::Struct(ref struct_calling_on_path, _, _) = struct_calling_on.node;
102
+ // let _ = println!("It was a struct");
103
+ // let vec_name = match struct_calling_on_path.into_inner() {
104
+ // rustc::hir::QPath::Resolved(_, path) =>
105
+ // path.segments.last().map(|path_seg| path_seg.ident.name.as_str().get()).unwrap_or("x"),
106
+ // rustc::hir::QPath::TypeRelative(_, path_seg) => path_seg.ident.name.as_str().get(),
107
+ // };
85
108
let vec_name = "x" ;
109
+ // let _ = println!("About to span_lint on \"{}\"", vec_name);
86
110
87
111
then {
88
112
span_lint( cx,
0 commit comments