add diagnostics subcommand to rust-analyzer CLI
This commit is contained in:
parent
c388130f5f
commit
f62c73a972
@ -35,6 +35,10 @@ pub(crate) enum Command {
|
||||
what: BenchWhat,
|
||||
load_output_dirs: bool,
|
||||
},
|
||||
Diagnostics {
|
||||
path: PathBuf,
|
||||
load_output_dirs: bool,
|
||||
},
|
||||
RunServer,
|
||||
Version,
|
||||
}
|
||||
@ -209,6 +213,36 @@ pub(crate) fn parse() -> Result<Result<Args, HelpPrinted>> {
|
||||
let load_output_dirs = matches.contains("--load-output-dirs");
|
||||
Command::Bench { path, what, load_output_dirs }
|
||||
}
|
||||
"diagnostics" => {
|
||||
if matches.contains(["-h", "--help"]) {
|
||||
eprintln!(
|
||||
"\
|
||||
ra-cli-diagnostics
|
||||
|
||||
USAGE:
|
||||
rust-analyzer diagnostics [FLAGS] [PATH]
|
||||
|
||||
FLAGS:
|
||||
-h, --help Prints help information
|
||||
--load-output-dirs Load OUT_DIR values by running `cargo check` before analysis
|
||||
|
||||
ARGS:
|
||||
<PATH>"
|
||||
);
|
||||
return Ok(Err(HelpPrinted));
|
||||
}
|
||||
|
||||
let load_output_dirs = matches.contains("--load-output-dirs");
|
||||
let path = {
|
||||
let mut trailing = matches.free()?;
|
||||
if trailing.len() != 1 {
|
||||
bail!("Invalid flags");
|
||||
}
|
||||
trailing.pop().unwrap().into()
|
||||
};
|
||||
|
||||
Command::Diagnostics { path, load_output_dirs }
|
||||
}
|
||||
_ => {
|
||||
eprintln!(
|
||||
"\
|
||||
|
@ -39,6 +39,10 @@ fn main() -> Result<()> {
|
||||
cli::analysis_bench(args.verbosity, path.as_ref(), what, load_output_dirs)?
|
||||
}
|
||||
|
||||
args::Command::Diagnostics { path, load_output_dirs } => {
|
||||
cli::diagnostics(path.as_ref(), load_output_dirs)?
|
||||
}
|
||||
|
||||
args::Command::RunServer => run_server()?,
|
||||
args::Command::Version => println!("rust-analyzer {}", env!("REV")),
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
mod load_cargo;
|
||||
mod analysis_stats;
|
||||
mod analysis_bench;
|
||||
mod diagnostics;
|
||||
mod progress_report;
|
||||
|
||||
use std::io::Read;
|
||||
@ -12,6 +13,10 @@
|
||||
use ra_prof::profile;
|
||||
use ra_syntax::{AstNode, SourceFile};
|
||||
|
||||
pub use analysis_bench::{analysis_bench, BenchWhat, Position};
|
||||
pub use analysis_stats::analysis_stats;
|
||||
pub use diagnostics::diagnostics;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Verbosity {
|
||||
Spammy,
|
||||
@ -60,9 +65,6 @@ pub fn highlight(rainbow: bool) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub use analysis_bench::{analysis_bench, BenchWhat, Position};
|
||||
pub use analysis_stats::analysis_stats;
|
||||
|
||||
fn file() -> Result<SourceFile> {
|
||||
let text = read_stdin()?;
|
||||
Ok(SourceFile::parse(&text).tree())
|
||||
|
74
crates/rust-analyzer/src/cli/diagnostics.rs
Normal file
74
crates/rust-analyzer/src/cli/diagnostics.rs
Normal file
@ -0,0 +1,74 @@
|
||||
//! Analyze all files in project for diagnostics. Exits with a non-zero status
|
||||
//! code if any errors are found.
|
||||
|
||||
use anyhow::anyhow;
|
||||
use ra_db::{SourceDatabaseExt, SourceRootId};
|
||||
use ra_ide::{Analysis, Severity};
|
||||
use std::{collections::HashSet, path::Path};
|
||||
|
||||
use crate::cli::{load_cargo::load_cargo, Result};
|
||||
use hir::{db::HirDatabase, Crate, Module};
|
||||
|
||||
pub fn diagnostics(path: &Path, load_output_dirs: bool) -> Result<()> {
|
||||
let (host, roots) = load_cargo(path, load_output_dirs)?;
|
||||
let db = host.raw_database();
|
||||
let analysis = host.analysis();
|
||||
let members = roots
|
||||
.into_iter()
|
||||
.filter_map(
|
||||
|(source_root_id, project_root)| {
|
||||
if project_root.is_member() {
|
||||
Some(source_root_id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
)
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
let mut found_error = false;
|
||||
let mut visited_modules = HashSet::new();
|
||||
for krate in Crate::all(db) {
|
||||
let module = krate.root_module(db).expect("crate without root module");
|
||||
check_module(module, db, &mut visited_modules, &members, &analysis, &mut found_error);
|
||||
}
|
||||
|
||||
println!();
|
||||
println!("diagnostic scan complete");
|
||||
|
||||
if found_error {
|
||||
println!();
|
||||
Err(anyhow!("diagnostic error detected"))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn check_module(
|
||||
module: Module,
|
||||
db: &(impl HirDatabase + SourceDatabaseExt),
|
||||
visited_modules: &mut HashSet<Module>,
|
||||
members: &HashSet<SourceRootId>,
|
||||
analysis: &Analysis,
|
||||
found_error: &mut bool,
|
||||
) {
|
||||
let file_id = module.definition_source(db).file_id.original_file(db);
|
||||
if !visited_modules.contains(&module) {
|
||||
if members.contains(&db.file_source_root(file_id)) {
|
||||
println!("processing: {}", db.file_relative_path(file_id));
|
||||
for diagnostic in analysis.diagnostics(file_id).unwrap() {
|
||||
if matches!(diagnostic.severity, Severity::Error) {
|
||||
*found_error = true;
|
||||
}
|
||||
|
||||
println!("{:?}", diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
visited_modules.insert(module);
|
||||
|
||||
for child_module in module.children(db) {
|
||||
check_module(child_module, db, visited_modules, members, analysis, found_error);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user