-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
61 additions
and
265 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[toolchain] | ||
channel = "nightly-2023-12-16" | ||
components = ["llvm-tools-preview", "rustc-dev"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
extern crate rustc_lint; | ||
extern crate rustc_span; | ||
|
||
use std::collections::HashSet; | ||
|
||
use rustc_lint::LateContext; | ||
use rustc_span::def_id::DefId; | ||
|
||
/// 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. | ||
/// - `function_def_id`: The `DefId` of the current function being analyzed. | ||
/// | ||
/// # Returns | ||
/// - `true` if the function belongs to a Soroban contract implementation. | ||
/// - `false` if the function does not belong to a Soroban contract implementation. | ||
pub fn is_soroban_function( | ||
cx: &LateContext<'_>, | ||
checked_functions: &HashSet<String>, | ||
function_def_id: &DefId, | ||
) -> bool { | ||
let def_path_str = cx.tcx.def_path_str(*function_def_id); | ||
let mut parts = def_path_str.rsplitn(2, "::"); | ||
|
||
// Safely extract function_name and contract_path, checking for empty strings | ||
let function_name = match parts.next() { | ||
Some(name) if !name.is_empty() => name, | ||
_ => return false, // Return false if function_name is None or empty | ||
}; | ||
let contract_path = match parts.next() { | ||
Some(path) if !path.is_empty() => path, | ||
_ => return false, // Return false if contract_path is None or empty | ||
}; | ||
|
||
// Define the patterns to check against | ||
let patterns = [ | ||
format!("{}Client::<'a>::try_{}", contract_path, function_name), | ||
format!("{}::{}", contract_path, function_name), | ||
format!("{}::spec_xdr_{}", contract_path, function_name), | ||
format!("{}Client::<'a>::{}", contract_path, function_name), | ||
]; | ||
|
||
// Check if all defined patterns are contained within the checked_functions | ||
patterns | ||
.iter() | ||
.all(|pattern| checked_functions.contains(pattern)) | ||
} |