diff options
author | Igor Aleksanov <[email protected]> | 2020-10-03 12:35:26 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-10-12 08:59:54 +0100 |
commit | 1773c6d154abe5da00b31bb16139addcaa443bbb (patch) | |
tree | 1b6f0c6b6a031bcc74d3a46004553fcdaf6d0d16 /crates/hir_ty/src/diagnostics/decl_check.rs | |
parent | f5cea35986a0c8182ca427f10e20bc97ec564315 (diff) |
Extract helper functions into a separate module
Diffstat (limited to 'crates/hir_ty/src/diagnostics/decl_check.rs')
-rw-r--r-- | crates/hir_ty/src/diagnostics/decl_check.rs | 49 |
1 files changed, 20 insertions, 29 deletions
diff --git a/crates/hir_ty/src/diagnostics/decl_check.rs b/crates/hir_ty/src/diagnostics/decl_check.rs index 083df3772..1a0906492 100644 --- a/crates/hir_ty/src/diagnostics/decl_check.rs +++ b/crates/hir_ty/src/diagnostics/decl_check.rs | |||
@@ -9,6 +9,8 @@ | |||
9 | // If you see these lines in the pull request, feel free to call me stupid :P. | 9 | // If you see these lines in the pull request, feel free to call me stupid :P. |
10 | #![allow(dead_code, unused_imports, unused_variables)] | 10 | #![allow(dead_code, unused_imports, unused_variables)] |
11 | 11 | ||
12 | mod str_helpers; | ||
13 | |||
12 | use std::sync::Arc; | 14 | use std::sync::Arc; |
13 | 15 | ||
14 | use hir_def::{ | 16 | use hir_def::{ |
@@ -18,7 +20,7 @@ use hir_def::{ | |||
18 | item_tree::ItemTreeNode, | 20 | item_tree::ItemTreeNode, |
19 | resolver::{resolver_for_expr, ResolveValueResult, ValueNs}, | 21 | resolver::{resolver_for_expr, ResolveValueResult, ValueNs}, |
20 | src::HasSource, | 22 | src::HasSource, |
21 | AdtId, FunctionId, Lookup, ModuleDefId, | 23 | AdtId, EnumId, FunctionId, Lookup, ModuleDefId, StructId, |
22 | }; | 24 | }; |
23 | use hir_expand::{diagnostics::DiagnosticSink, name::Name}; | 25 | use hir_expand::{diagnostics::DiagnosticSink, name::Name}; |
24 | use syntax::{ | 26 | use syntax::{ |
@@ -28,7 +30,7 @@ use syntax::{ | |||
28 | 30 | ||
29 | use crate::{ | 31 | use crate::{ |
30 | db::HirDatabase, | 32 | db::HirDatabase, |
31 | diagnostics::{CaseType, IncorrectCase}, | 33 | diagnostics::{decl_check::str_helpers::*, CaseType, IncorrectCase}, |
32 | lower::CallableDefId, | 34 | lower::CallableDefId, |
33 | ApplicationTy, InferenceResult, Ty, TypeCtor, | 35 | ApplicationTy, InferenceResult, Ty, TypeCtor, |
34 | }; | 36 | }; |
@@ -191,41 +193,30 @@ impl<'a, 'b> DeclValidator<'a, 'b> { | |||
191 | } | 193 | } |
192 | } | 194 | } |
193 | 195 | ||
194 | fn validate_adt(&mut self, db: &dyn HirDatabase, adt: AdtId) {} | 196 | fn validate_adt(&mut self, db: &dyn HirDatabase, adt: AdtId) { |
195 | } | 197 | match adt { |
196 | 198 | AdtId::StructId(struct_id) => self.validate_struct(db, struct_id), | |
197 | fn pat_equals_to_name(pat: Option<ast::Pat>, name: &Name) -> bool { | 199 | AdtId::EnumId(enum_id) => self.validate_enum(db, enum_id), |
198 | if let Some(ast::Pat::IdentPat(ident)) = pat { | 200 | AdtId::UnionId(_) => { |
199 | ident.to_string() == name.to_string() | 201 | // Unions aren't yet supported by this validator. |
200 | } else { | 202 | } |
201 | false | 203 | } |
202 | } | 204 | } |
203 | } | ||
204 | 205 | ||
205 | fn to_lower_snake_case(ident: &str) -> Option<String> { | 206 | fn validate_struct(&mut self, db: &dyn HirDatabase, struct_id: StructId) { |
206 | // First, assume that it's UPPER_SNAKE_CASE. | 207 | let data = db.struct_data(struct_id); |
207 | if let Some(normalized) = to_lower_snake_case_from_upper_snake_case(ident) { | ||
208 | return Some(normalized); | ||
209 | } | 208 | } |
210 | 209 | ||
211 | // Otherwise, assume that it's CamelCase. | 210 | fn validate_enum(&mut self, db: &dyn HirDatabase, enum_id: EnumId) { |
212 | let lower_snake_case = stdx::to_lower_snake_case(ident); | 211 | let data = db.enum_data(enum_id); |
213 | |||
214 | if lower_snake_case == ident { | ||
215 | None | ||
216 | } else { | ||
217 | Some(lower_snake_case) | ||
218 | } | 212 | } |
219 | } | 213 | } |
220 | 214 | ||
221 | fn to_lower_snake_case_from_upper_snake_case(ident: &str) -> Option<String> { | 215 | fn pat_equals_to_name(pat: Option<ast::Pat>, name: &Name) -> bool { |
222 | let is_upper_snake_case = ident.chars().all(|c| c.is_ascii_uppercase() || c == '_'); | 216 | if let Some(ast::Pat::IdentPat(ident)) = pat { |
223 | 217 | ident.to_string() == name.to_string() | |
224 | if is_upper_snake_case { | ||
225 | let string = ident.chars().map(|c| c.to_ascii_lowercase()).collect(); | ||
226 | Some(string) | ||
227 | } else { | 218 | } else { |
228 | None | 219 | false |
229 | } | 220 | } |
230 | } | 221 | } |
231 | 222 | ||