diff options
author | Igor Aleksanov <[email protected]> | 2020-10-03 13:02:46 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-10-12 08:59:54 +0100 |
commit | 21dd704b6b28374ea7bd2d1e13469be6807c4a8d (patch) | |
tree | e264fa53197cb3a6c3a59e48a7dc18b38dfdd111 /crates/hir_ty | |
parent | 329626124f360feadb47e83be5690861c62a4b70 (diff) |
Check structure fields to be snake_case
Diffstat (limited to 'crates/hir_ty')
-rw-r--r-- | crates/hir_ty/src/diagnostics/decl_check.rs | 111 |
1 files changed, 65 insertions, 46 deletions
diff --git a/crates/hir_ty/src/diagnostics/decl_check.rs b/crates/hir_ty/src/diagnostics/decl_check.rs index b7f511fd8..260aa9607 100644 --- a/crates/hir_ty/src/diagnostics/decl_check.rs +++ b/crates/hir_ty/src/diagnostics/decl_check.rs | |||
@@ -23,7 +23,10 @@ use hir_def::{ | |||
23 | src::HasSource, | 23 | src::HasSource, |
24 | AdtId, EnumId, FunctionId, Lookup, ModuleDefId, StructId, | 24 | AdtId, EnumId, FunctionId, Lookup, ModuleDefId, StructId, |
25 | }; | 25 | }; |
26 | use hir_expand::{diagnostics::DiagnosticSink, name::Name}; | 26 | use hir_expand::{ |
27 | diagnostics::DiagnosticSink, | ||
28 | name::{AsName, Name}, | ||
29 | }; | ||
27 | use syntax::{ | 30 | use syntax::{ |
28 | ast::{self, NameOwner}, | 31 | ast::{self, NameOwner}, |
29 | AstPtr, | 32 | AstPtr, |
@@ -288,51 +291,49 @@ impl<'a, 'b> DeclValidator<'a, 'b> { | |||
288 | self.sink.push(diagnostic); | 291 | self.sink.push(diagnostic); |
289 | } | 292 | } |
290 | 293 | ||
291 | // let fn_params_list = match fn_src.value.param_list() { | 294 | let struct_fields_list = match struct_src.value.field_list() { |
292 | // Some(params) => params, | 295 | Some(ast::FieldList::RecordFieldList(fields)) => fields, |
293 | // None => { | 296 | _ => { |
294 | // if !fn_param_replacements.is_empty() { | 297 | if !struct_fields_replacements.is_empty() { |
295 | // log::error!( | 298 | log::error!( |
296 | // "Replacements ({:?}) were generated for a function parameters which had no parameters list: {:?}", | 299 | "Replacements ({:?}) were generated for a structure fields which had no fields list: {:?}", |
297 | // fn_param_replacements, fn_src | 300 | struct_fields_replacements, struct_src |
298 | // ); | 301 | ); |
299 | // } | 302 | } |
300 | // return; | 303 | return; |
301 | // } | 304 | } |
302 | // }; | 305 | }; |
303 | // let mut fn_params_iter = fn_params_list.params(); | 306 | let mut struct_fields_iter = struct_fields_list.fields(); |
304 | // for param_to_rename in fn_param_replacements { | 307 | for field_to_rename in struct_fields_replacements { |
305 | // // We assume that parameters in replacement are in the same order as in the | 308 | // We assume that parameters in replacement are in the same order as in the |
306 | // // actual params list, but just some of them (ones that named correctly) are skipped. | 309 | // actual params list, but just some of them (ones that named correctly) are skipped. |
307 | // let ast_ptr = loop { | 310 | let ast_ptr = loop { |
308 | // match fn_params_iter.next() { | 311 | match struct_fields_iter.next() { |
309 | // Some(element) | 312 | Some(element) if names_equal(element.name(), &field_to_rename.current_name) => { |
310 | // if pat_equals_to_name(element.pat(), ¶m_to_rename.current_name) => | 313 | break element.name().unwrap() |
311 | // { | 314 | } |
312 | // break element.pat().unwrap() | 315 | Some(_) => {} |
313 | // } | 316 | None => { |
314 | // Some(_) => {} | 317 | log::error!( |
315 | // None => { | 318 | "Replacement ({:?}) was generated for a function parameter which was not found: {:?}", |
316 | // log::error!( | 319 | field_to_rename, struct_src |
317 | // "Replacement ({:?}) was generated for a function parameter which was not found: {:?}", | 320 | ); |
318 | // param_to_rename, fn_src | 321 | return; |
319 | // ); | 322 | } |
320 | // return; | 323 | } |
321 | // } | 324 | }; |
322 | // } | 325 | |
323 | // }; | 326 | let diagnostic = IncorrectCase { |
324 | 327 | file: struct_src.file_id, | |
325 | // let diagnostic = IncorrectCase { | 328 | ident_type: "Field".to_string(), |
326 | // file: fn_src.file_id, | 329 | ident: AstPtr::new(&ast_ptr).into(), |
327 | // ident_type: "Argument".to_string(), | 330 | expected_case: field_to_rename.expected_case, |
328 | // ident: AstPtr::new(&ast_ptr).into(), | 331 | ident_text: field_to_rename.current_name.to_string(), |
329 | // expected_case: param_to_rename.expected_case, | 332 | suggested_text: field_to_rename.suggested_text, |
330 | // ident_text: param_to_rename.current_name.to_string(), | 333 | }; |
331 | // suggested_text: param_to_rename.suggested_text, | 334 | |
332 | // }; | 335 | self.sink.push(diagnostic); |
333 | 336 | } | |
334 | // self.sink.push(diagnostic); | ||
335 | // } | ||
336 | } | 337 | } |
337 | 338 | ||
338 | fn validate_enum(&mut self, db: &dyn HirDatabase, enum_id: EnumId) { | 339 | fn validate_enum(&mut self, db: &dyn HirDatabase, enum_id: EnumId) { |
@@ -340,6 +341,14 @@ impl<'a, 'b> DeclValidator<'a, 'b> { | |||
340 | } | 341 | } |
341 | } | 342 | } |
342 | 343 | ||
344 | fn names_equal(left: Option<ast::Name>, right: &Name) -> bool { | ||
345 | if let Some(left) = left { | ||
346 | &left.as_name() == right | ||
347 | } else { | ||
348 | false | ||
349 | } | ||
350 | } | ||
351 | |||
343 | fn pat_equals_to_name(pat: Option<ast::Pat>, name: &Name) -> bool { | 352 | fn pat_equals_to_name(pat: Option<ast::Pat>, name: &Name) -> bool { |
344 | if let Some(ast::Pat::IdentPat(ident)) = pat { | 353 | if let Some(ast::Pat::IdentPat(ident)) = pat { |
345 | ident.to_string() == name.to_string() | 354 | ident.to_string() == name.to_string() |
@@ -384,4 +393,14 @@ struct non_camel_case_name {} | |||
384 | "#, | 393 | "#, |
385 | ); | 394 | ); |
386 | } | 395 | } |
396 | |||
397 | #[test] | ||
398 | fn incorrect_struct_field() { | ||
399 | check_diagnostics( | ||
400 | r#" | ||
401 | struct SomeStruct { SomeField: u8 } | ||
402 | // ^^^^^^^^^ Field `SomeField` should have a snake_case name, e.g. `some_field` | ||
403 | "#, | ||
404 | ); | ||
405 | } | ||
387 | } | 406 | } |