Skip to content

Commit 3812951

Browse files
committed
Auto merge of rust-lang#14123 - dqkqd:discard-postfix-completion-for-indivisble-expr, r=Veykril
fix: Don't trigger postfix completion in `if` block which has an `else` block Fix rust-lang#14096
2 parents a33e9d9 + 0285acc commit 3812951

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

crates/ide-completion/src/completions/postfix.rs

+12
Original file line numberDiff line numberDiff line change
@@ -747,4 +747,16 @@ fn main() {
747747
"#,
748748
);
749749
}
750+
751+
#[test]
752+
fn no_postfix_completions_in_if_block_that_has_an_else() {
753+
check(
754+
r#"
755+
fn test() {
756+
if true {}.$0 else {}
757+
}
758+
"#,
759+
expect![[r#""#]],
760+
);
761+
}
750762
}

crates/ide-completion/src/context/analysis.rs

+28
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,18 @@ fn classify_name_ref(
605605
},
606606
_ => false,
607607
};
608+
609+
let reciever_is_part_of_indivisible_expression = match &receiver {
610+
Some(ast::Expr::IfExpr(_)) => {
611+
let next_token_kind = next_non_trivia_token(name_ref.syntax().clone()).map(|t| t.kind());
612+
next_token_kind == Some(SyntaxKind::ELSE_KW)
613+
},
614+
_ => false
615+
};
616+
if reciever_is_part_of_indivisible_expression {
617+
return None;
618+
}
619+
608620
let kind = NameRefKind::DotAccess(DotAccess {
609621
receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
610622
kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal },
@@ -1317,6 +1329,22 @@ fn previous_non_trivia_token(e: impl Into<SyntaxElement>) -> Option<SyntaxToken>
13171329
None
13181330
}
13191331

1332+
fn next_non_trivia_token(e: impl Into<SyntaxElement>) -> Option<SyntaxToken> {
1333+
let mut token = match e.into() {
1334+
SyntaxElement::Node(n) => n.last_token()?,
1335+
SyntaxElement::Token(t) => t,
1336+
}
1337+
.next_token();
1338+
while let Some(inner) = token {
1339+
if !inner.kind().is_trivia() {
1340+
return Some(inner);
1341+
} else {
1342+
token = inner.next_token();
1343+
}
1344+
}
1345+
None
1346+
}
1347+
13201348
fn next_non_trivia_sibling(ele: SyntaxElement) -> Option<SyntaxElement> {
13211349
let mut e = ele.next_sibling_or_token();
13221350
while let Some(inner) = e {

0 commit comments

Comments
 (0)