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 Exp02C;
impl CertRule for Exp02C {
fn rule_id(&self) -> &'static str {
"EXP02-C"
}
fn description(&self) -> &'static str {
"Be aware of the short-circuit behavior of the logical AND and OR operators"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"EXP02-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Exp02C {
fn check_node(&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 matches!(op_text, "&&" | "||") {
if let Some(right) = node.child_by_field_name("right") {
if let Some(left) = node.child_by_field_name("left") {
if self.is_guard_pattern(&left, source)
&& !self.has_mutation_side_effects(&right, source)
{
return;
}
if self.is_guard_pattern(&left, source)
&& Self::is_simple_decrement(&right)
{
return;
}
}
if self.has_side_effects(&right, source) {
let start_point = right.start_position();
let right_text = get_node_text(&right, source);
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Low,
message: format!(
"Side effect in right operand of '{}' operator may not execute due to short-circuit evaluation: '{}'",
op_text, right_text
),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some(
"Move side effects to separate statements before the logical expression".to_string()
),
..Default::default()
});
}
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
fn is_guard_pattern(&self, node: &Node, source: &str) -> bool {
match node.kind() {
"binary_expression" => {
if let Some(op) = node.child_by_field_name("operator") {
let op_text = get_node_text(&op, source);
if matches!(op_text, "==" | "!=" | "<" | ">" | "<=" | ">=") {
return true;
}
if matches!(op_text, "&" | "|" | "^") {
return true;
}
if matches!(op_text, "&&" | "||") {
let left_guard = node
.child_by_field_name("left")
.map(|l| self.is_guard_pattern(&l, source))
.unwrap_or(false);
let right_guard = node
.child_by_field_name("right")
.map(|r| self.is_guard_pattern(&r, source))
.unwrap_or(false);
return left_guard && right_guard;
}
}
false
}
"identifier"
| "unary_expression"
| "field_expression"
| "pointer_expression"
| "subscript_expression" => true,
"parenthesized_expression" => {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if matches!(child.kind(), "(" | ")") {
continue;
}
return self.is_guard_pattern(&child, source);
}
}
false
}
_ => false,
}
}
fn has_mutation_side_effects(&self, node: &Node, source: &str) -> bool {
match node.kind() {
"assignment_expression" | "update_expression" => return true,
_ => {}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.has_mutation_side_effects(&child, source) {
return true;
}
}
}
false
}
fn is_simple_decrement(node: &Node) -> bool {
match node.kind() {
"update_expression" => {
if let Some(arg) = node.child_by_field_name("argument") {
let is_decrement = (0..node.child_count())
.any(|i| node.child(i).map(|c| c.kind() == "--").unwrap_or(false));
return is_decrement && arg.kind() == "identifier";
}
false
}
"parenthesized_expression" => {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if !matches!(child.kind(), "(" | ")") {
return Self::is_simple_decrement(&child);
}
}
}
false
}
_ => false,
}
}
fn is_getter_in_field_access(&self, call_node: &Node) -> bool {
if let Some(parent) = call_node.parent() {
if parent.kind() == "field_expression" {
if let Some(arg) = parent.child_by_field_name("argument") {
return arg.id() == call_node.id();
}
}
}
false
}
fn has_side_effects(&self, node: &Node, source: &str) -> bool {
match node.kind() {
"call_expression" => {
if self.is_getter_in_field_access(node) {
return false;
}
true
}
"assignment_expression" => true,
"update_expression" => {
if let Some(operator) = node.child_by_field_name("operator") {
let op = get_node_text(&operator, source);
matches!(op, "++" | "--")
} else {
false
}
}
"compound_assignment_expr" => true,
_ => {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.has_side_effects(&child, source) {
return true;
}
}
}
false
}
}
}
}