Skip to content

Commit 2a72f87

Browse files
committed
Fix issues with match arm bindings
1 parent 45ac2b2 commit 2a72f87

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

crates/hir_ty/src/diagnostics/decl_check.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,26 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
263263
Some(parent) => parent,
264264
None => continue,
265265
};
266+
let name_ast = match ident_pat.name() {
267+
Some(name_ast) => name_ast,
268+
None => continue,
269+
};
266270

267-
// We have to check that it's either `let var = ...` or `Variant(_) @ var` statement,
271+
// We have to check that it's either `let var = ...` or `var @ Variant(_)` statement,
268272
// because e.g. match arms are patterns as well.
269273
// In other words, we check that it's a named variable binding.
270-
if !ast::LetStmt::cast(parent.clone()).is_some()
271-
&& !ast::IdentPat::cast(parent).is_some()
272-
{
274+
let is_binding = ast::LetStmt::cast(parent.clone()).is_some()
275+
|| (ast::MatchArm::cast(parent).is_some()
276+
&& ident_pat.at_token().is_some());
277+
if !is_binding {
273278
// This pattern is not an actual variable declaration, e.g. `Some(val) => {..}` match arm.
274279
continue;
275280
}
276281

277282
let diagnostic = IncorrectCase {
278283
file: source_ptr.file_id,
279284
ident_type: "Variable".to_string(),
280-
ident: AstPtr::new(&ident_pat).into(),
285+
ident: AstPtr::new(&name_ast).into(),
281286
expected_case: replacement.expected_case,
282287
ident_text: replacement.current_name.to_string(),
283288
suggested_text: replacement.suggested_text,
@@ -801,8 +806,8 @@ enum Option { Some, None }
801806
802807
fn main() {
803808
match Option::None {
804-
None @ SOME_VAR => (),
805-
// ^^^^^^^^ Variable `SOME_VAR` should have snake_case name, e.g. `some_var`
809+
SOME_VAR @ None => (),
810+
// ^^^^^^^^ Variable `SOME_VAR` should have snake_case name, e.g. `some_var`
806811
Some => (),
807812
}
808813
}

0 commit comments

Comments
 (0)