Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a0a7860

Browse files
committed
Refactor
1 parent a4966c9 commit a0a7860

File tree

2 files changed

+67
-64
lines changed

2 files changed

+67
-64
lines changed

crates/hir-def/src/nameres/collector.rs

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -289,80 +289,84 @@ impl DefCollector<'_> {
289289
let module_id = self.def_map.root;
290290

291291
let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
292-
if attrs.cfg().map_or(true, |cfg| self.cfg_options.check(&cfg) != Some(false)) {
293-
self.inject_prelude(&attrs);
294-
295-
// Process other crate-level attributes.
296-
for attr in &*attrs {
297-
let attr_name = match attr.path.as_ident() {
298-
Some(name) => name,
299-
None => continue,
300-
};
292+
if let Some(cfg) = attrs.cfg() {
293+
if self.cfg_options.check(&cfg) == Some(false) {
294+
return;
295+
}
296+
}
301297

302-
if *attr_name == hir_expand::name![recursion_limit] {
303-
if let Some(limit) = attr.string_value() {
304-
if let Ok(limit) = limit.parse() {
305-
self.def_map.recursion_limit = Some(limit);
306-
}
307-
}
308-
continue;
309-
}
298+
self.inject_prelude(&attrs);
310299

311-
if *attr_name == hir_expand::name![crate_type] {
312-
if let Some("proc-macro") = attr.string_value().map(SmolStr::as_str) {
313-
self.is_proc_macro = true;
314-
}
315-
continue;
316-
}
300+
// Process other crate-level attributes.
301+
for attr in &*attrs {
302+
let attr_name = match attr.path.as_ident() {
303+
Some(name) => name,
304+
None => continue,
305+
};
317306

318-
if attr_name.as_text().as_deref() == Some("rustc_coherence_is_core") {
319-
self.def_map.rustc_coherence_is_core = true;
320-
continue;
307+
if *attr_name == hir_expand::name![recursion_limit] {
308+
if let Some(limit) = attr.string_value() {
309+
if let Ok(limit) = limit.parse() {
310+
self.def_map.recursion_limit = Some(limit);
311+
}
321312
}
313+
continue;
314+
}
322315

323-
if *attr_name == hir_expand::name![feature] {
324-
let hygiene = &Hygiene::new_unhygienic();
325-
let features = attr
326-
.parse_path_comma_token_tree(self.db.upcast(), hygiene)
327-
.into_iter()
328-
.flatten()
329-
.filter_map(|feat| match feat.segments() {
330-
[name] => Some(name.to_smol_str()),
331-
_ => None,
332-
});
333-
self.def_map.unstable_features.extend(features);
316+
if *attr_name == hir_expand::name![crate_type] {
317+
if let Some("proc-macro") = attr.string_value().map(SmolStr::as_str) {
318+
self.is_proc_macro = true;
334319
}
320+
continue;
321+
}
335322

336-
let attr_is_register_like = *attr_name == hir_expand::name![register_attr]
337-
|| *attr_name == hir_expand::name![register_tool];
338-
if !attr_is_register_like {
339-
continue;
340-
}
323+
if attr_name.as_text().as_deref() == Some("rustc_coherence_is_core") {
324+
self.def_map.rustc_coherence_is_core = true;
325+
continue;
326+
}
341327

342-
let registered_name = match attr.single_ident_value() {
343-
Some(ident) => ident.as_name(),
344-
_ => continue,
345-
};
328+
if *attr_name == hir_expand::name![feature] {
329+
let hygiene = &Hygiene::new_unhygienic();
330+
let features = attr
331+
.parse_path_comma_token_tree(self.db.upcast(), hygiene)
332+
.into_iter()
333+
.flatten()
334+
.filter_map(|feat| match feat.segments() {
335+
[name] => Some(name.to_smol_str()),
336+
_ => None,
337+
});
338+
self.def_map.unstable_features.extend(features);
339+
}
346340

347-
if *attr_name == hir_expand::name![register_attr] {
348-
self.def_map.registered_attrs.push(registered_name.to_smol_str());
349-
cov_mark::hit!(register_attr);
350-
} else {
351-
self.def_map.registered_tools.push(registered_name.to_smol_str());
352-
cov_mark::hit!(register_tool);
353-
}
341+
let attr_is_register_like = *attr_name == hir_expand::name![register_attr]
342+
|| *attr_name == hir_expand::name![register_tool];
343+
if !attr_is_register_like {
344+
continue;
354345
}
355346

356-
ModCollector {
357-
def_collector: self,
358-
macro_depth: 0,
359-
module_id,
360-
tree_id: TreeId::new(file_id.into(), None),
361-
item_tree: &item_tree,
362-
mod_dir: ModDir::root(),
347+
let registered_name = match attr.single_ident_value() {
348+
Some(ident) => ident.as_name(),
349+
_ => continue,
350+
};
351+
352+
if *attr_name == hir_expand::name![register_attr] {
353+
self.def_map.registered_attrs.push(registered_name.to_smol_str());
354+
cov_mark::hit!(register_attr);
355+
} else {
356+
self.def_map.registered_tools.push(registered_name.to_smol_str());
357+
cov_mark::hit!(register_tool);
363358
}
364-
.collect_in_top_module(item_tree.top_level_items());
365359
}
360+
361+
ModCollector {
362+
def_collector: self,
363+
macro_depth: 0,
364+
module_id,
365+
tree_id: TreeId::new(file_id.into(), None),
366+
item_tree: &item_tree,
367+
mod_dir: ModDir::root(),
368+
}
369+
.collect_in_top_module(item_tree.top_level_items());
366370
}
367371

368372
fn seed_with_inner(&mut self, tree_id: TreeId) {

crates/hir/src/attrs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use hir_def::{
44
attr::{AttrsWithOwner, Documentation},
55
item_scope::ItemInNs,
66
path::ModPath,
7-
per_ns::PerNs,
87
resolver::HasResolver,
98
AttrDefId, GenericParamId, ModuleDefId,
109
};
@@ -155,14 +154,14 @@ fn resolve_doc_path(
155154
.syntax_node()
156155
.descendants()
157156
.find_map(ast::Path::cast)?;
158-
if ast_path.to_string() != link {
157+
if ast_path.syntax().text() != link {
159158
return None;
160159
}
161160
ModPath::from_src(db.upcast(), ast_path, &Hygiene::new_unhygienic())?
162161
};
163162

164163
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
165-
let resolved = if resolved == PerNs::none() {
164+
let resolved = if resolved.is_none() {
166165
resolver.resolve_module_path_in_trait_assoc_items(db.upcast(), &modpath)?
167166
} else {
168167
resolved

0 commit comments

Comments
 (0)