use crate::adapters::analyzers::iosp::{ComplexityMetrics, FunctionAnalysis};
use crate::config::sections::ComplexityConfig;
pub(super) fn triggered_targets(
f: &FunctionAnalysis,
c: &ComplexityMetrics,
cx: &ComplexityConfig,
test_max_lines: usize,
) -> Vec<(&'static str, Option<f64>)> {
let mut out = Vec::new();
if c.cognitive_complexity > cx.max_cognitive {
out.push(("max_cognitive", Some(c.cognitive_complexity as f64)));
}
if c.cyclomatic_complexity > cx.max_cyclomatic {
out.push(("max_cyclomatic", Some(c.cyclomatic_complexity as f64)));
}
if c.max_nesting > cx.max_nesting_depth {
out.push(("max_nesting_depth", Some(c.max_nesting as f64)));
}
if exceeds_length(f, c, cx, test_max_lines) {
out.push(("max_function_lines", Some(c.function_lines as f64)));
}
if exceeds_unsafe(c, cx) {
out.push(("unsafe", None));
}
if exceeds_error_handling(f, c, cx) {
out.push(("error_handling", None));
}
out
}
fn exceeds_length(
f: &FunctionAnalysis,
c: &ComplexityMetrics,
cx: &ComplexityConfig,
test_max_lines: usize,
) -> bool {
let max = if f.is_test {
test_max_lines
} else {
cx.max_function_lines
};
c.function_lines > max
}
fn exceeds_unsafe(c: &ComplexityMetrics, cx: &ComplexityConfig) -> bool {
cx.detect_unsafe && c.unsafe_blocks > 0
}
fn exceeds_error_handling(
f: &FunctionAnalysis,
c: &ComplexityMetrics,
cx: &ComplexityConfig,
) -> bool {
if !cx.detect_error_handling || f.is_test {
return false;
}
let expect_threshold = if cx.allow_expect { 0 } else { 1 };
c.unwrap_count + c.panic_count + c.todo_count + c.expect_count.min(expect_threshold) > 0
}