Skip to content

Commit 56b5185

Browse files
committed
minor: dedup
1 parent 726da98 commit 56b5185

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

crates/hir/src/semantics.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir_def::{
1010
resolver::{self, HasResolver, Resolver, TypeNs},
1111
AsMacroCall, FunctionId, TraitId, VariantId,
1212
};
13-
use hir_expand::{name::AsName, ExpansionInfo, MacroCallLoc};
13+
use hir_expand::{name::AsName, ExpansionInfo, MacroCallId, MacroCallLoc};
1414
use hir_ty::{associated_type_shorthand_candidates, Interner};
1515
use itertools::Itertools;
1616
use rustc_hash::{FxHashMap, FxHashSet};
@@ -448,46 +448,45 @@ impl<'db> SemanticsImpl<'db> {
448448
}
449449

450450
fn resolve_derive_macro(&self, attr: &ast::Attr) -> Option<Vec<MacroDef>> {
451-
let item = attr.syntax().parent().and_then(ast::Item::cast)?;
452-
let file_id = self.find_file(item.syntax()).file_id;
453-
let item = InFile::new(file_id, &item);
454-
let src = InFile::new(file_id, attr.clone());
455-
self.with_ctx(|ctx| {
456-
let macro_call_ids = ctx.attr_to_derive_macro_call(item, src)?;
457-
458-
let res = macro_call_ids
459-
.iter()
460-
.map(|&call| {
461-
let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call);
462-
MacroDef { id: loc.def }
463-
})
464-
.collect();
465-
Some(res)
466-
})
451+
let macro_call_ids = self.derive_macro_calls(attr)?;
452+
let res = macro_call_ids
453+
.iter()
454+
.map(|&call| {
455+
let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call);
456+
MacroDef { id: loc.def }
457+
})
458+
.collect();
459+
Some(res)
467460
}
468461

469462
fn expand_derive_macro(&self, attr: &ast::Attr) -> Option<Vec<SyntaxNode>> {
463+
let macro_call_ids = self.derive_macro_calls(attr)?;
464+
465+
let expansions: Vec<_> = macro_call_ids
466+
.iter()
467+
.map(|call| call.as_file())
468+
.flat_map(|file_id| {
469+
let node = self.db.parse_or_expand(file_id)?;
470+
self.cache(node.clone(), file_id);
471+
Some(node)
472+
})
473+
.collect();
474+
475+
if expansions.is_empty() {
476+
None
477+
} else {
478+
Some(expansions)
479+
}
480+
}
481+
482+
fn derive_macro_calls(&self, attr: &ast::Attr) -> Option<Vec<MacroCallId>> {
470483
let item = attr.syntax().parent().and_then(ast::Item::cast)?;
471484
let file_id = self.find_file(item.syntax()).file_id;
472485
let item = InFile::new(file_id, &item);
473486
let src = InFile::new(file_id, attr.clone());
474487
self.with_ctx(|ctx| {
475-
let macro_call_ids = ctx.attr_to_derive_macro_call(item, src)?;
476-
477-
let expansions: Vec<_> = macro_call_ids
478-
.iter()
479-
.map(|call| call.as_file())
480-
.flat_map(|file_id| {
481-
let node = self.db.parse_or_expand(file_id)?;
482-
self.cache(node.clone(), file_id);
483-
Some(node)
484-
})
485-
.collect();
486-
if expansions.is_empty() {
487-
None
488-
} else {
489-
Some(expansions)
490-
}
488+
let res = ctx.attr_to_derive_macro_call(item, src)?;
489+
Some(res.to_vec())
491490
})
492491
}
493492

0 commit comments

Comments
 (0)