|
1 | | -//! Analyze all modules in a project for diagnostics. Exits with a non-zero status |
2 | | -//! code if any errors are found. |
| 1 | +//! Analyze all modules in a project for diagnostics. Exits with a non-zero |
| 2 | +//! status code if any errors are found. |
3 | 3 |
|
4 | | -use std::path::Path; |
5 | | - |
6 | | -use anyhow::anyhow; |
7 | 4 | use rustc_hash::FxHashSet; |
8 | 5 |
|
9 | 6 | use hir::{db::HirDatabase, Crate, Module}; |
10 | 7 | use ide::{AssistResolveStrategy, DiagnosticsConfig, Severity}; |
11 | 8 | use ide_db::base_db::SourceDatabaseExt; |
12 | 9 |
|
13 | 10 | use crate::cli::{ |
| 11 | + flags, |
14 | 12 | load_cargo::{load_workspace_at, LoadCargoConfig}, |
15 | | - Result, |
16 | 13 | }; |
17 | 14 |
|
18 | | -fn all_modules(db: &dyn HirDatabase) -> Vec<Module> { |
19 | | - let mut worklist: Vec<_> = |
20 | | - Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect(); |
21 | | - let mut modules = Vec::new(); |
| 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, |
| 22 | + }; |
| 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(); |
22 | 27 |
|
23 | | - while let Some(module) = worklist.pop() { |
24 | | - modules.push(module); |
25 | | - worklist.extend(module.children(db)); |
26 | | - } |
27 | | - |
28 | | - modules |
29 | | -} |
| 28 | + let mut found_error = false; |
| 29 | + let mut visited_files = FxHashSet::default(); |
30 | 30 |
|
31 | | -pub fn diagnostics( |
32 | | - path: &Path, |
33 | | - load_out_dirs_from_check: bool, |
34 | | - with_proc_macro: bool, |
35 | | -) -> Result<()> { |
36 | | - let cargo_config = Default::default(); |
37 | | - let load_cargo_config = |
38 | | - LoadCargoConfig { load_out_dirs_from_check, with_proc_macro, prefill_caches: false }; |
39 | | - let (host, _vfs, _proc_macro) = |
40 | | - load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; |
41 | | - let db = host.raw_database(); |
42 | | - let analysis = host.analysis(); |
| 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 |
| 36 | + }); |
43 | 37 |
|
44 | | - let mut found_error = false; |
45 | | - let mut visited_files = FxHashSet::default(); |
| 38 | + for module in work { |
| 39 | + let file_id = module.definition_source(db).file_id.original_file(db); |
| 40 | + if !visited_files.contains(&file_id) { |
| 41 | + let crate_name = |
| 42 | + module.krate().display_name(db).as_deref().unwrap_or("unknown").to_string(); |
| 43 | + println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id)); |
| 44 | + for diagnostic in analysis |
| 45 | + .diagnostics( |
| 46 | + &DiagnosticsConfig::default(), |
| 47 | + AssistResolveStrategy::None, |
| 48 | + file_id, |
| 49 | + ) |
| 50 | + .unwrap() |
| 51 | + { |
| 52 | + if matches!(diagnostic.severity, Severity::Error) { |
| 53 | + found_error = true; |
| 54 | + } |
46 | 55 |
|
47 | | - let work = all_modules(db).into_iter().filter(|module| { |
48 | | - let file_id = module.definition_source(db).file_id.original_file(db); |
49 | | - let source_root = db.file_source_root(file_id); |
50 | | - let source_root = db.source_root(source_root); |
51 | | - !source_root.is_library |
52 | | - }); |
53 | | - |
54 | | - for module in work { |
55 | | - let file_id = module.definition_source(db).file_id.original_file(db); |
56 | | - if !visited_files.contains(&file_id) { |
57 | | - let crate_name = |
58 | | - module.krate().display_name(db).as_deref().unwrap_or("unknown").to_string(); |
59 | | - println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id)); |
60 | | - for diagnostic in analysis |
61 | | - .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::None, file_id) |
62 | | - .unwrap() |
63 | | - { |
64 | | - if matches!(diagnostic.severity, Severity::Error) { |
65 | | - found_error = true; |
| 56 | + println!("{:?}", diagnostic); |
66 | 57 | } |
67 | 58 |
|
68 | | - println!("{:?}", diagnostic); |
| 59 | + visited_files.insert(file_id); |
69 | 60 | } |
| 61 | + } |
| 62 | + |
| 63 | + println!(); |
| 64 | + println!("diagnostic scan complete"); |
70 | 65 |
|
71 | | - visited_files.insert(file_id); |
| 66 | + if found_error { |
| 67 | + println!(); |
| 68 | + anyhow::bail!("diagnostic error detected") |
72 | 69 | } |
| 70 | + |
| 71 | + Ok(()) |
73 | 72 | } |
| 73 | +} |
74 | 74 |
|
75 | | - println!(); |
76 | | - println!("diagnostic scan complete"); |
| 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(); |
77 | 79 |
|
78 | | - if found_error { |
79 | | - println!(); |
80 | | - Err(anyhow!("diagnostic error detected")) |
81 | | - } else { |
82 | | - Ok(()) |
| 80 | + while let Some(module) = worklist.pop() { |
| 81 | + modules.push(module); |
| 82 | + worklist.extend(module.children(db)); |
83 | 83 | } |
| 84 | + |
| 85 | + modules |
84 | 86 | } |
0 commit comments