use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use tree_sitter::Node;
pub struct Exp20C;
impl CertRule for Exp20C {
fn rule_id(&self) -> &'static str {
"EXP20-C"
}
fn description(&self) -> &'static str {
"Perform explicit tests to determine success, true and false, and equality"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"EXP20-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.find_implicit_tests(node, source, &mut violations);
violations
}
}
impl Exp20C {
fn find_implicit_tests(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "binary_expression" {
if let Some(operator) = node.child_by_field_name("operator") {
let op_text = get_node_text(&operator, source);
if op_text == "==" {
if let (Some(left), Some(right)) = (
node.child_by_field_name("left"),
node.child_by_field_name("right"),
) {
let left_text = get_node_text(&left, source);
let right_text = get_node_text(&right, source);
if left.kind() == "call_expression" && right_text == "1" {
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
message: format!(
"Testing '{}' for equality with 1. Functions may return values > 1. \
Use '!= 0' for boolean-like tests.",
left_text
),
severity: self.severity(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
file_path: String::new(),
suggestion: Some(format!("Consider using: {} != 0", left_text)),
requires_manual_review: None,
});
}
}
}
}
}
if node.kind() == "unary_expression" {
if let Some(operator) = node.child_by_field_name("operator") {
let op_text = get_node_text(&operator, source);
if op_text == "!" {
if let Some(argument) = node.child_by_field_name("argument") {
if argument.kind() == "call_expression" {
let func_text = get_node_text(&argument, source);
if self.is_validation_function(&func_text) {
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
message: format!(
"Implicit boolean test on '{}'. May be unclear what constitutes success or failure.",
func_text
),
severity: self.severity(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
file_path: String::new(),
suggestion: Some(format!(
"Use explicit test: {} == 0 or {} != 0 depending on success convention",
func_text, func_text
)),
requires_manual_review: None,
});
}
}
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.find_implicit_tests(&child, source, violations);
}
}
}
fn is_validation_function(&self, func_text: &str) -> bool {
func_text.contains("validate")
|| func_text.contains("Validate")
|| func_text.contains("check")
|| func_text.contains("Check")
|| func_text.contains("verify")
|| func_text.contains("Verify")
|| func_text.contains("strcmp")
|| func_text.contains("strncmp")
|| func_text.contains("memcmp")
|| func_text.contains("wcscmp")
|| func_text.contains("wcsncmp")
}
}