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
use crate::config::ResolvedConfig;
use crate::diagnostics::{Diagnostic, ScanResult, Severity};
use crate::discovery::ProjectInfo;
use crate::{
    audit, clippy, config, coverage, deny, diff, geiger, machete, msrv, output, rules, scanner,
    semver_checks, suppression, workspace,
};
use rayon::prelude::*;
use std::path::PathBuf;
use std::time::Duration;

/// Derive custom rule names from the rule registry at runtime.
/// Includes the external "unused-dependency" rule which is not AST-based.
pub fn custom_rule_names() -> Vec<String> {
    let mut names: Vec<String> = rules::all_custom_rules()
        .iter()
        .map(|r| r.name().to_string())
        .collect();
    names.push("unused-dependency".to_string());
    names
}

/// Run a complete scan on a discovered Rust project.
///
/// This is the core scanning pipeline used by both the CLI and MCP server.
/// The caller is responsible for project discovery and config resolution.
///
/// Pipeline: validate → resolve roots → run passes (parallel) → dedup → diff filter → suppress → score
pub fn scan_project(
    project_info: &ProjectInfo,
    resolved: &ResolvedConfig,
    offline: bool,
    project_filter: &[String],
    suppress_spinner: bool,
) -> Result<ScanResult, crate::error::ScanError> {
    // Step 1: Verify ignored rules are known — warns on typos in config
    validate_config(resolved);

    // Step 2: Resolve workspace members or single project root
    let scan_roots = resolve_scan_roots(project_info, resolved, project_filter)?;
    log_project_info(project_info, resolved);

    // Step 3: Parse git diff if --diff was specified (narrows scope to changed files)
    let diff_context = resolve_diff_context(project_info, resolved);

    // Step 4: Run all analysis passes in parallel
    // Two levels of parallelism: rayon for scan roots, std::thread::scope for passes within a root
    let (mut all_diagnostics, total_source_files, all_skipped_passes, total_elapsed) = run_passes(
        project_info,
        resolved,
        &scan_roots,
        diff_context.as_ref(),
        offline,
        suppress_spinner,
    );

    // Step 5: Deduplicate — same rule+file+line from overlapping workspace scans = one diagnostic
    dedup_diagnostics(&mut all_diagnostics);

    // Step 6: In diff mode, drop diagnostics outside changed files
    if let Some(ref ctx) = diff_context {
        all_diagnostics = diff::filter_to_changed_files(all_diagnostics, &ctx.changed_files);
    }

    // Step 7: Apply inline suppressions (// rust-doctor-disable-next-line <rule>)
    let all_diagnostics = apply_suppressions(all_diagnostics, project_info, resolved);

    // Step 8: Calculate score and build the final result
    Ok(build_result(
        all_diagnostics,
        total_source_files,
        all_skipped_passes,
        total_elapsed,
    ))
}

// ---------------------------------------------------------------------------
// Pipeline stages
// ---------------------------------------------------------------------------

fn validate_config(resolved: &ResolvedConfig) {
    let mut known_rules: Vec<&str> = clippy::known_lint_names();
    let custom_names = custom_rule_names();
    known_rules.extend(custom_names.iter().map(String::as_str));
    config::validate_ignored_rules(&resolved.ignore_rules, &known_rules);
}

fn resolve_scan_roots(
    project_info: &ProjectInfo,
    resolved: &ResolvedConfig,
    project_filter: &[String],
) -> Result<Vec<PathBuf>, crate::error::ScanError> {
    if project_info.is_workspace {
        let members = workspace::resolve_members(&project_info.workspace_members, project_filter)?;
        if resolved.verbose {
            eprintln!(
                "Workspace: scanning {} of {} members",
                members.len(),
                project_info.member_count
            );
        }
        Ok(members.iter().map(|m| m.root_dir.clone()).collect())
    } else {
        if !project_filter.is_empty() {
            eprintln!("Warning: --project is only applicable to Cargo workspaces; ignoring");
        }
        Ok(vec![project_info.root_dir.clone()])
    }
}

fn log_project_info(project_info: &ProjectInfo, resolved: &ResolvedConfig) {
    if !resolved.verbose {
        return;
    }
    eprintln!(
        "Project: {} v{} (edition {})",
        project_info.name, project_info.version, project_info.edition
    );
    if !project_info.frameworks.is_empty() {
        let fw_list: Vec<String> = project_info
            .frameworks
            .iter()
            .map(std::string::ToString::to_string)
            .collect();
        eprintln!("Frameworks: {}", fw_list.join(", "));
    }
    if project_info.is_no_std {
        eprintln!("Mode: no_std");
    }
    if project_info.has_build_script {
        eprintln!("Build script: yes");
    }
    if let Some(ref rv) = project_info.rust_version {
        eprintln!("MSRV: {rv}");
    }
}

