ra_ap_rust_analyzer/cli/
diagnostics.rs1use 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::{LineIndexDatabase, base_db::SourceDatabase};
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 proc_macro_processes: 1,
45 };
46 let (db, _vfs, _proc_macro) =
47 load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
48 let host = AnalysisHost::with_database(db);
49 let db = host.raw_database();
50 let analysis = host.analysis();
51
52 let mut found_error = false;
53 let mut visited_files = FxHashSet::default();
54 let min_severity = self.severity.unwrap_or(flags::Severity::Weak);
55
56 let work = all_modules(db)
57 .into_iter()
58 .filter(|module| {
59 let file_id = module.definition_source_file_id(db).original_file(db);
60 let source_root = db.file_source_root(file_id.file_id(db)).source_root_id(db);
61 let source_root = db.source_root(source_root).source_root(db);
62 !source_root.is_library
63 })
64 .collect::<Vec<_>>();
65
66 let mut bar = ProgressReport::new(work.len());
67 for module in work {
68 let file_id = module.definition_source_file_id(db).original_file(db);
69 if !visited_files.contains(&file_id) {
70 let message = format!("processing {}", _vfs.file_path(file_id.file_id(db)));
71 bar.set_message(move || message.clone());
72 let crate_name = module
73 .krate(db)
74 .display_name(db)
75 .as_deref()
76 .unwrap_or(&sym::unknown)
77 .to_owned();
78 for diagnostic in analysis
79 .full_diagnostics(
80 &DiagnosticsConfig::test_sample(),
81 AssistResolveStrategy::None,
82 file_id.file_id(db),
83 )
84 .unwrap()
85 {
86 let severity = match diagnostic.severity {
87 Severity::Error => flags::Severity::Error,
88 Severity::Warning => flags::Severity::Warning,
89 Severity::WeakWarning => flags::Severity::Weak,
90 Severity::Allow => continue,
91 };
92 if severity < min_severity {
93 continue;
94 }
95
96 if matches!(diagnostic.severity, Severity::Error) {
97 found_error = true;
98 }
99
100 let Diagnostic { code, message, range, severity, .. } = diagnostic;
101 let line_index = db.line_index(range.file_id);
102 let start = line_index.line_col(range.range.start());
103 let end = line_index.line_col(range.range.end());
104 bar.println(format!(
105 "at crate {crate_name}, file {}: {severity:?} {code:?} from {start:?} to {end:?}: {message}",
106 _vfs.file_path(file_id.file_id(db))
107 ));
108 }
109
110 visited_files.insert(file_id);
111 }
112 bar.inc(1);
113 }
114 bar.finish_and_clear();
115
116 println!();
117 println!("diagnostic scan complete");
118
119 if found_error {
120 println!();
121 anyhow::bail!("diagnostic error detected")
122 }
123
124 Ok(())
125 }
126}
127
128fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
129 let mut worklist: Vec<_> =
130 Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();
131 let mut modules = Vec::new();
132
133 while let Some(module) = worklist.pop() {
134 modules.push(module);
135 worklist.extend(module.children(db));
136 }
137
138 modules
139}