Skip to main content

rust_analyzer/cli/
diagnostics.rs

1//! Analyze all modules in a project for diagnostics. Exits with a non-zero
2//! status code if any errors are found.
3
4use project_model::{CargoConfig, RustLibSource};
5use rustc_hash::FxHashSet;
6
7use hir::{Crate, Module, db::HirDatabase, sym};
8use ide::{AnalysisHost, AssistResolveStrategy, Diagnostic, DiagnosticsConfig, Severity};
9use ide_db::{base_db::SourceDatabase, line_index};
10use load_cargo::{LoadCargoConfig, ProcMacroServerChoice, load_workspace_at};
11
12use crate::cli::{flags, progress_report::ProgressReport};
13
14impl flags::Diagnostics {
15    pub fn run(self) -> anyhow::Result<()> {
16        const STACK_SIZE: usize = 1024 * 1024 * 8;
17
18        let handle = stdx::thread::Builder::new(
19            stdx::thread::ThreadIntent::LatencySensitive,
20            "BIG_STACK_THREAD",
21        )
22        .stack_size(STACK_SIZE)
23        .spawn(|| self.run_())
24        .unwrap();
25
26        handle.join()
27    }
28    fn run_(self) -> anyhow::Result<()> {
29        let cargo_config = CargoConfig {
30            sysroot: Some(RustLibSource::Discover),
31            all_targets: true,
32            ..Default::default()
33        };
34        let with_proc_macro_server = if let Some(p) = &self.proc_macro_srv {
35            let path = vfs::AbsPathBuf::assert_utf8(std::env::current_dir()?.join(p));
36            ProcMacroServerChoice::Explicit(path)
37        } else {
38            ProcMacroServerChoice::Sysroot
39        };
40        let load_cargo_config = LoadCargoConfig {
41            load_out_dirs_from_check: !self.disable_build_scripts,
42            with_proc_macro_server,
43            prefill_caches: false,
44            num_worker_threads: 1,
45            proc_macro_processes: 1,
46        };
47        let (db, _vfs, _proc_macro) =
48            load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
49        let host = AnalysisHost::with_database(db);
50        let db = host.raw_database();
51        let analysis = host.analysis();
52
53        let mut found_error = false;
54        let mut visited_files = FxHashSet::default();
55        let min_severity = self.severity.unwrap_or(flags::Severity::Weak);
56
57        let work = all_modules(db)
58            .into_iter()
59            .filter(|module| {
60                let file_id = module.definition_source_file_id(db).original_file(db);
61                let source_root = db.file_source_root(file_id.file_id(db)).source_root_id(db);
62                let source_root = db.source_root(source_root).source_root(db);
63                !source_root.is_library
64            })
65            .collect::<Vec<_>>();
66
67        let mut bar = ProgressReport::new(work.len());
68        for module in work {
69            let file_id = module.definition_source_file_id(db).original_file(db);
70            if !visited_files.contains(&file_id) {
71                let message = format!("processing {}", _vfs.file_path(file_id.file_id(db)));
72                bar.set_message(move || message.clone());
73                let crate_name = module
74                    .krate(db)
75                    .display_name(db)
76                    .as_deref()
77                    .unwrap_or(&sym::unknown)
78                    .to_owned();
79                for diagnostic in analysis
80                    .full_diagnostics(
81                        &DiagnosticsConfig::test_sample(),
82                        AssistResolveStrategy::None,
83                        file_id.file_id(db),
84                    )
85                    .unwrap()
86                {
87                    let severity = match diagnostic.severity {
88                        Severity::Error => flags::Severity::Error,
89                        Severity::Warning => flags::Severity::Warning,
90                        Severity::WeakWarning => flags::Severity::Weak,
91                        Severity::Allow => continue,
92                    };
93                    if severity < min_severity {
94                        continue;
95                    }
96
97                    if matches!(diagnostic.severity, Severity::Error) {
98                        found_error = true;
99                    }
100
101                    let Diagnostic { code, message, range, severity, .. } = diagnostic;
102                    let line_index = line_index(db, range.file_id);
103                    let start = line_index.line_col(range.range.start());
104                    let end = line_index.line_col(range.range.end());
105                    bar.println(format!(
106                        "at crate {crate_name}, file {}: {severity:?} {code:?} from {start:?} to {end:?}: {message}",
107                        _vfs.file_path(file_id.file_id(db))
108                    ));
109                }
110
111                visited_files.insert(file_id);
112            }
113            bar.inc(1);
114        }
115        bar.finish_and_clear();
116
117        println!();
118        println!("diagnostic scan complete");
119
120        if found_error {
121            println!();
122            anyhow::bail!("diagnostic error detected")
123        }
124
125        Ok(())
126    }
127}
128
129fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
130    let mut worklist: Vec<_> =
131        Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();
132    let mut modules = Vec::new();
133
134    while let Some(module) = worklist.pop() {
135        modules.push(module);
136        worklist.extend(module.children(db));
137    }
138
139    modules
140}