fn resolve_diff_context(
    project_info: &ProjectInfo,
    resolved: &ResolvedConfig,
) -> Option<diff::DiffContext> {
    let ctx = resolved.diff.as_ref().and_then(|base_hint| {
        match diff::resolve_diff(&project_info.root_dir, base_hint) {
            Ok(ctx) => Some(ctx),
            Err(e) => {
                eprintln!("Warning: {e}");
                None
            }
        }
    });

    if let Some(ref ctx) = ctx {
        eprintln!(
            "Diff mode: scanning {} changed file(s) vs {}",
            ctx.changed_files.len(),
            ctx.base,
        );
    }

    ctx
}

/// Construct the set of analysis passes based on project info and config.
/// Lint passes (clippy + custom rules) are always included when lint=true.
/// Dependency passes (audit, deny, geiger, machete, semver, coverage) run
/// only when dependencies=true and NOT in diff mode (they scan the whole project).
fn build_passes(
    project_info: &ProjectInfo,
    resolved: &ResolvedConfig,
    is_diff_mode: bool,
    offline: bool,
) -> Vec<Box<dyn scanner::AnalysisPass>> {
    let has_async_runtime = project_info.frameworks.iter().any(|f| {
        matches!(
            f,
            crate::discovery::Framework::Tokio
                | crate::discovery::Framework::AsyncStd
                | crate::discovery::Framework::Smol
        )
    });

    let mut passes: Vec<Box<dyn scanner::AnalysisPass>> = Vec::new();

    if resolved.lint {
        passes.push(Box::new(clippy::ClippyPass));
        passes.push(Box::new(rules::RuleEnginePass::with_config(
            rules::error_handling::all_rules()
                .into_iter()
                .chain(rules::performance::all_rules())
                .chain(rules::complexity::all_rules())
                .chain(rules::security::all_rules())
                .chain(if has_async_runtime {
                    rules::async_rules::all_rules()
                } else {
                    vec![]
                })
                .chain(rules::framework::rules_for_frameworks(
                    &project_info.frameworks,
                ))
                .filter(|rule| {
                    rule.default_enabled()
                        || resolved.enable_rules.contains(&rule.name().to_string())
                })
                .collect(),
            resolved.ignore_files.clone(),
            resolved.ignore_rules.clone(),
            resolved.enable_rules.clone(),
        )));
    }

    if resolved.dependencies && !is_diff_mode {
        // Prefer cargo-deny (advisory + license + ban + source checks).
        // Fall back to cargo-audit for advisory-only checks when cargo-deny
        // is not installed.
        let deny_pass = deny::DenyPass { offline };
        passes.push(Box::new(deny_pass));
        if !deny::is_cargo_deny_available() {
            passes.push(Box::new(audit::AuditPass { offline }));
        }
        passes.push(Box::new(machete::MachetePass));
        passes.push(Box::new(geiger::GeigerPass));
        passes.push(Box::new(semver_checks::SemVerPass));
        passes.push(Box::new(coverage::CoveragePass));
    }

    // MSRV validation always runs (not gated by config flags).
    passes.push(Box::new(msrv::MsrvPass {
        rust_version: project_info.rust_version.clone(),
    }));

    passes
}

fn run_passes(
    project_info: &ProjectInfo,
    resolved: &ResolvedConfig,
    scan_roots: &[PathBuf],
    diff_context: Option<&diff::DiffContext>,
    offline: bool,
    suppress_spinner: bool,
) -> (Vec<Diagnostic>, usize, Vec<String>, Duration) {
    let is_diff_mode = diff_context.is_some();
    let mut all_diagnostics = Vec::new();
    let mut total_source_files = 0;
    let mut all_skipped_passes = Vec::new();
    let mut total_elapsed = Duration::ZERO;

    // In diff mode, count changed files once (not per scan root)
    if let Some(ctx) = diff_context {
        total_source_files = ctx.changed_files.len();
    }

    let results: Vec<_> = scan_roots
        .par_iter()
        .map(|scan_root| {
            let source_files = if diff_context.is_none() {
                scanner::count_source_files(scan_root)
            } else {
                0
            };
            let passes = build_passes(project_info, resolved, is_diff_mode, offline);
            let orchestrator = scanner::ScanOrchestrator::new(passes);
            let pass_result = orchestrator.run(scan_root, resolved, suppress_spinner);
            (source_files, pass_result)
        })
        .collect();

    for (source_files, pass_result) in results {
        total_source_files += source_files;
        all_diagnostics.extend(pass_result.diagnostics);
        all_skipped_passes.extend(pass_result.skipped_passes);
        total_elapsed = total_elapsed.max(pass_result.elapsed); // max, not sum, since parallel
    }

    (
        all_diagnostics,
        total_source_files,
        all_skipped_passes,
        total_elapsed,
    )
}

/// Deduplicate diagnostics from overlapping workspace scans.
fn dedup_diagnostics(diagnostics: &mut Vec<Diagnostic>) {
    diagnostics.sort_by(|a, b| {
        a.file_path
            .cmp(&b.file_path)
            .then(a.rule.cmp(&b.rule))
            .then(a.line.cmp(&b.line))
            .then(a.column.cmp(&b.column))
            .then(a.message.cmp(&b.message))
    });
    diagnostics.dedup_by(|a, b| {
        a.file_path == b.file_path
            && a.rule == b.rule
            && a.line == b.line
            && a.column == b.column
            && a.message == b.message
    });
}

