Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Vara Prasad Bandaru committed Feb 7, 2024
1 parent 8d6613b commit 236b611
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lints/missing_owner_check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use if_chain::if_chain;
use rustc_hir::{
def_id::LocalDefId,
intravisit::{walk_expr, FnKind, Visitor},
Body, Expr, ExprKind, FnDecl, QPath, BinOpKind,
BinOpKind, Body, Expr, ExprKind, FnDecl, QPath,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
Expand Down Expand Up @@ -71,7 +71,9 @@ impl<'tcx> LateLintPass<'tcx> for MissingOwnerCheck {
if !span.from_expansion() {
let accounts = get_referenced_accounts(cx, body);
for account_expr in accounts {
if !contains_owner_use(cx, body, account_expr) && !contains_key_check(cx, body, account_expr) {
if !contains_owner_use(cx, body, account_expr)
&& !contains_key_check(cx, body, account_expr)
{
span_lint(
cx,
MISSING_OWNER_CHECK,
Expand Down Expand Up @@ -107,7 +109,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for AccountUses<'cx, 'tcx> {
if_chain! {
// s3v3ru5: the following check removes duplicate warnings where lint would report both `x` and `x.clone()` expressions.
// ignore `clone()` expressions
if !is_expr_method_call(self.cx, expr, &paths::CORE_CLONE).is_some();
if is_expr_method_call(self.cx, expr, &paths::CORE_CLONE).is_none();
let ty = self.cx.typeck_results().expr_ty(expr);
if match_type(self.cx, ty, &paths::SOLANA_PROGRAM_ACCOUNT_INFO);
if !is_expr_local_variable(expr);
Expand Down Expand Up @@ -144,7 +146,7 @@ fn is_expr_local_variable<'tcx>(expr: &'tcx Expr<'tcx>) -> bool {
fn is_safe_to_account_info<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
if_chain! {
// is the expression method call `to_account_info()`
if let Some(recv) = is_expr_method_call(cx, expr, &paths::ANCHOR_LANG_TO_ACCOUNT_INFO);
if let Some(recv) = is_expr_method_call(cx, expr, &paths::ANCHOR_LANG_TO_ACCOUNT_INFO);
if let ty::Ref(_, recv_ty, _) = cx.typeck_results().expr_ty_adjusted(recv).kind();
if let ty::Adt(adt_def, _) = recv_ty.kind();
// smoelius:
Expand Down Expand Up @@ -202,7 +204,9 @@ fn contains_owner_use<'tcx>(
body: &'tcx Body<'tcx>,
account_expr: &Expr<'tcx>,
) -> bool {
visit_expr_no_bodies(body.value, |expr| uses_given_field(cx, expr, account_expr, "owner"))
visit_expr_no_bodies(body.value, |expr| {
uses_given_field(cx, expr, account_expr, "owner")
})
}

/// Checks if `expr` is references `field` on `account_expr`
Expand Down Expand Up @@ -247,15 +251,15 @@ fn calls_method_on_expr<'tcx>(
}
}


// Return true if the expr access key of account_expr(AccountInfo)
fn expr_accesses_key<'tcx>(
cx: &LateContext<'tcx>,
expr: &Expr<'tcx>,
account_expr: &Expr<'tcx>,
) -> bool {
// Anchor AccountInfo: `.key()` and Solana AccountInfo: `.key` field.
calls_method_on_expr(cx, expr, account_expr, &paths::ANCHOR_LANG_KEY) || uses_given_field(cx, expr, account_expr, "key")
calls_method_on_expr(cx, expr, account_expr, &paths::ANCHOR_LANG_KEY)
|| uses_given_field(cx, expr, account_expr, "key")
}

fn contains_key_check<'tcx>(
Expand Down

0 comments on commit 236b611

Please sign in to comment.