rust-doctor 0.1.18

A unified code health tool for Rust — scan, score, and fix your codebase
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
pub mod async_rules;
pub mod complexity;
pub mod error_handling;
pub mod framework;
pub mod performance;
pub mod security;

use crate::cache::{self, ScanCache};
use crate::diagnostics::{Category, Diagnostic, Severity};
use crate::scanner::{self, AnalysisPass};
use globset::GlobSet;
use rayon::prelude::*;
use std::panic::{self, AssertUnwindSafe};
use std::path::Path;

// ─── Shared helpers for test-code detection ─────────────────────────────────

/// Check if an attribute list contains `#[test]`.
pub fn has_test_attr(attrs: &[syn::Attribute]) -> bool {
    attrs.iter().any(|attr| attr.path().is_ident("test"))
}

/// Check if an attribute list contains `#[cfg(test)]`.
pub fn has_cfg_test(attrs: &[syn::Attribute]) -> bool {
    attrs.iter().any(|attr| {
        if !attr.path().is_ident("cfg") {
            return false;
        }
        attr.parse_args::<syn::Ident>()
            .is_ok_and(|ident| ident == "test")
    })
}

/// Trait for custom AST-based rules that clippy doesn't cover.
///
/// Rules must be `Send + Sync` for parallel file processing.
/// Metadata methods (`description`, `fix_hint`) co-locate documentation
/// with the implementation, so adding a new rule only requires changes
/// in one place.
#[expect(
    dead_code,
    reason = "helper methods used by implementors in sub-modules"
)]
pub trait CustomRule: Send + Sync {
    /// Unique rule identifier (e.g. "unwrap-in-production").
    fn name(&self) -> &'static str;

    /// Category this rule belongs to.
    fn category(&self) -> Category;

    /// Default severity for findings from this rule.
    fn severity(&self) -> Severity;

    /// Human-readable description of what this rule detects.
    fn description(&self) -> &'static str;

    /// Actionable fix guidance for violations found by this rule.
    fn fix_hint(&self) -> &'static str;

    /// Whether this rule is enabled by default. Rules returning `false` are
    /// opt-in only — they run only when not listed in `ignore.rules` AND the
    /// scanner explicitly includes opt-in rules (e.g., via `--strict` or config).
    fn default_enabled(&self) -> bool {
        true
    }

    /// Check a parsed Rust file and return diagnostics.
    fn check_file(&self, syntax: &syn::File, path: &Path) -> Vec<Diagnostic>;

    /// Helper to construct a `Diagnostic` using this rule's metadata.
    fn diagnostic(
        &self,
        path: &Path,
        message: String,
        help: Option<String>,
        line: Option<u32>,
        column: Option<u32>,
    ) -> Diagnostic {
        Diagnostic {
            file_path: path.to_path_buf(),
            rule: self.name().to_string(),
            category: self.category(),
            severity: self.severity(),
            message,
            help,
            line,
            column,
            fix: None,
        }
    }
}

/// The rule engine: runs custom rules against all `.rs` files in parallel.
pub struct RuleEngine {
    rules: Vec<Box<dyn CustomRule>>,
}

impl RuleEngine {
    /// Create a new rule engine with the given rules.
    pub fn new(rules: Vec<Box<dyn CustomRule>>) -> Self {
        Self { rules }
    }

