diff options
author | Phil Ellison <[email protected]> | 2020-12-28 13:41:15 +0000 |
---|---|---|
committer | Phil Ellison <[email protected]> | 2021-01-23 07:40:24 +0000 |
commit | 1316422a7c2ef26e9da78fa23f170407b1cb39bb (patch) | |
tree | 1eea156207bcc920f0573ef51d7f6ad4fe03299d /crates/hir_ty/src/diagnostics.rs | |
parent | eab5db20edd9604ba5d489fa8c6430eb7bac6610 (diff) |
Add diagnostic for filter_map followed by next
Diffstat (limited to 'crates/hir_ty/src/diagnostics.rs')
-rw-r--r-- | crates/hir_ty/src/diagnostics.rs | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/crates/hir_ty/src/diagnostics.rs b/crates/hir_ty/src/diagnostics.rs index 247da43f2..b4cf81c10 100644 --- a/crates/hir_ty/src/diagnostics.rs +++ b/crates/hir_ty/src/diagnostics.rs | |||
@@ -247,7 +247,7 @@ impl Diagnostic for RemoveThisSemicolon { | |||
247 | 247 | ||
248 | // Diagnostic: break-outside-of-loop | 248 | // Diagnostic: break-outside-of-loop |
249 | // | 249 | // |
250 | // This diagnostic is triggered if `break` keyword is used outside of a loop. | 250 | // This diagnostic is triggered if the `break` keyword is used outside of a loop. |
251 | #[derive(Debug)] | 251 | #[derive(Debug)] |
252 | pub struct BreakOutsideOfLoop { | 252 | pub struct BreakOutsideOfLoop { |
253 | pub file: HirFileId, | 253 | pub file: HirFileId, |
@@ -271,7 +271,7 @@ impl Diagnostic for BreakOutsideOfLoop { | |||
271 | 271 | ||
272 | // Diagnostic: missing-unsafe | 272 | // Diagnostic: missing-unsafe |
273 | // | 273 | // |
274 | // This diagnostic is triggered if operation marked as `unsafe` is used outside of `unsafe` function or block. | 274 | // This diagnostic is triggered if an operation marked as `unsafe` is used outside of an `unsafe` function or block. |
275 | #[derive(Debug)] | 275 | #[derive(Debug)] |
276 | pub struct MissingUnsafe { | 276 | pub struct MissingUnsafe { |
277 | pub file: HirFileId, | 277 | pub file: HirFileId, |
@@ -295,7 +295,7 @@ impl Diagnostic for MissingUnsafe { | |||
295 | 295 | ||
296 | // Diagnostic: mismatched-arg-count | 296 | // Diagnostic: mismatched-arg-count |
297 | // | 297 | // |
298 | // This diagnostic is triggered if function is invoked with an incorrect amount of arguments. | 298 | // This diagnostic is triggered if a function is invoked with an incorrect amount of arguments. |
299 | #[derive(Debug)] | 299 | #[derive(Debug)] |
300 | pub struct MismatchedArgCount { | 300 | pub struct MismatchedArgCount { |
301 | pub file: HirFileId, | 301 | pub file: HirFileId, |
@@ -347,7 +347,7 @@ impl fmt::Display for CaseType { | |||
347 | 347 | ||
348 | // Diagnostic: incorrect-ident-case | 348 | // Diagnostic: incorrect-ident-case |
349 | // | 349 | // |
350 | // This diagnostic is triggered if item name doesn't follow https://doc.rust-lang.org/1.0.0/style/style/naming/README.html[Rust naming convention]. | 350 | // This diagnostic is triggered if an item name doesn't follow https://doc.rust-lang.org/1.0.0/style/style/naming/README.html[Rust naming convention]. |
351 | #[derive(Debug)] | 351 | #[derive(Debug)] |
352 | pub struct IncorrectCase { | 352 | pub struct IncorrectCase { |
353 | pub file: HirFileId, | 353 | pub file: HirFileId, |
@@ -386,6 +386,31 @@ impl Diagnostic for IncorrectCase { | |||
386 | } | 386 | } |
387 | } | 387 | } |
388 | 388 | ||
389 | // Diagnostic: replace-filter-map-next-with-find-map | ||
390 | // | ||
391 | // This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`. | ||
392 | #[derive(Debug)] | ||
393 | pub struct ReplaceFilterMapNextWithFindMap { | ||
394 | pub file: HirFileId, | ||
395 | pub filter_map_expr: AstPtr<ast::Expr>, | ||
396 | pub next_expr: AstPtr<ast::Expr>, | ||
397 | } | ||
398 | |||
399 | impl Diagnostic for ReplaceFilterMapNextWithFindMap { | ||
400 | fn code(&self) -> DiagnosticCode { | ||
401 | DiagnosticCode("replace-filter-map-next-with-find-map") | ||
402 | } | ||
403 | fn message(&self) -> String { | ||
404 | "replace filter_map(..).next() with find_map(..)".to_string() | ||
405 | } | ||
406 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
407 | InFile { file_id: self.file, value: self.filter_map_expr.clone().into() } | ||
408 | } | ||
409 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
410 | self | ||
411 | } | ||
412 | } | ||
413 | |||
389 | #[cfg(test)] | 414 | #[cfg(test)] |
390 | mod tests { | 415 | mod tests { |
391 | use base_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt}; | 416 | use base_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt}; |
@@ -644,4 +669,19 @@ fn foo() { break; } | |||
644 | "#, | 669 | "#, |
645 | ); | 670 | ); |
646 | } | 671 | } |
672 | |||
673 | #[test] | ||
674 | fn replace_missing_filter_next_with_find_map() { | ||
675 | check_diagnostics( | ||
676 | r#" | ||
677 | fn foo() { | ||
678 | let m = [1, 2, 3] | ||
679 | .iter() | ||
680 | .filter_map(|x| if *x == 2 { Some (4) } else { None }) | ||
681 | .next(); | ||
682 | //^^^ Replace .filter_map(..).next() with .find_map(..) | ||
683 | } | ||
684 | "#, | ||
685 | ); | ||
686 | } | ||
647 | } | 687 | } |