Skip to content

Commit 80357b8

Browse files
committed
Add command to report unresolved references
1 parent 9b72445 commit 80357b8

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed

crates/rust-analyzer/src/bin/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ fn actual_main() -> anyhow::Result<ExitCode> {
8282
flags::RustAnalyzerCmd::Highlight(cmd) => cmd.run()?,
8383
flags::RustAnalyzerCmd::AnalysisStats(cmd) => cmd.run(verbosity)?,
8484
flags::RustAnalyzerCmd::Diagnostics(cmd) => cmd.run()?,
85+
flags::RustAnalyzerCmd::UnresolvedReferences(cmd) => cmd.run()?,
8586
flags::RustAnalyzerCmd::Ssr(cmd) => cmd.run()?,
8687
flags::RustAnalyzerCmd::Search(cmd) => cmd.run()?,
8788
flags::RustAnalyzerCmd::Lsif(cmd) => cmd.run()?,

crates/rust-analyzer/src/cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod rustc_tests;
1313
mod scip;
1414
mod ssr;
1515
mod symbols;
16+
mod unresolved_references;
1617

1718
mod progress_report;
1819

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@ xflags::xflags! {
124124
optional --proc-macro-srv path: PathBuf
125125
}
126126

127+
/// Report unresolved references
128+
cmd unresolved-references {
129+
/// Directory with Cargo.toml.
130+
required path: PathBuf
131+
132+
/// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
133+
optional --disable-build-scripts
134+
/// Don't use expand proc macros.
135+
optional --disable-proc-macros
136+
/// Run a custom proc-macro-srv binary.
137+
optional --proc-macro-srv path: PathBuf
138+
}
139+
127140
cmd ssr {
128141
/// A structured search replace rule (`$a.foo($b) ==>> bar($a, $b)`)
129142
repeated rule: SsrRule
@@ -181,6 +194,7 @@ pub enum RustAnalyzerCmd {
181194
RunTests(RunTests),
182195
RustcTests(RustcTests),
183196
Diagnostics(Diagnostics),
197+
UnresolvedReferences(UnresolvedReferences),
184198
Ssr(Ssr),
185199
Search(Search),
186200
Lsif(Lsif),
@@ -250,6 +264,15 @@ pub struct Diagnostics {
250264
pub proc_macro_srv: Option<PathBuf>,
251265
}
252266

267+
#[derive(Debug)]
268+
pub struct UnresolvedReferences {
269+
pub path: PathBuf,
270+
271+
pub disable_build_scripts: bool,
272+
pub disable_proc_macros: bool,
273+
pub proc_macro_srv: Option<PathBuf>,
274+
}
275+
253276
#[derive(Debug)]
254277
pub struct Ssr {
255278
pub rule: Vec<SsrRule>,
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
use hir::{
2+
db::HirDatabase, AnyDiagnostic, Crate, HirFileIdExt as _, MacroFileIdExt as _, Module,
3+
Semantics,
4+
};
5+
use ide::{AnalysisHost, RootDatabase, TextRange};
6+
use ide_db::{
7+
base_db::{SourceDatabase, SourceRootDatabase},
8+
defs::NameRefClass,
9+
EditionedFileId, FxHashSet, LineIndexDatabase as _,
10+
};
11+
use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
12+
use parser::SyntaxKind;
13+
use project_model::{CargoConfig, RustLibSource};
14+
use syntax::{ast, AstNode, WalkEvent};
15+
use vfs::FileId;
16+
17+
use crate::cli::flags;
18+
19+
impl flags::UnresolvedReferences {
20+
pub fn run(self) -> anyhow::Result<()> {
21+
const STACK_SIZE: usize = 1024 * 1024 * 8;
22+
23+
let handle = stdx::thread::Builder::new(stdx::thread::ThreadIntent::LatencySensitive)
24+
.name("BIG_STACK_THREAD".into())
25+
.stack_size(STACK_SIZE)
26+
.spawn(|| self.run_())
27+
.unwrap();
28+
29+
handle.join()
30+
}
31+
fn run_(self) -> anyhow::Result<()> {
32+
let cargo_config =
33+
CargoConfig { sysroot: Some(RustLibSource::Discover), ..Default::default() };
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+
};
45+
let (db, vfs, _proc_macro) =
46+
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
47+
let host = AnalysisHost::with_database(db);
48+
let db = host.raw_database();
49+
let sema = Semantics::new(db);
50+
51+
let mut visited_files = FxHashSet::default();
52+
53+
let work = all_modules(db).into_iter().filter(|module| {
54+
let file_id = module.definition_source_file_id(db).original_file(db);
55+
let source_root = db.file_source_root(file_id.into());
56+
let source_root = db.source_root(source_root);
57+
!source_root.is_library
58+
});
59+
60+
for module in work {
61+
let file_id = module.definition_source_file_id(db).original_file(db);
62+
if !visited_files.contains(&file_id) {
63+
let crate_name =
64+
module.krate().display_name(db).as_deref().unwrap_or("unknown").to_owned();
65+
let file_path = vfs.file_path(file_id.into());
66+
eprintln!("processing crate: {crate_name}, module: {file_path}",);
67+
68+
let line_index = db.line_index(file_id.into());
69+
let file_text = db.file_text(file_id.into());
70+
71+
for range in find_unresolved_references(&db, &sema, file_id.into(), &module) {
72+
let line_col = line_index.line_col(range.start());
73+
let line = line_col.line + 1;
74+
let col = line_col.col + 1;
75+
let text = &file_text[range];
76+
println!("{file_path}:{line}:{col}: {text}");
77+
}
78+
79+
visited_files.insert(file_id);
80+
}
81+
}
82+
83+
eprintln!();
84+
eprintln!("scan complete");
85+
86+
Ok(())
87+
}
88+
}
89+
90+
fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
91+
let mut worklist: Vec<_> =
92+
Crate::all(db).into_iter().map(|krate| krate.root_module()).collect();
93+
let mut modules = Vec::new();
94+
95+
while let Some(module) = worklist.pop() {
96+
modules.push(module);
97+
worklist.extend(module.children(db));
98+
}
99+
100+
modules
101+
}
102+
103+
fn find_unresolved_references(
104+
db: &RootDatabase,
105+
sema: &Semantics<'_, RootDatabase>,
106+
file_id: FileId,
107+
module: &Module,
108+
) -> Vec<TextRange> {
109+
let mut unresolved_references = all_unresolved_references(sema, file_id);
110+
111+
// remove unresolved references which are within inactive code
112+
let mut diagnostics = Vec::new();
113+
module.diagnostics(db, &mut diagnostics, false);
114+
for diagnostic in diagnostics {
115+
let AnyDiagnostic::InactiveCode(inactive_code) = diagnostic else {
116+
continue;
117+
};
118+
119+
let node = inactive_code.node;
120+
121+
if node.file_id != file_id {
122+
continue;
123+
}
124+
125+
unresolved_references.retain(|range| !node.value.text_range().contains_range(*range));
126+
}
127+
128+
unresolved_references
129+
}
130+
131+
fn all_unresolved_references(
132+
sema: &Semantics<'_, RootDatabase>,
133+
file_id: FileId,
134+
) -> Vec<TextRange> {
135+
let file_id = sema
136+
.attach_first_edition(file_id)
137+
.unwrap_or_else(|| EditionedFileId::current_edition(file_id));
138+
let file = sema.parse(file_id);
139+
let root = file.syntax();
140+
141+
let mut unresolved_references = Vec::new();
142+
for event in root.preorder() {
143+
let WalkEvent::Enter(syntax) = event else {
144+
continue;
145+
};
146+
let Some(ast::NameLike::NameRef(name_ref)) = ast::NameLike::cast(syntax) else {
147+
continue;
148+
};
149+
150+
// if we can classify the name_ref, it's not unresolved
151+
if NameRefClass::classify(&sema, &name_ref).is_some() {
152+
continue;
153+
}
154+
155+
// if we couldn't classify it, try descending into macros and classifying that
156+
let Some(ast::NameLike::NameRef(descended_name_ref)) = name_ref
157+
.Self_token()
158+
.or_else(|| name_ref.crate_token())
159+
.or_else(|| name_ref.ident_token())
160+
.or_else(|| name_ref.int_number_token())
161+
.or_else(|| name_ref.self_token())
162+
.or_else(|| name_ref.super_token())
163+
.and_then(|token| {
164+
sema.descend_into_macros_single_exact(token).parent().and_then(ast::NameLike::cast)
165+
})
166+
else {
167+
continue;
168+
};
169+
170+
if NameRefClass::classify(&sema, &descended_name_ref).is_some() {
171+
continue;
172+
}
173+
174+
// if we still couldn't classify it, but it's in an attr, ignore it. See #10935
175+
if descended_name_ref.syntax().ancestors().any(|it| it.kind() == SyntaxKind::ATTR)
176+
&& !sema
177+
.hir_file_for(descended_name_ref.syntax())
178+
.macro_file()
179+
.map_or(false, |it| it.is_derive_attr_pseudo_expansion(sema.db))
180+
{
181+
continue;
182+
}
183+
184+
// otherwise, it's unresolved
185+
unresolved_references.push(name_ref.syntax().text_range());
186+
}
187+
unresolved_references
188+
}

0 commit comments

Comments
 (0)