    /// Scan with full config context for cache key computation.
    ///
    /// This is the main implementation; [`scan`](Self::scan) delegates here
    /// with empty config slices for backward compatibility.
    pub fn scan_with_config(
        &self,
        project_root: &Path,
        ignore_files: &[String],
        ignore_rules: &[String],
        enable_rules: &[String],
    ) -> Vec<Diagnostic> {
        if self.rules.is_empty() {
            return vec![];
        }

        // Collect .rs files
        let src_dir = project_root.join("src");
        if !src_dir.is_dir() {
            return vec![];
        }

        let files = scanner::collect_rs_files(&src_dir);
        if files.is_empty() {
            return vec![];
        }

        // Build ignore glob set
        let ignore_set = build_ignore_set(ignore_files);

        // Compute config hash including active rule names so cache
        // invalidates when rules are added or removed.
        let active_rule_names: Vec<&str> = self.rules.iter().map(|r| r.name()).collect();
        let config_hash = cache::compute_config_hash(
            ignore_rules,
            ignore_files,
            enable_rules,
            &active_rule_names,
        );
        let mut scan_cache = ScanCache::load(project_root, &config_hash)
            .unwrap_or_else(|| ScanCache::new(config_hash.clone()));

        // Read all files into memory, filtering ignored paths
        let file_contents: Vec<(std::path::PathBuf, String)> = files
            .into_iter()
            .filter_map(|file_path| {
                let rel_path = file_path.strip_prefix(project_root).unwrap_or(&file_path);
                if let Ok(ref set) = ignore_set
                    && set.is_match(rel_path)
                {
                    return None;
                }
                match std::fs::read_to_string(&file_path) {
                    Ok(content) => Some((file_path, content)),
                    Err(e) => {
                        eprintln!("Warning: could not read '{}': {e}", file_path.display());
                        None
                    }
                }
            })
            .collect();

        // Partition into fresh (cache hit) and stale (need scanning) files
        let (fresh, stale): (Vec<_>, Vec<_>) =
            file_contents.iter().partition(|(file_path, content)| {
                let rel_path = file_path.strip_prefix(project_root).unwrap_or(file_path);
                scan_cache.is_fresh(rel_path, content)
            });

        // Collect cached diagnostics for fresh files
        let mut all_diagnostics: Vec<Diagnostic> = fresh
            .iter()
            .flat_map(|(file_path, _content)| {
                let rel_path = file_path.strip_prefix(project_root).unwrap_or(file_path);
                scan_cache
                    .get_cached_diagnostics(rel_path)
                    .unwrap_or(&[])
                    .to_vec()
            })
            .collect();

        // Process stale files in parallel with rayon
        let stale_results: Vec<(std::path::PathBuf, String, Vec<Diagnostic>)> = stale
            .par_iter()
            .map(|&(file_path, content)| {
                let rel_path = file_path.strip_prefix(project_root).unwrap_or(file_path);

                let diagnostics = match syn::parse_file(content) {
                    Ok(syntax) => self
                        .rules
                        .iter()
                        .flat_map(|rule| run_rule_safely(rule.as_ref(), &syntax, rel_path))
                        .collect::<Vec<_>>(),
                    Err(e) => {
                        eprintln!("Warning: parse error in '{}': {e}", rel_path.display());
                        vec![]
                    }
                };

                (rel_path.to_path_buf(), content.clone(), diagnostics)
            })
            .collect();

        // Update the cache with newly scanned results
        for (rel_path, content, diagnostics) in stale_results {
            all_diagnostics.extend_from_slice(&diagnostics);
            scan_cache.update(&rel_path, &content, diagnostics);
        }

        // Persist the updated cache (best-effort)
        scan_cache.save(project_root);

        all_diagnostics
    }
}

/// Run a single rule with panic isolation.
fn run_rule_safely(rule: &dyn CustomRule, syntax: &syn::File, path: &Path) -> Vec<Diagnostic> {
    let result = panic::catch_unwind(AssertUnwindSafe(|| rule.check_file(syntax, path)));

    match result {
        Ok(diagnostics) => diagnostics,
        Err(payload) => {
            let msg = payload.downcast_ref::<&'static str>().map_or_else(
                || {
                    payload
                        .downcast_ref::<String>()
                        .map_or_else(|| "<non-string panic>".to_string(), String::clone)
                },
                |s| (*s).to_string(),
            );
            eprintln!(
                "Warning: rule '{}' panicked on '{}': {msg}",
                rule.name(),
                path.display()
            );
            vec![]
        }
    }
}

/// Return all custom rules across all categories.
/// Used to derive the rule registry and documentation at startup.
pub fn all_custom_rules() -> Vec<Box<dyn CustomRule>> {
    error_handling::all_rules()
        .into_iter()
        .chain(performance::all_rules())
        .chain(complexity::all_rules())
        .chain(security::all_rules())
        .chain(async_rules::all_rules())
        .chain(framework::all_rules())
        .collect()
}

