|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use rustc_hir::{ |
| 4 | + def_id::DefId, |
| 5 | + intravisit::{walk_expr, Visitor}, |
| 6 | + BinOpKind, Body, Expr, ExprKind, Mutability, |
| 7 | +}; |
| 8 | +use rustc_lint::LateContext; |
| 9 | +use rustc_middle::ty::TyKind as MiddleTyKind; |
| 10 | + |
| 11 | +use crate::ANCHOR_ACCOUNT_GENERIC_ARG_COUNT; |
| 12 | +use clippy_utils::{ty::match_type, SpanlessEq}; |
| 13 | +use if_chain::if_chain; |
| 14 | +use solana_lints::paths; |
| 15 | + |
| 16 | +pub struct Values<'cx, 'tcx> { |
| 17 | + cx: &'cx LateContext<'tcx>, |
| 18 | + pub accounts: HashMap<DefId, Vec<&'tcx Expr<'tcx>>>, |
| 19 | + pub if_statements: Vec<(&'tcx Expr<'tcx>, &'tcx Expr<'tcx>)>, |
| 20 | +} |
| 21 | + |
| 22 | +impl<'cx, 'tcx> Values<'cx, 'tcx> { |
| 23 | + pub fn new(cx: &'cx LateContext<'tcx>) -> Self { |
| 24 | + Values { |
| 25 | + cx, |
| 26 | + accounts: HashMap::new(), |
| 27 | + if_statements: Vec::new(), |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + pub fn get_referenced_accounts_and_if_statements(&mut self, body: &'tcx Body<'tcx>) -> &Self { |
| 32 | + self.visit_expr(&body.value); |
| 33 | + self |
| 34 | + } |
| 35 | + |
| 36 | + /// Checks if there is a valid key constraint for `first_account` and `second_account`. |
| 37 | + /// NOTE: currently only considers `first.key() == second.key()` or the symmetric relation as valid constraints. |
| 38 | + /// TODO: if == relation used, should return some error in the THEN block |
| 39 | + pub fn check_key_constraint( |
| 40 | + &self, |
| 41 | + first_account: &Expr<'_>, |
| 42 | + second_account: &Expr<'_>, |
| 43 | + ) -> bool { |
| 44 | + for (left, right) in &self.if_statements { |
| 45 | + if_chain! { |
| 46 | + if let ExprKind::MethodCall(path_seg_left, exprs_left, _span) = left.kind; |
| 47 | + if let ExprKind::MethodCall(path_seg_right, exprs_right, _span) = right.kind; |
| 48 | + if path_seg_left.ident.name.as_str() == "key" && path_seg_right.ident.name.as_str() == "key"; |
| 49 | + if !exprs_left.is_empty() && !exprs_right.is_empty(); |
| 50 | + let mut spanless_eq = SpanlessEq::new(self.cx); |
| 51 | + if (spanless_eq.eq_expr(&exprs_left[0], first_account) && spanless_eq.eq_expr(&exprs_right[0], second_account)) |
| 52 | + || (spanless_eq.eq_expr(&exprs_left[0], second_account) && spanless_eq.eq_expr(&exprs_right[0], first_account)); |
| 53 | + then { |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + false |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<'cx, 'tcx> Visitor<'tcx> for Values<'cx, 'tcx> { |
| 63 | + fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) { |
| 64 | + if_chain! { |
| 65 | + // get mutable reference expressions |
| 66 | + if let ExprKind::AddrOf(_, mutability, mut_expr) = expr.kind; |
| 67 | + if let Mutability::Mut = mutability; |
| 68 | + // check type of expr == Account<'info, T> |
| 69 | + let middle_ty = self.cx.typeck_results().expr_ty(mut_expr); |
| 70 | + // let mut_expr_def_id = self.cx.tcx.hir().local_def_id(mut_expr.hir_id).to_def_id(); |
| 71 | + // let middle_ty = self.cx.tcx.type_of(mut_expr_def_id); |
| 72 | + if match_type(self.cx, middle_ty, &paths::ANCHOR_ACCOUNT); |
| 73 | + // grab T generic parameter |
| 74 | + if let MiddleTyKind::Adt(_adt_def, substs) = middle_ty.kind(); |
| 75 | + if substs.len() == ANCHOR_ACCOUNT_GENERIC_ARG_COUNT; |
| 76 | + let account_type = substs[1].expect_ty(); |
| 77 | + if let Some(adt_def) = account_type.ty_adt_def(); |
| 78 | + then { |
| 79 | + let def_id = adt_def.did(); |
| 80 | + if let Some(exprs) = self.accounts.get_mut(&def_id) { |
| 81 | + let mut spanless_eq = SpanlessEq::new(self.cx); |
| 82 | + // check that expr is not a duplicate within its particular key-pair |
| 83 | + if exprs.iter().all(|e| !spanless_eq.eq_expr(e, mut_expr)) { |
| 84 | + exprs.push(mut_expr); |
| 85 | + } |
| 86 | + } else { |
| 87 | + self.accounts.insert(def_id, vec![mut_expr]); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // get if statements |
| 93 | + if_chain! { |
| 94 | + if let ExprKind::If(wrapped_if_expr, _then, _else_opt) = expr.kind; |
| 95 | + if let ExprKind::DropTemps(if_expr) = wrapped_if_expr.kind; |
| 96 | + if let ExprKind::Binary(op, left, right) = if_expr.kind; |
| 97 | + // TODO: leaves out || or &&. Could implement something that pulls apart |
| 98 | + // an if expr that is of this form into individual == or != comparisons |
| 99 | + if let BinOpKind::Ne | BinOpKind::Eq = op.node; |
| 100 | + then { |
| 101 | + // println!("{:#?}, {:#?}", expr, then); |
| 102 | + self.if_statements.push((left, right)); |
| 103 | + } |
| 104 | + } |
| 105 | + walk_expr(self, expr); |
| 106 | + } |
| 107 | +} |
0 commit comments