Skip to content

Commit

Permalink
fix: Prevent add_missing_match_arms assist from ignoring existing v…
Browse files Browse the repository at this point in the history
…ariants prefixed with `Self::`

This is an ad hoc fix of rust-lang#4650 by making only the last identifier
segment compared when the variant is prefixed, so that the comparison
is not affected by differences in prefix, such as type names, type
aliases, or `Self::`.

(The appropriate fix would be to use `hir_ty::diagnostics::match_check`,
as discussed in rust-lang#8493.)

Closes rust-lang#4650.
  • Loading branch information
noritada committed Dec 25, 2022
1 parent bb8bf15 commit 324cf8b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions crates/ide-assists/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,22 @@ pub(crate) fn next_prev() -> impl Iterator<Item = Direction> {

pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
let first_node_text = |pat: &ast::Pat| pat.syntax().first_child().map(|node| node.text());
let last_segment_text =
|path: &ast::Path| path.segments().last().map(|segment| segment.syntax().text());
let normalize_pat = |pat: &ast::Pat| match pat {
ast::Pat::PathPat(p) => p.path().and_then(|p| last_segment_text(&p)),
ast::Pat::TupleStructPat(p) => p.path().and_then(|p| last_segment_text(&p)),
_ => first_node_text(&pat),
};

let pat_head = match pat {
ast::Pat::IdentPat(bind_pat) => match bind_pat.pat() {
Some(p) => first_node_text(&p),
Some(p) => normalize_pat(&p),
None => return pat.syntax().text() == var.syntax().text(),
},
pat => first_node_text(pat),
_ => normalize_pat(pat),
};

let var_head = first_node_text(var);
let var_head = normalize_pat(var);

pat_head == var_head
}
Expand Down

0 comments on commit 324cf8b

Please sign in to comment.