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 Dcl16C;
impl Dcl16C {
#[allow(dead_code)]
pub fn new() -> Self {
Dcl16C
}
fn check_node<'a>(
&self,
node: &Node<'a>,
source: &'a str,
violations: &mut Vec<RuleViolation>,
) {
if node.kind() == "number_literal" {
let text = get_node_text(node, source);
if self.has_lowercase_long_suffix(text) {
let suggested = self.fix_lowercase_suffix(text);
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
message: format!(
"Integer literal '{}' uses lowercase 'l' suffix which can be confused with digit '1'",
text
),
severity: self.severity(),
file_path: String::new(),
suggestion: Some(format!(
"Use uppercase 'L': {}",
suggested
)),
requires_manual_review: None,
});
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
fn has_lowercase_long_suffix(&self, text: &str) -> bool {
let text = text.trim_end_matches('u').trim_end_matches('U');
text.ends_with("ll") || (text.ends_with('l') && !text.ends_with('L'))
}
fn fix_lowercase_suffix(&self, text: &str) -> String {
let mut result = text.to_string();
let has_u_suffix = result.ends_with('u') || result.ends_with('U');
let u_suffix = if has_u_suffix {
let suffix = result.chars().last().unwrap();
result.pop();
Some(suffix)
} else {
None
};
if result.ends_with("ll") {
result = result[..result.len() - 2].to_string() + "LL";
} else if result.ends_with('l') {
result = result[..result.len() - 1].to_string() + "L";
}
if let Some(u) = u_suffix {
result.push(u);
}
result
}
}
impl CertRule for Dcl16C {
fn rule_id(&self) -> &'static str {
"DCL16-C"
}
fn description(&self) -> &'static str {
"Use \"L,\" not \"l,\" to indicate a long value"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"DCL16-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}