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
//! Analyze Dead Code Example
//!
//! This example demonstrates how to use pmat's dead code analysis command
//! with the new --fail-on-violation flag for CI/CD integration.
//!
//! Run with: `cargo run --example analyze_dead_code`
use anyhow::Result;
use pmat::cli::handlers::handle_analyze_dead_code;
use pmat::cli::DeadCodeOutputFormat;
use std::path::PathBuf;
#[tokio::main]
async fn main() -> Result<()> {
println!("🔍 Analyze Dead Code Example\n");
// Example 1: Basic dead code analysis
println!("Example 1: Basic dead code analysis");
println!("{}", "=".repeat(60));
let result = handle_analyze_dead_code(
PathBuf::from("."),
DeadCodeOutputFormat::Summary,
Some(10), // Top 10 files
true, // Include unreachable code
5, // Min 5 dead lines to report
false, // Exclude tests
None, // Output to stdout
false, // Don't fail on violation
15.0, // Default max percentage
60, // Timeout in seconds
vec![], // Include patterns
vec![], // Exclude patterns
8, // Max depth
)
.await;
match result {
Ok(_) => println!("✅ Dead code analysis completed\n"),
Err(e) => println!("❌ Analysis failed: {}\n", e),
}
// Example 2: Strict CI/CD mode with custom threshold
println!("\nExample 2: CI/CD mode with strict dead code threshold");
println!("{}", "=".repeat(60));
let strict_result = handle_analyze_dead_code(
PathBuf::from("."),
DeadCodeOutputFormat::Json, // JSON for CI parsing
None, // Analyze all files
true, // Include unreachable
3, // Report even small dead code blocks
false, // Exclude tests
None, // Output to stdout
false, // Don't fail on violation in example (to avoid CI failure)
5.0, // Max 5% dead code allowed (strict!)
60, // Timeout in seconds
vec![], // Include patterns
vec![], // Exclude patterns
8, // Max depth
)
.await;
match strict_result {
Ok(_) => println!("✅ Dead code analysis completed!"),
Err(e) => {
println!("❌ Analysis failed: {}", e);
return Err(e);
}
}
println!(
"Note: In real CI, you would use --fail-on-violation to exit(1) on threshold exceeded"
);
// Example 3: Summary format with moderate threshold
println!("\nExample 3: Summary format with moderate threshold");
println!("{}", "=".repeat(60));
let summary_result = handle_analyze_dead_code(
PathBuf::from("."),
DeadCodeOutputFormat::Summary,
Some(5), // Top 5 files
false, // Don't include unreachable
10, // Only report significant dead code
false, // Exclude tests
None,
false, // Don't fail
10.0, // 10% threshold (moderate)
60, // Timeout in seconds
vec![], // Include patterns
vec![], // Exclude patterns
8, // Max depth
)
.await;
match summary_result {
Ok(_) => println!("✅ Summary analysis complete!"),
Err(e) => println!("❌ Summary analysis failed: {}", e),
}
// Example 4: Save results to file
println!("\nExample 4: Save dead code report to file");
println!("{}", "=".repeat(60));
let output_path = PathBuf::from("dead-code-report.json");
let file_result = handle_analyze_dead_code(
PathBuf::from("."),
DeadCodeOutputFormat::Json,
None, // All files
true, // Include unreachable
5, // Standard threshold
true, // Include tests this time
Some(output_path.clone()),
false,
15.0, // Default threshold
60, // Timeout in seconds
vec![], // Include patterns
vec![], // Exclude patterns
8, // Max depth
)
.await;
match file_result {
Ok(_) => println!("✅ Dead code report saved to: {}", output_path.display()),
Err(e) => println!("❌ Failed to save report: {}", e),
}
// Example 5: GitHub Actions usage
println!("\nExample 5: GitHub Actions CI integration");
println!("{}", "=".repeat(60));
println!("In your GitHub Actions workflow, use:");
println!("```yaml");
println!("- name: Check for dead code");
println!(" run: |");
println!(" pmat analyze dead-code \\");
println!(" --max-percentage 10.0 \\");
println!(" --fail-on-violation \\");
println!(" --format json \\");
println!(" --include-unreachable");
println!("```");
println!("This ensures your codebase maintains less than 10% dead code.");
println!("\n🎉 Dead code analysis examples completed!");
Ok(())
}