garbage_code_hunter/rules/
mod.rs1use std::path::Path;
2use syn::File;
3
4use crate::analyzer::CodeIssue;
5
6pub mod advanced_rust;
7pub mod code_smells;
8pub mod complexity;
9pub mod comprehensive_rust;
10pub mod duplication;
11pub mod file_structure;
12pub mod garbage_naming;
13pub mod naming;
14pub mod rust_patterns;
15pub mod rust_specific;
16pub mod student_code;
17
18pub trait Rule {
19 #[allow(dead_code)]
20 fn name(&self) -> &'static str;
21 fn check(
22 &self,
23 file_path: &Path,
24 syntax_tree: &File,
25 content: &str,
26 lang: &str,
27 ) -> Vec<CodeIssue>;
28}
29
30pub struct RuleEngine {
31 rules: Vec<Box<dyn Rule>>,
32}
33
34impl Default for RuleEngine {
35 fn default() -> Self {
36 Self::new()
37 }
38}
39
40impl RuleEngine {
41 pub fn new() -> Self {
42 let rules: Vec<Box<dyn Rule>> = vec![
43 Box::new(naming::TerribleNamingRule),
45 Box::new(naming::SingleLetterVariableRule),
46 Box::new(garbage_naming::MeaninglessNamingRule),
48 Box::new(garbage_naming::HungarianNotationRule),
49 Box::new(garbage_naming::AbbreviationAbuseRule),
50 Box::new(student_code::PrintlnDebuggingRule),
52 Box::new(student_code::PanicAbuseRule),
53 Box::new(student_code::TodoCommentRule),
54 Box::new(code_smells::MagicNumberRule),
56 Box::new(code_smells::GodFunctionRule),
57 Box::new(code_smells::CommentedCodeRule),
58 Box::new(code_smells::DeadCodeRule),
59 Box::new(rust_patterns::StringAbuseRule),
61 Box::new(rust_patterns::VecAbuseRule),
62 Box::new(rust_patterns::IteratorAbuseRule),
63 Box::new(rust_patterns::MatchAbuseRule),
64 Box::new(complexity::DeepNestingRule),
65 Box::new(complexity::LongFunctionRule),
66 Box::new(duplication::CodeDuplicationRule),
67 Box::new(rust_specific::UnwrapAbuseRule),
68 Box::new(rust_specific::UnnecessaryCloneRule),
69 Box::new(advanced_rust::ComplexClosureRule),
71 Box::new(advanced_rust::LifetimeAbuseRule),
72 Box::new(advanced_rust::TraitComplexityRule),
73 Box::new(advanced_rust::GenericAbuseRule),
74 Box::new(comprehensive_rust::ChannelAbuseRule),
76 Box::new(comprehensive_rust::AsyncAbuseRule),
77 Box::new(comprehensive_rust::DynTraitAbuseRule),
78 Box::new(comprehensive_rust::UnsafeAbuseRule),
79 Box::new(comprehensive_rust::FFIAbuseRule),
80 Box::new(comprehensive_rust::MacroAbuseRule),
81 Box::new(comprehensive_rust::ModuleComplexityRule),
82 Box::new(comprehensive_rust::PatternMatchingAbuseRule),
83 Box::new(comprehensive_rust::ReferenceAbuseRule),
84 Box::new(comprehensive_rust::BoxAbuseRule),
85 Box::new(comprehensive_rust::SliceAbuseRule),
86 Box::new(file_structure::FileStructureRule),
88 Box::new(file_structure::ImportChaosRule),
89 Box::new(file_structure::ModuleNestingRule),
90 ];
91
92 Self { rules }
93 }
94
95 pub fn check_file(
96 &self,
97 file_path: &Path,
98 syntax_tree: &File,
99 content: &str,
100 lang: &str,
101 ) -> Vec<CodeIssue> {
102 let mut issues = Vec::new();
103
104 for rule in &self.rules {
105 issues.extend(rule.check(file_path, syntax_tree, content, lang));
106 }
107
108 issues
109 }
110}