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 Exp00C;
impl CertRule for Exp00C {
fn rule_id(&self) -> &'static str {
"EXP00-C"
}
fn description(&self) -> &'static str {
"Use parentheses for precedence of operation"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"EXP00-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
if node.kind() == "binary_expression" {
if let Some(operator_node) = node.child_by_field_name("operator") {
let operator = get_node_text(&operator_node, source);
if is_comparison_operator(operator) {
if let Some(left) = node.child_by_field_name("left") {
if has_unparenthesized_bitwise_operator(&left, source) {
report_violation(&left, source, &mut violations);
}
}
if let Some(right) = node.child_by_field_name("right") {
if has_unparenthesized_bitwise_operator(&right, source) {
report_violation(&right, source, &mut violations);
}
}
}
else if is_bitwise_operator(operator) {
if let Some(left) = node.child_by_field_name("left") {
if contains_comparison_operator(&left, source) {
report_violation(node, source, &mut violations);
}
}
if let Some(right) = node.child_by_field_name("right") {
if contains_comparison_operator(&right, source) {
report_violation(node, source, &mut violations);
}
}
if operator == "&" || operator == "|" || operator == "^" {
if let Some(right) = node.child_by_field_name("right") {
let right_text = get_node_text(&right, source).trim();
if right_text == "0" {
report_violation(node, source, &mut violations);
}
}
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
violations.extend(self.check(&child, source));
}
}
violations
}
}
fn is_comparison_operator(op: &str) -> bool {
matches!(op, "==" | "!=" | "<" | ">" | "<=" | ">=")
}
fn is_bitwise_operator(op: &str) -> bool {
matches!(op, "&" | "|" | "^" | "<<" | ">>")
}
fn has_unparenthesized_bitwise_operator(node: &Node, source: &str) -> bool {
match node.kind() {
"binary_expression" => {
if let Some(operator_node) = node.child_by_field_name("operator") {
let operator = get_node_text(&operator_node, source);
is_bitwise_operator(operator)
} else {
false
}
}
"parenthesized_expression" => {
false
}
_ => false,
}
}
fn contains_comparison_operator(node: &Node, source: &str) -> bool {
match node.kind() {
"binary_expression" => {
if let Some(operator_node) = node.child_by_field_name("operator") {
let operator = get_node_text(&operator_node, source);
if is_comparison_operator(operator) {
return true;
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if contains_comparison_operator(&child, source) {
return true;
}
}
}
false
}
"parenthesized_expression" => {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if contains_comparison_operator(&child, source) {
return true;
}
}
}
false
}
_ => {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if contains_comparison_operator(&child, source) {
return true;
}
}
}
false
}
}
}
fn report_violation(node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
let start_point = node.start_position();
let node_text = get_node_text(node, source);
violations.push(RuleViolation {
rule_id: "EXP00-C".to_string(),
severity: Severity::Low,
message: format!(
"Use parentheses to clarify operator precedence: '{}'",
node_text
),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some("Add parentheses to enforce the intended order of operations".to_string()),
..Default::default()
});
}