From e6d4656805964c1eca1d7b797fae4c42f8db6c08 Mon Sep 17 00:00:00 2001 From: Jose Garcia Crosta Date: Tue, 16 Apr 2024 16:44:47 -0300 Subject: [PATCH] Update utils --- utils/src/lib.rs | 2 +- utils/src/soroban_utils/mod.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 4b37f939..0fa3186c 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -4,4 +4,4 @@ mod function_call_visitor; pub use function_call_visitor::FunctionCallVisitor; mod soroban_utils; -pub use soroban_utils::is_soroban_function; +pub use soroban_utils::*; diff --git a/utils/src/soroban_utils/mod.rs b/utils/src/soroban_utils/mod.rs index ec10c67b..67c2d053 100644 --- a/utils/src/soroban_utils/mod.rs +++ b/utils/src/soroban_utils/mod.rs @@ -1,18 +1,19 @@ extern crate rustc_lint; +extern crate rustc_middle; extern crate rustc_span; use std::collections::HashSet; use rustc_lint::LateContext; +use rustc_middle::ty::{Ty, TyKind}; use rustc_span::def_id::DefId; +/// Constants defining the fully qualified names of Soroban types. +const SOROBAN_ENV: &str = "soroban_sdk::Env"; +const SOROBAN_ADDRESS: &str = "soroban_sdk::Address"; + /// Determines whether a function defined by its `DefId` is part of a Soroban contract implementation. /// -/// This function checks if the provided `DefId` corresponds to a function that is part of an -/// implementation marked with the `#[contractimpl]` attribute specific to Soroban contracts. -/// It does this by comparing the function's path to a set of known patterns that are associated -/// with Soroban contract implementations. -/// /// # Parameters /// - `cx`: The context from the compiler's late lint pass. /// - `checked_functions`: A set containing strings that represent function names. @@ -52,3 +53,21 @@ pub fn is_soroban_function( .iter() .all(|pattern| checked_functions.contains(pattern)) } + +/// Checks if the provided type is a Soroban environment (`soroban_sdk::Env`). +pub fn is_soroban_env(cx: &LateContext<'_>, expr_type: Ty<'_>) -> bool { + match expr_type.kind() { + TyKind::Adt(adt_def, _) => cx.tcx.def_path_str(adt_def.did()).contains(SOROBAN_ENV), + TyKind::Ref(_, ty, _) => is_soroban_env(cx, *ty), + _ => false, + } +} + +/// Checks if the provided type is a Soroban Address (`soroban_sdk::Address`). +pub fn is_soroban_address(cx: &LateContext<'_>, expr_type: Ty<'_>) -> bool { + match expr_type.kind() { + TyKind::Adt(adt_def, _) => cx.tcx.def_path_str(adt_def.did()).contains(SOROBAN_ADDRESS), + TyKind::Ref(_, ty, _) => is_soroban_address(cx, *ty), + _ => false, + } +}