{"record":{"id":"005a86e0b4c2ff85","repo":"rust-lang/rust-analyzer","slug":"diagnostic-error-detected","errorCode":null,"errorMessage":"diagnostic error detected","messagePattern":"diagnostic error detected","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/rust-analyzer/src/cli/diagnostics.rs","lineNumber":123,"sourceCode":"                    let end = line_index.line_col(range.range.end());\n                    bar.println(format!(\n                        \"at crate {crate_name}, file {}: {severity:?} {code:?} from {start:?} to {end:?}: {message}\",\n                        _vfs.file_path(file_id.file_id(db))\n                    ));\n                }\n\n                visited_files.insert(file_id);\n            }\n            bar.inc(1);\n        }\n        bar.finish_and_clear();\n\n        println!();\n        println!(\"diagnostic scan complete\");\n\n        if found_error {\n            println!();\n            anyhow::bail!(\"diagnostic error detected\")\n        }\n\n        Ok(())\n    }\n}\n\nfn all_modules(db: &dyn HirDatabase) -> Vec<Module> {\n    let mut worklist: Vec<_> =\n        Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();\n    let mut modules = Vec::new();\n\n    while let Some(module) = worklist.pop() {\n        modules.push(module);\n        worklist.extend(module.children(db));\n    }\n\n    modules\n}","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/rust-analyzer/src/cli/diagnostics.rs#L105-L141","documentation":"The `rust-analyzer diagnostics` CLI scans the project for unsound/diagnostic findings (mismatched args, no-such-field, etc.). It collects a boolean `found_error` while printing each diagnostic, and if any error-level diagnostic was seen, it finishes by bailing with \"diagnostic error detected\" so the CLI exits non-zero. This is an aggregate signal — the specific findings are printed above it in stdout.","triggerScenarios":"Running `rust-analyzer diagnostics <path>` on code containing error-level rust-analyzer diagnostics (e.g. mismatched-arg-count, missing fields, type errors) so `found_error` becomes true at scan end.","commonSituations":"CI job running the diagnostics CLI on a crate with real compile-level errors; using the tool to smoke-test a project that is actually broken; running against generated code that doesn't compile.","solutions":["Read the diagnostics printed before the bail; fix each reported error in the source.","Run `cargo check` on the target to confirm/see the underlying compiler errors.","If scanning intentionally-broken fixtures, expect non-zero exit and treat it as the success signal.","Re-run after fixes; the exit code 0 means the scan found no error-level diagnostics."],"exampleFix":"// before\nrust-analyzer diagnostics ./crates/broken  # exits non-zero\n// after fixing reported diagnostics in the source\nrust-analyzer diagnostics ./crates/broken  # exits 0","handlingStrategy":"try-catch","validationCode":"// pre-check the code compiles before running the diagnostics CLI\nlet status = Command::new(\"cargo\").args([\"check\", \"--workspace\"]).status()?;\nif !status.success() {\n    // fix compile errors first; diagnostics CLI will exit non-zero\n}","typeGuard":null,"tryCatchPattern":"let status = Command::new(\"rust-analyzer\")\n    .args([\"diagnostics\", path])\n    .status()?;\nif !status.success() {\n    // parse the diagnostics printed on stdout above the bail message\n    // and fix each reported finding, or treat as expected for fixtures\n}","preventionTips":["Run `cargo check` before the diagnostics scan to catch obvious breakage.","Fix the individual diagnostics printed before the aggregate bail message.","In CI, treat a non-zero exit as 'error-level diagnostics found' and parse stdout for details.","Only scan code that is expected to type-check."],"tags":["cli","diagnostics","static-analysis","exit-code"],"backgroundTag":"diagnostic-scan-found-errors","analyzedSha":"e8f7e90aa3e7b26aa9a000200f606c1078da99ec","analyzedAt":"2026-09-03T21:08:06.959Z","contentChangedAt":"2026-09-03T21:08:06.959Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}