1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
impl DeadCodeAnalyzer {
/// Analyze dead code with ranking functionality
///
/// Performs comprehensive dead code analysis on a project directory,
/// identifying unused functions, classes, and other code elements.
/// Returns ranked results with scoring and filtering capabilities.
///
/// # Examples
///
/// ```
/// use pmat::services::dead_code_analyzer::DeadCodeAnalyzer;
/// use pmat::models::dead_code::DeadCodeAnalysisConfig;
/// use std::path::Path;
/// use tempfile::TempDir;
/// use std::fs;
///
/// # tokio_test::block_on(async {
/// let temp_dir = TempDir::new().expect("internal error");
/// let test_file = temp_dir.path().join("test.rs");
/// fs::write(&test_file, r#"
/// fn used_function() -> i32 { 42 }
/// fn unused_function() -> i32 { 100 }
/// fn main() { println!("{}", used_function()); }
/// "#).expect("internal error");
///
/// let mut analyzer = DeadCodeAnalyzer::new(1000);
/// let config = DeadCodeAnalysisConfig::default();
/// let result = analyzer.analyze_with_ranking(temp_dir.path(), config).await.expect("internal error");
///
/// assert!(result.summary.total_files_analyzed > 0);
/// # });
/// ```
pub async fn analyze_with_ranking(
&mut self,
project_path: &Path,
config: crate::models::dead_code::DeadCodeAnalysisConfig,
) -> anyhow::Result<crate::models::dead_code::DeadCodeRankingResult> {
use crate::services::context::analyze_project_for_dead_code;
use chrono::Utc;
// 1. Use optimized dead code analysis that only scans relevant source files
let project_context = analyze_project_for_dead_code(project_path, "rust").await?;
// Track total files analyzed
let total_files_in_project = project_context.files.len();
// 2. (DAG building not needed for this implementation)
// 3. Perform dead code analysis using the project context directly
let report = self.analyze_project_context(&project_context)?;
// 4. Aggregate by file and create ranking metrics
let mut file_metrics = self.aggregate_by_file(&report, &project_context, &config)?;
// 5. Calculate scores and sort
for metrics in &mut file_metrics {
metrics.calculate_score();
}
file_metrics.sort_by(|a, b| {
b.dead_score
.partial_cmp(&a.dead_score)
.unwrap_or(std::cmp::Ordering::Equal)
});
// 6. Apply filters
if !config.include_tests {
file_metrics.retain(|f| !f.path.contains("test"));
}
file_metrics.retain(|f| f.dead_lines >= config.min_dead_lines);
let mut summary = crate::models::dead_code::DeadCodeSummary::from_files(&file_metrics);
// Update total files analyzed to reflect actual project files
summary.total_files_analyzed = total_files_in_project;
Ok(crate::models::dead_code::DeadCodeRankingResult {
summary,
ranked_files: file_metrics,
analysis_timestamp: Utc::now(),
config,
})
}
/// Aggregate dead code by file
fn aggregate_by_file(
&self,
report: &DeadCodeReport,
project_context: &crate::services::context::ProjectContext,
config: &crate::models::dead_code::DeadCodeAnalysisConfig,
) -> anyhow::Result<Vec<crate::models::dead_code::FileDeadCodeMetrics>> {
use std::collections::HashMap;
let mut file_map: HashMap<String, crate::models::dead_code::FileDeadCodeMetrics> =
HashMap::new();
// Process dead functions
for dead_item in &report.dead_functions {
let file_path = dead_item.file_path.clone();
let entry = file_map
.entry(file_path.clone())
.or_insert_with(|| crate::models::dead_code::FileDeadCodeMetrics::new(file_path));
entry.add_item(crate::models::dead_code::DeadCodeItem {
item_type: crate::models::dead_code::DeadCodeType::Function,
name: dead_item.name.clone(),
line: dead_item.line_number,
reason: dead_item.reason.clone(),
});
}
// Process dead classes
for dead_item in &report.dead_classes {
let file_path = dead_item.file_path.clone();
let entry = file_map
.entry(file_path.clone())
.or_insert_with(|| crate::models::dead_code::FileDeadCodeMetrics::new(file_path));
entry.add_item(crate::models::dead_code::DeadCodeItem {
item_type: crate::models::dead_code::DeadCodeType::Class,
name: dead_item.name.clone(),
line: dead_item.line_number,
reason: dead_item.reason.clone(),
});
}
// Process dead variables
for dead_item in &report.dead_variables {
let file_path = dead_item.file_path.clone();
let entry = file_map
.entry(file_path.clone())
.or_insert_with(|| crate::models::dead_code::FileDeadCodeMetrics::new(file_path));
entry.add_item(crate::models::dead_code::DeadCodeItem {
item_type: crate::models::dead_code::DeadCodeType::Variable,
name: dead_item.name.clone(),
line: dead_item.line_number,
reason: dead_item.reason.clone(),
});
}
// Process unreachable blocks if requested
if config.include_unreachable {
for unreachable in &report.unreachable_code {
let file_path = unreachable.file_path.clone();
let entry = file_map.entry(file_path.clone()).or_insert_with(|| {
crate::models::dead_code::FileDeadCodeMetrics::new(file_path)
});
// Count unreachable lines
let unreachable_lines = unreachable.end_line - unreachable.start_line + 1;
entry.dead_lines += unreachable_lines as usize;
entry.unreachable_blocks += 1;
entry.add_item(crate::models::dead_code::DeadCodeItem {
item_type: crate::models::dead_code::DeadCodeType::UnreachableCode,
name: format!(
"unreachable block {}-{}",
unreachable.start_line, unreachable.end_line
),
line: unreachable.start_line,
reason: unreachable.reason.clone(),
});
}
}
// Calculate total lines and percentages for each file
for (file_path, metrics) in &mut file_map {
// Try to get total lines from the project context or read from file
if let Some(file_info) = project_context.files.iter().find(|f| f.path == *file_path) {
// Estimate total lines from file info (we don't have content, so we'll estimate)
metrics.total_lines = file_info.items.len() * 10; // Rough estimate: 10 lines per item
} else {
// Fallback: read file directly
if let Ok(content) = std::fs::read_to_string(file_path) {
metrics.total_lines = content.lines().count();
}
}
metrics.update_percentage();
}
Ok(file_map.into_values().collect())
}
}