| 7 | use crate::analyzer::utils; |
| 8 | |
| 9 | pub fn string_error_optimization(source_unit: SourceUnit) -> HashSet<Loc> { |
| 10 | //Create a new hashset that stores the location of each optimization target identified |
| 11 | let mut optimization_locations: HashSet<Loc> = HashSet::new(); |
| 12 | |
| 13 | let solidity_version = utils::get_solidity_version_from_source_unit(source_unit.clone()) |
| 14 | .expect("Could not extract Solidity version from source unit."); |
| 15 | |
| 16 | if solidity_version.1 >= 8 && solidity_version.2 >= 4 { |
| 17 | //Extract the target nodes from the source_unit |
| 18 | let target_nodes = ast::extract_target_from_node(Target::FunctionCall, source_unit.into()); |
| 19 | |
| 20 | for node in target_nodes { |
| 21 | //We can use unwrap because Target::FunctionCall is an expression |
| 22 | let expression = node.expression().unwrap(); |
| 23 | |
| 24 | if let pt::Expression::FunctionCall(_, function_identifier, func_call_expressions) = |
| 25 | expression |
| 26 | { |
| 27 | //if the function call identifier is a variable |
| 28 | if let pt::Expression::Variable(identifier) = *function_identifier { |
| 29 | //if the identifier name is "require" |
| 30 | if identifier.name == "require".to_string() { |
| 31 | //If the require statement contains strings |
| 32 | if let Some(pt::Expression::StringLiteral(vec_string_literal)) = |
| 33 | func_call_expressions.last() |
| 34 | { |
| 35 | optimization_locations.insert(vec_string_literal[0].loc); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | //Return the identified optimization locations |
| 43 | optimization_locations |
| 44 | } |
| 45 | #[test] |
| 46 | fn test_string_error_optimization() { |
| 47 | //test when base solidiy version is > than 0.8.4 |