fn apply_suppressions(
    diagnostics: Vec<Diagnostic>,
    project_info: &ProjectInfo,
    resolved: &ResolvedConfig,
) -> Vec<Diagnostic> {
    let (diagnostics, suppressed_count) =
        suppression::apply_inline_suppressions(diagnostics, &project_info.root_dir);
    if resolved.verbose && suppressed_count > 0 {
        eprintln!("Suppressed {suppressed_count} diagnostic(s) via inline comments");
    }
    diagnostics
}

fn build_result(
    diagnostics: Vec<Diagnostic>,
    source_file_count: usize,
    mut skipped_passes: Vec<String>,
    elapsed: Duration,
) -> ScanResult {
    let error_count = diagnostics
        .iter()
        .filter(|d| d.severity == Severity::Error)
        .count();
    let warning_count = diagnostics
        .iter()
        .filter(|d| d.severity == Severity::Warning)
        .count();
    let info_count = diagnostics
        .iter()
        .filter(|d| d.severity == Severity::Info)
        .count();
    let (score, score_label, dimension_scores) = output::calculate_score(&diagnostics);

    skipped_passes.sort();
    skipped_passes.dedup();

    ScanResult {
        diagnostics,
        score,
        score_label,
        dimension_scores,
        source_file_count,
        elapsed,
        skipped_passes,
        error_count,
        warning_count,
        info_count,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::diagnostics::Category;
    use std::path::PathBuf;

    fn make_diagnostic(rule: &str, severity: Severity, line: Option<u32>) -> Diagnostic {
        Diagnostic {
            file_path: PathBuf::from("src/main.rs"),
            rule: rule.to_string(),
            category: Category::Correctness,
            severity,
            message: format!("test diagnostic for {rule}"),
            help: None,
            line,
            column: None,
            fix: None,
        }
    }

    #[test]
    fn custom_rule_names_includes_all_rules() {
        let names = custom_rule_names();
        // Must include custom AST rules + the special "unused-dependency" rule
        assert!(names.contains(&"unwrap-in-production".to_string()));
        assert!(names.contains(&"high-cyclomatic-complexity".to_string()));
        assert!(names.contains(&"hardcoded-secrets".to_string()));
        assert!(names.contains(&"unused-dependency".to_string()));
        // At least 15+ rules (5 error_handling + 5 performance + 1 complexity + 3 security + ...)
        assert!(
            names.len() >= 15,
            "Expected >= 15 rules, got {}",
            names.len()
        );
    }

    #[test]
    fn dedup_removes_duplicate_diagnostics() {
        let mut diags = vec![
            make_diagnostic("rule-a", Severity::Warning, Some(10)),
            make_diagnostic("rule-a", Severity::Warning, Some(10)), // duplicate
            make_diagnostic("rule-b", Severity::Error, Some(20)),
        ];
        dedup_diagnostics(&mut diags);
        assert_eq!(diags.len(), 2);
        assert_eq!(diags[0].rule, "rule-a");
        assert_eq!(diags[1].rule, "rule-b");
    }

    #[test]
    fn dedup_keeps_different_lines() {
        let mut diags = vec![
            make_diagnostic("rule-a", Severity::Warning, Some(10)),
            make_diagnostic("rule-a", Severity::Warning, Some(20)), // same rule, different line
        ];
        dedup_diagnostics(&mut diags);
        assert_eq!(diags.len(), 2);
    }

    #[test]
    fn dedup_handles_empty() {
        let mut diags: Vec<Diagnostic> = vec![];
        dedup_diagnostics(&mut diags);
        assert!(diags.is_empty());
    }

    #[test]
    fn build_result_counts_severities() {
        let diags = vec![
            make_diagnostic("err1", Severity::Error, Some(1)),
            make_diagnostic("err2", Severity::Error, Some(2)),
            make_diagnostic("warn1", Severity::Warning, Some(3)),
            make_diagnostic("info1", Severity::Info, Some(4)),
        ];
        let result = build_result(diags, 10, vec![], Duration::from_secs(1));
        assert_eq!(result.error_count, 2);
        assert_eq!(result.warning_count, 1);
        assert_eq!(result.info_count, 1);
        assert_eq!(result.source_file_count, 10);
    }

    #[test]
    fn build_result_deduplicates_skipped_passes() {
        let skipped = vec![
            "cargo-deny".to_string(),
            "cargo-audit".to_string(),
            "cargo-deny".to_string(), // duplicate
        ];
        let result = build_result(vec![], 0, skipped, Duration::ZERO);
        assert_eq!(result.skipped_passes.len(), 2);
        assert_eq!(result.skipped_passes[0], "cargo-audit"); // sorted
        assert_eq!(result.skipped_passes[1], "cargo-deny");
    }

    #[test]
    fn build_result_empty_diagnostics_gives_perfect_score() {
        let result = build_result(vec![], 5, vec![], Duration::from_millis(100));
        assert_eq!(result.score, 100);
        assert_eq!(result.error_count, 0);
        assert_eq!(result.warning_count, 0);
        assert_eq!(result.info_count, 0);
    }
}