Skip to content

Commit 6f9a6ba

Browse files
authored
Merge pull request #20652 from ChayimFriedman2/cli-diags
internal: Improve `rust-analyzer diagnostics`
2 parents 3a67544 + 27897e4 commit 6f9a6ba

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

crates/rust-analyzer/src/cli/diagnostics.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ide::{AnalysisHost, AssistResolveStrategy, Diagnostic, DiagnosticsConfig, Se
99
use ide_db::{LineIndexDatabase, base_db::SourceDatabase};
1010
use load_cargo::{LoadCargoConfig, ProcMacroServerChoice, load_workspace_at};
1111

12-
use crate::cli::flags;
12+
use crate::cli::{flags, progress_report::ProgressReport};
1313

1414
impl flags::Diagnostics {
1515
pub fn run(self) -> anyhow::Result<()> {
@@ -50,23 +50,26 @@ impl flags::Diagnostics {
5050

5151
let mut found_error = false;
5252
let mut visited_files = FxHashSet::default();
53-
54-
let work = all_modules(db).into_iter().filter(|module| {
55-
let file_id = module.definition_source_file_id(db).original_file(db);
56-
let source_root = db.file_source_root(file_id.file_id(db)).source_root_id(db);
57-
let source_root = db.source_root(source_root).source_root(db);
58-
!source_root.is_library
59-
});
60-
53+
let min_severity = self.severity.unwrap_or(flags::Severity::Weak);
54+
55+
let work = all_modules(db)
56+
.into_iter()
57+
.filter(|module| {
58+
let file_id = module.definition_source_file_id(db).original_file(db);
59+
let source_root = db.file_source_root(file_id.file_id(db)).source_root_id(db);
60+
let source_root = db.source_root(source_root).source_root(db);
61+
!source_root.is_library
62+
})
63+
.collect::<Vec<_>>();
64+
65+
let mut bar = ProgressReport::new(work.len());
6166
for module in work {
6267
let file_id = module.definition_source_file_id(db).original_file(db);
6368
if !visited_files.contains(&file_id) {
69+
let message = format!("processing {}", _vfs.file_path(file_id.file_id(db)));
70+
bar.set_message(move || message.clone());
6471
let crate_name =
6572
module.krate().display_name(db).as_deref().unwrap_or(&sym::unknown).to_owned();
66-
println!(
67-
"processing crate: {crate_name}, module: {}",
68-
_vfs.file_path(file_id.file_id(db))
69-
);
7073
for diagnostic in analysis
7174
.full_diagnostics(
7275
&DiagnosticsConfig::test_sample(),
@@ -75,6 +78,16 @@ impl flags::Diagnostics {
7578
)
7679
.unwrap()
7780
{
81+
let severity = match diagnostic.severity {
82+
Severity::Error => flags::Severity::Error,
83+
Severity::Warning => flags::Severity::Warning,
84+
Severity::WeakWarning => flags::Severity::Weak,
85+
Severity::Allow => continue,
86+
};
87+
if severity < min_severity {
88+
continue;
89+
}
90+
7891
if matches!(diagnostic.severity, Severity::Error) {
7992
found_error = true;
8093
}
@@ -83,12 +96,17 @@ impl flags::Diagnostics {
8396
let line_index = db.line_index(range.file_id);
8497
let start = line_index.line_col(range.range.start());
8598
let end = line_index.line_col(range.range.end());
86-
println!("{severity:?} {code:?} from {start:?} to {end:?}: {message}");
99+
bar.println(format!(
100+
"at crate {crate_name}, file {}: {severity:?} {code:?} from {start:?} to {end:?}: {message}",
101+
_vfs.file_path(file_id.file_id(db))
102+
));
87103
}
88104

89105
visited_files.insert(file_id);
90106
}
107+
bar.inc(1);
91108
}
109+
bar.finish_and_clear();
92110

93111
println!();
94112
println!("diagnostic scan complete");

crates/rust-analyzer/src/cli/flags.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ xflags::xflags! {
124124
optional --disable-proc-macros
125125
/// Run the proc-macro-srv binary at the specified path.
126126
optional --proc-macro-srv path: PathBuf
127+
128+
/// The minimum severity.
129+
optional --severity severity: Severity
127130
}
128131

129132
/// Report unresolved references
@@ -281,6 +284,7 @@ pub struct Diagnostics {
281284
pub disable_build_scripts: bool,
282285
pub disable_proc_macros: bool,
283286
pub proc_macro_srv: Option<PathBuf>,
287+
pub severity: Option<Severity>,
284288
}
285289

286290
#[derive(Debug)]
@@ -376,3 +380,23 @@ impl FromStr for OutputFormat {
376380
}
377381
}
378382
}
383+
384+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
385+
pub enum Severity {
386+
Weak,
387+
Warning,
388+
Error,
389+
}
390+
391+
impl FromStr for Severity {
392+
type Err = String;
393+
394+
fn from_str(s: &str) -> Result<Self, Self::Err> {
395+
match &*s.to_ascii_lowercase() {
396+
"weak" => Ok(Self::Weak),
397+
"warning" => Ok(Self::Warning),
398+
"error" => Ok(Self::Error),
399+
_ => Err(format!("unknown severity `{s}`")),
400+
}
401+
}
402+
}

0 commit comments

Comments
 (0)