1 //! Analyze all modules in a project for diagnostics. Exits with a non-zero
2 //! status code if any errors are found.
4 use rustc_hash::FxHashSet;
6 use hir::{db::HirDatabase, Crate, Module};
7 use ide::{AssistResolveStrategy, DiagnosticsConfig, Severity};
8 use ide_db::base_db::SourceDatabaseExt;
12 load_cargo::{load_workspace_at, LoadCargoConfig},
15 impl flags::Diagnostics {
16 pub fn run(self) -> anyhow::Result<()> {
17 let cargo_config = Default::default();
18 let load_cargo_config = LoadCargoConfig {
19 load_out_dirs_from_check: !self.disable_build_scripts,
20 with_proc_macro: !self.disable_proc_macros,
21 prefill_caches: false,
23 let (host, _vfs, _proc_macro) =
24 load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
25 let db = host.raw_database();
26 let analysis = host.analysis();
28 let mut found_error = false;
29 let mut visited_files = FxHashSet::default();
31 let work = all_modules(db).into_iter().filter(|module| {
32 let file_id = module.definition_source(db).file_id.original_file(db);
33 let source_root = db.file_source_root(file_id);
34 let source_root = db.source_root(source_root);
35 !source_root.is_library
39 let file_id = module.definition_source(db).file_id.original_file(db);
40 if !visited_files.contains(&file_id) {
42 module.krate().display_name(db).as_deref().unwrap_or("unknown").to_string();
43 println!("processing crate: {crate_name}, module: {}", _vfs.file_path(file_id));
44 for diagnostic in analysis
46 &DiagnosticsConfig::test_sample(),
47 AssistResolveStrategy::None,
52 if matches!(diagnostic.severity, Severity::Error) {
56 println!("{diagnostic:?}");
59 visited_files.insert(file_id);
64 println!("diagnostic scan complete");
68 anyhow::bail!("diagnostic error detected")
75 fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
76 let mut worklist: Vec<_> =
77 Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();
78 let mut modules = Vec::new();
80 while let Some(module) = worklist.pop() {
82 worklist.extend(module.children(db));