Skip to content

Commit 6189be1

Browse files
committed
Auto merge of rust-lang#13952 - WaffleLapkin:either_ast_node2, r=lnicola
minor: Use the fact that `Either`: `AstNode` Continuation of rust-lang/rust-analyzer#13949
2 parents c78b9f0 + a778753 commit 6189be1

File tree

5 files changed

+8
-34
lines changed

5 files changed

+8
-34
lines changed

crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ pub(crate) fn convert_named_struct_to_tuple_struct(
5252
acc: &mut Assists,
5353
ctx: &AssistContext<'_>,
5454
) -> Option<()> {
55-
let strukt = ctx
56-
.find_node_at_offset::<ast::Struct>()
57-
.map(Either::Left)
58-
.or_else(|| ctx.find_node_at_offset::<ast::Variant>().map(Either::Right))?;
55+
let strukt = ctx.find_node_at_offset::<Either<ast::Struct, ast::Variant>>()?;
5956
let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?;
6057
let record_fields = match field_list {
6158
ast::FieldList::RecordFieldList(it) => it,

crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ pub(crate) fn convert_tuple_struct_to_named_struct(
5050
acc: &mut Assists,
5151
ctx: &AssistContext<'_>,
5252
) -> Option<()> {
53-
let strukt = ctx
54-
.find_node_at_offset::<ast::Struct>()
55-
.map(Either::Left)
56-
.or_else(|| ctx.find_node_at_offset::<ast::Variant>().map(Either::Right))?;
53+
let strukt = ctx.find_node_at_offset::<Either<ast::Struct, ast::Variant>>()?;
5754
let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?;
5855
let tuple_fields = match field_list {
5956
ast::FieldList::TupleFieldList(it) => it,

crates/ide-assists/src/handlers/extract_type_alias.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use either::Either;
22
use ide_db::syntax_helpers::node_ext::walk_ty;
3-
use syntax::{
4-
ast::{self, edit::IndentLevel, make, AstNode, HasGenericParams, HasName},
5-
match_ast,
6-
};
3+
use syntax::ast::{self, edit::IndentLevel, make, AstNode, HasGenericParams, HasName};
74

85
use crate::{AssistContext, AssistId, AssistKind, Assists};
96

@@ -31,15 +28,8 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
3128

3229
let ty = ctx.find_node_at_range::<ast::Type>()?;
3330
let item = ty.syntax().ancestors().find_map(ast::Item::cast)?;
34-
let assoc_owner = item.syntax().ancestors().nth(2).and_then(|it| {
35-
match_ast! {
36-
match it {
37-
ast::Trait(tr) => Some(Either::Left(tr)),
38-
ast::Impl(impl_) => Some(Either::Right(impl_)),
39-
_ => None,
40-
}
41-
}
42-
});
31+
let assoc_owner =
32+
item.syntax().ancestors().nth(2).and_then(Either::<ast::Trait, ast::Impl>::cast);
4333
let node = assoc_owner.as_ref().map_or_else(
4434
|| item.syntax(),
4535
|impl_| impl_.as_ref().either(AstNode::syntax, AstNode::syntax),

crates/ide-assists/src/handlers/reorder_fields.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2020
// const test: Foo = Foo {foo: 1, bar: 0}
2121
// ```
2222
pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
23-
let record = ctx
24-
.find_node_at_offset::<ast::RecordExpr>()
25-
.map(Either::Left)
26-
.or_else(|| ctx.find_node_at_offset::<ast::RecordPat>().map(Either::Right))?;
23+
let record = ctx.find_node_at_offset::<Either<ast::RecordExpr, ast::RecordPat>>()?;
2724

2825
let path = record.as_ref().either(|it| it.path(), |it| it.path())?;
2926
let ranks = compute_fields_ranks(&path, ctx)?;

crates/ide/src/hover.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,8 @@ fn hover_ranged(
230230
config: &HoverConfig,
231231
) -> Option<RangeInfo<HoverResult>> {
232232
// FIXME: make this work in attributes
233-
let expr_or_pat = file.covering_element(range).ancestors().find_map(|it| {
234-
match_ast! {
235-
match it {
236-
ast::Expr(expr) => Some(Either::Left(expr)),
237-
ast::Pat(pat) => Some(Either::Right(pat)),
238-
_ => None,
239-
}
240-
}
241-
})?;
233+
let expr_or_pat =
234+
file.covering_element(range).ancestors().find_map(Either::<ast::Expr, ast::Pat>::cast)?;
242235
let res = match &expr_or_pat {
243236
Either::Left(ast::Expr::TryExpr(try_expr)) => render::try_expr(sema, config, try_expr),
244237
Either::Left(ast::Expr::PrefixExpr(prefix_expr))

0 commit comments

Comments
 (0)