Improve rust-analyzer diagnostics

In my experience the `processing <module>` messages make it harder to search for the actual diagnostics, so remove them and instead print the filename only if there is a diagnostic.

Also allow choosing the minimum severity.
This commit is contained in:
Chayim Refael Friedman 2025-09-10 20:34:02 +03:00
parent d048a477dc
commit 27897e48dc
2 changed files with 54 additions and 12 deletions

View File

@ -9,7 +9,7 @@ use ide::{AnalysisHost, AssistResolveStrategy, Diagnostic, DiagnosticsConfig, Se
use ide_db::{LineIndexDatabase, base_db::SourceDatabase};
use load_cargo::{LoadCargoConfig, ProcMacroServerChoice, load_workspace_at};
use crate::cli::flags;
use crate::cli::{flags, progress_report::ProgressReport};
impl flags::Diagnostics {
pub fn run(self) -> anyhow::Result<()> {
@ -50,23 +50,26 @@ impl flags::Diagnostics {
let mut found_error = false;
let mut visited_files = FxHashSet::default();
let min_severity = self.severity.unwrap_or(flags::Severity::Weak);
let work = all_modules(db).into_iter().filter(|module| {
let file_id = module.definition_source_file_id(db).original_file(db);
let source_root = db.file_source_root(file_id.file_id(db)).source_root_id(db);
let source_root = db.source_root(source_root).source_root(db);
!source_root.is_library
});
let work = all_modules(db)
.into_iter()
.filter(|module| {
let file_id = module.definition_source_file_id(db).original_file(db);
let source_root = db.file_source_root(file_id.file_id(db)).source_root_id(db);
let source_root = db.source_root(source_root).source_root(db);
!source_root.is_library
})
.collect::<Vec<_>>();
let mut bar = ProgressReport::new(work.len());
for module in work {
let file_id = module.definition_source_file_id(db).original_file(db);
if !visited_files.contains(&file_id) {
let message = format!("processing {}", _vfs.file_path(file_id.file_id(db)));
bar.set_message(move || message.clone());
let crate_name =
module.krate().display_name(db).as_deref().unwrap_or(&sym::unknown).to_owned();
println!(
"processing crate: {crate_name}, module: {}",
_vfs.file_path(file_id.file_id(db))
);
for diagnostic in analysis
.full_diagnostics(
&DiagnosticsConfig::test_sample(),
@ -75,6 +78,16 @@ impl flags::Diagnostics {
)
.unwrap()
{
let severity = match diagnostic.severity {
Severity::Error => flags::Severity::Error,
Severity::Warning => flags::Severity::Warning,
Severity::WeakWarning => flags::Severity::Weak,
Severity::Allow => continue,
};
if severity < min_severity {
continue;
}
if matches!(diagnostic.severity, Severity::Error) {
found_error = true;
}
@ -83,12 +96,17 @@ impl flags::Diagnostics {
let line_index = db.line_index(range.file_id);
let start = line_index.line_col(range.range.start());
let end = line_index.line_col(range.range.end());
println!("{severity:?} {code:?} from {start:?} to {end:?}: {message}");
bar.println(format!(
"at crate {crate_name}, file {}: {severity:?} {code:?} from {start:?} to {end:?}: {message}",
_vfs.file_path(file_id.file_id(db))
));
}
visited_files.insert(file_id);
}
bar.inc(1);
}
bar.finish_and_clear();
println!();
println!("diagnostic scan complete");

View File

@ -124,6 +124,9 @@ xflags::xflags! {
optional --disable-proc-macros
/// Run the proc-macro-srv binary at the specified path.
optional --proc-macro-srv path: PathBuf
/// The minimum severity.
optional --severity severity: Severity
}
/// Report unresolved references
@ -281,6 +284,7 @@ pub struct Diagnostics {
pub disable_build_scripts: bool,
pub disable_proc_macros: bool,
pub proc_macro_srv: Option<PathBuf>,
pub severity: Option<Severity>,
}
#[derive(Debug)]
@ -376,3 +380,23 @@ impl FromStr for OutputFormat {
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
Weak,
Warning,
Error,
}
impl FromStr for Severity {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match &*s.to_ascii_lowercase() {
"weak" => Ok(Self::Weak),
"warning" => Ok(Self::Warning),
"error" => Ok(Self::Error),
_ => Err(format!("unknown severity `{s}`")),
}
}
}