/// Check if the tail of `actual` matches `pattern` exactly.
/// Used by async and framework rules to match blocking-call path segments.
#[inline]
pub fn segments_match(actual: &[&str], pattern: &[&str]) -> bool {
    actual.len() >= pattern.len() && actual.ends_with(pattern)
}

fn build_ignore_set(patterns: &[String]) -> Result<GlobSet, globset::Error> {
    scanner::build_glob_set(patterns)
}

/// Analysis pass that wraps the rule engine for the scan orchestrator.
pub struct RuleEnginePass {
    engine: RuleEngine,
    ignore_files: Vec<String>,
    ignore_rules: Vec<String>,
    enable_rules: Vec<String>,
}

impl RuleEnginePass {
    /// Create a new pass with full config context for incremental caching.
    pub fn with_config(
        rules: Vec<Box<dyn CustomRule>>,
        ignore_files: Vec<String>,
        ignore_rules: Vec<String>,
        enable_rules: Vec<String>,
    ) -> Self {
        Self {
            engine: RuleEngine::new(rules),
            ignore_files,
            ignore_rules,
            enable_rules,
        }
    }
}

impl AnalysisPass for RuleEnginePass {
    fn name(&self) -> &'static str {
        "custom rules"
    }

    fn run(&self, project_root: &Path) -> Result<Vec<Diagnostic>, crate::error::PassError> {
        Ok(self.engine.scan_with_config(
            project_root,
            &self.ignore_files,
            &self.ignore_rules,
            &self.enable_rules,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    // --- Test rule implementations ---

    struct CountFnRule;

    impl CustomRule for CountFnRule {
        fn name(&self) -> &'static str {
            "count-fn"
        }
        fn category(&self) -> Category {
            Category::Architecture
        }
        fn severity(&self) -> Severity {
            Severity::Warning
        }
        fn description(&self) -> &'static str {
            "test rule"
        }
        fn fix_hint(&self) -> &'static str {
            "test fix"
        }
        fn check_file(&self, syntax: &syn::File, path: &Path) -> Vec<Diagnostic> {
            let fn_count = syntax
                .items
                .iter()
                .filter(|item| matches!(item, syn::Item::Fn(_)))
                .count();
            if fn_count > 10 {
                vec![Diagnostic {
                    file_path: path.to_path_buf(),
                    rule: self.name().to_string(),
                    category: self.category(),
                    severity: self.severity(),
                    message: format!("File has {fn_count} functions (threshold: 10)"),
                    help: None,
                    line: None,
                    column: None,
                    fix: None,
                }]
            } else {
                vec![]
            }
        }
    }

    struct PanickingRule;

    impl CustomRule for PanickingRule {
        fn name(&self) -> &'static str {
            "panicking-rule"
        }
        fn category(&self) -> Category {
            Category::Correctness
        }
        fn severity(&self) -> Severity {
            Severity::Error
        }
        fn description(&self) -> &'static str {
            "test rule"
        }
        fn fix_hint(&self) -> &'static str {
            "test fix"
        }
        fn check_file(&self, _syntax: &syn::File, _path: &Path) -> Vec<Diagnostic> {
            panic!("intentional test panic");
        }
    }

    struct AlwaysWarnsRule;

    impl CustomRule for AlwaysWarnsRule {
        fn name(&self) -> &'static str {
            "always-warns"
        }
        fn category(&self) -> Category {
            Category::Style
        }
        fn severity(&self) -> Severity {
            Severity::Warning
        }
        fn description(&self) -> &'static str {
            "test rule"
        }
        fn fix_hint(&self) -> &'static str {
            "test fix"
        }
        fn check_file(&self, _syntax: &syn::File, path: &Path) -> Vec<Diagnostic> {
            vec![Diagnostic {
                file_path: path.to_path_buf(),
                rule: self.name().to_string(),
                category: self.category(),
                severity: self.severity(),
                message: "Test warning".to_string(),
                help: None,
                line: None,
                column: None,
                fix: None,
            }]
        }
    }

    // --- Tests ---

    fn make_temp_project(files: &[(&str, &str)]) -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        let src_dir = dir.path().join("src");
        std::fs::create_dir_all(&src_dir).unwrap();
        for (filename, content) in files {
            let path = src_dir.join(filename);
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent).unwrap();
            }
            let mut f = std::fs::File::create(&path).unwrap();
            write!(f, "{content}").unwrap();
        }
        dir
    }

    #[test]
    fn test_rule_engine_with_no_rules() {
        let engine = RuleEngine::new(vec![]);
        let dir = make_temp_project(&[("main.rs", "fn main() {}")]);
        let diags = engine.scan_with_config(dir.path(), &[], &[], &[]);
        assert!(diags.is_empty());
    }

    #[test]
    fn test_rule_engine_no_src_dir() {
        let dir = tempfile::tempdir().unwrap();
        let engine = RuleEngine::new(vec![Box::new(AlwaysWarnsRule)]);
        let diags = engine.scan_with_config(dir.path(), &[], &[], &[]);
        assert!(diags.is_empty());
    }

    #[test]
    fn test_rule_engine_runs_rules_on_files() {
        let dir = make_temp_project(&[("main.rs", "fn main() {}")]);
        let engine = RuleEngine::new(vec![Box::new(AlwaysWarnsRule)]);
        let diags = engine.scan_with_config(dir.path(), &[], &[], &[]);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].rule, "always-warns");
    }

    #[test]
    fn test_rule_engine_multiple_files() {
        let dir = make_temp_project(&[
            ("main.rs", "fn main() {}"),
            ("lib.rs", "pub fn hello() {}"),
            ("utils.rs", "pub fn util() {}"),
        ]);
        let engine = RuleEngine::new(vec![Box::new(AlwaysWarnsRule)]);
        let diags = engine.scan_with_config(dir.path(), &[], &[], &[]);
        assert_eq!(diags.len(), 3);
    }

    #[test]
    fn test_rule_engine_catches_panics() {
        let dir = make_temp_project(&[("main.rs", "fn main() {}")]);
        let engine = RuleEngine::new(vec![Box::new(PanickingRule), Box::new(AlwaysWarnsRule)]);
        let diags = engine.scan_with_config(dir.path(), &[], &[], &[]);
        // PanickingRule panicked and was caught; AlwaysWarnsRule still ran
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].rule, "always-warns");
    }

    #[test]
    fn test_rule_engine_handles_parse_errors() {
        let dir = make_temp_project(&[("main.rs", "this is not valid rust {{{{")]);
        let engine = RuleEngine::new(vec![Box::new(AlwaysWarnsRule)]);
        let diags = engine.scan_with_config(dir.path(), &[], &[], &[]);
        // File couldn't be parsed, so no diagnostics
        assert!(diags.is_empty());
    }

    #[test]
    fn test_rule_engine_skips_ignored_files() {
        let dir = make_temp_project(&[
            ("main.rs", "fn main() {}"),
            ("generated/output.rs", "pub fn gen() {}"),
        ]);
        let engine = RuleEngine::new(vec![Box::new(AlwaysWarnsRule)]);
        let ignore = vec!["src/generated/**".to_string()];
        let diags = engine.scan_with_config(dir.path(), &ignore, &[], &[]);
        assert_eq!(diags.len(), 1);
        assert!(diags[0].file_path.to_string_lossy().contains("main.rs"));
    }

    #[test]
    fn test_rule_engine_on_self() {
        let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
        let engine = RuleEngine::new(vec![Box::new(CountFnRule)]);
        let _diags = engine.scan_with_config(manifest_dir, &[], &[], &[]);
        // CountFnRule only fires if a file has >10 functions, so may or may not find issues
    }

    #[test]
    fn test_collect_rs_files() {
        let dir = make_temp_project(&[("main.rs", ""), ("lib.rs", ""), ("sub/mod.rs", "")]);
        let files = scanner::collect_rs_files(&dir.path().join("src"));
        assert_eq!(files.len(), 3);
    }

    #[test]
    fn test_rule_engine_pass() {
        let dir = make_temp_project(&[("main.rs", "fn main() {}")]);
        let pass =
            RuleEnginePass::with_config(vec![Box::new(AlwaysWarnsRule)], vec![], vec![], vec![]);
        let result = pass.run(dir.path());
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 1);
    }
}