diff options
Diffstat (limited to 'crates/hir_ty/src/diagnostics/decl_check.rs')
-rw-r--r-- | crates/hir_ty/src/diagnostics/decl_check.rs | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/crates/hir_ty/src/diagnostics/decl_check.rs b/crates/hir_ty/src/diagnostics/decl_check.rs index d1c51849a..3a95f1b82 100644 --- a/crates/hir_ty/src/diagnostics/decl_check.rs +++ b/crates/hir_ty/src/diagnostics/decl_check.rs | |||
@@ -19,7 +19,7 @@ use hir_expand::{ | |||
19 | }; | 19 | }; |
20 | use syntax::{ | 20 | use syntax::{ |
21 | ast::{self, NameOwner}, | 21 | ast::{self, NameOwner}, |
22 | AstPtr, | 22 | AstNode, AstPtr, |
23 | }; | 23 | }; |
24 | 24 | ||
25 | use crate::{ | 25 | use crate::{ |
@@ -259,6 +259,21 @@ impl<'a, 'b> DeclValidator<'a, 'b> { | |||
259 | if let Some(expr) = source_ptr.value.as_ref().left() { | 259 | if let Some(expr) = source_ptr.value.as_ref().left() { |
260 | let root = source_ptr.file_syntax(db.upcast()); | 260 | let root = source_ptr.file_syntax(db.upcast()); |
261 | if let ast::Pat::IdentPat(ident_pat) = expr.to_node(&root) { | 261 | if let ast::Pat::IdentPat(ident_pat) = expr.to_node(&root) { |
262 | let parent = match ident_pat.syntax().parent() { | ||
263 | Some(parent) => parent, | ||
264 | None => continue, | ||
265 | }; | ||
266 | |||
267 | // We have to check that it's either `let var = ...` or `Variant(_) @ var` statement, | ||
268 | // because e.g. match arms are patterns as well. | ||
269 | // 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 | { | ||
273 | // This pattern is not an actual variable declaration, e.g. `Some(val) => {..}` match arm. | ||
274 | continue; | ||
275 | } | ||
276 | |||
262 | let diagnostic = IncorrectCase { | 277 | let diagnostic = IncorrectCase { |
263 | file: source_ptr.file_id, | 278 | file: source_ptr.file_id, |
264 | ident_type: "Variable".to_string(), | 279 | ident_type: "Variable".to_string(), |
@@ -663,7 +678,7 @@ fn foo2(ok_param: &str, CAPS_PARAM: u8) {} | |||
663 | r#" | 678 | r#" |
664 | fn foo() { | 679 | fn foo() { |
665 | let SOME_VALUE = 10; | 680 | let SOME_VALUE = 10; |
666 | // ^^^^^^^^^^ Variable `SOME_VALUE` should have a snake_case name, e.g. `some_value` | 681 | // ^^^^^^^^^^ Variable `SOME_VALUE` should have snake_case name, e.g. `some_value` |
667 | let AnotherValue = 20; | 682 | let AnotherValue = 20; |
668 | // ^^^^^^^^^^^^ Variable `AnotherValue` should have snake_case name, e.g. `another_value` | 683 | // ^^^^^^^^^^^^ Variable `AnotherValue` should have snake_case name, e.g. `another_value` |
669 | } | 684 | } |
@@ -761,4 +776,37 @@ impl someStruct { | |||
761 | "#, | 776 | "#, |
762 | ); | 777 | ); |
763 | } | 778 | } |
779 | |||
780 | #[test] | ||
781 | fn no_diagnostic_for_enum_varinats() { | ||
782 | check_diagnostics( | ||
783 | r#" | ||
784 | enum Option { Some, None } | ||
785 | |||
786 | fn main() { | ||
787 | match Option::None { | ||
788 | None => (), | ||
789 | Some => (), | ||
790 | } | ||
791 | } | ||
792 | "#, | ||
793 | ); | ||
794 | } | ||
795 | |||
796 | #[test] | ||
797 | fn non_let_bind() { | ||
798 | check_diagnostics( | ||
799 | r#" | ||
800 | enum Option { Some, None } | ||
801 | |||
802 | fn main() { | ||
803 | match Option::None { | ||
804 | None @ SOME_VAR => (), | ||
805 | // ^^^^^^^^ Variable `SOME_VAR` should have snake_case name, e.g. `some_var` | ||
806 | Some => (), | ||
807 | } | ||
808 | } | ||
809 | "#, | ||
810 | ); | ||
811 | } | ||
764 | } | 812 | } |