Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions crates/rust-analyzer/src/cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -50,23 +50,26 @@ impl flags::Diagnostics {

let mut found_error = false;
let mut visited_files = FxHashSet::default();

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 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
})
.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(),
Expand All @@ -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;
}
Expand All @@ -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");
Expand Down
24 changes: 24 additions & 0 deletions crates/rust-analyzer/src/cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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}`")),
}
}
}
Loading