From 1316422a7c2ef26e9da78fa23f170407b1cb39bb Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Mon, 28 Dec 2020 13:41:15 +0000 Subject: Add diagnostic for filter_map followed by next --- crates/hir_ty/src/diagnostics.rs | 48 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'crates/hir_ty/src/diagnostics.rs') 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 { // Diagnostic: break-outside-of-loop // -// This diagnostic is triggered if `break` keyword is used outside of a loop. +// This diagnostic is triggered if the `break` keyword is used outside of a loop. #[derive(Debug)] pub struct BreakOutsideOfLoop { pub file: HirFileId, @@ -271,7 +271,7 @@ impl Diagnostic for BreakOutsideOfLoop { // Diagnostic: missing-unsafe // -// This diagnostic is triggered if operation marked as `unsafe` is used outside of `unsafe` function or block. +// This diagnostic is triggered if an operation marked as `unsafe` is used outside of an `unsafe` function or block. #[derive(Debug)] pub struct MissingUnsafe { pub file: HirFileId, @@ -295,7 +295,7 @@ impl Diagnostic for MissingUnsafe { // Diagnostic: mismatched-arg-count // -// This diagnostic is triggered if function is invoked with an incorrect amount of arguments. +// This diagnostic is triggered if a function is invoked with an incorrect amount of arguments. #[derive(Debug)] pub struct MismatchedArgCount { pub file: HirFileId, @@ -347,7 +347,7 @@ impl fmt::Display for CaseType { // Diagnostic: incorrect-ident-case // -// 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]. +// 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]. #[derive(Debug)] pub struct IncorrectCase { pub file: HirFileId, @@ -386,6 +386,31 @@ impl Diagnostic for IncorrectCase { } } +// Diagnostic: replace-filter-map-next-with-find-map +// +// This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`. +#[derive(Debug)] +pub struct ReplaceFilterMapNextWithFindMap { + pub file: HirFileId, + pub filter_map_expr: AstPtr, + pub next_expr: AstPtr, +} + +impl Diagnostic for ReplaceFilterMapNextWithFindMap { + fn code(&self) -> DiagnosticCode { + DiagnosticCode("replace-filter-map-next-with-find-map") + } + fn message(&self) -> String { + "replace filter_map(..).next() with find_map(..)".to_string() + } + fn display_source(&self) -> InFile { + InFile { file_id: self.file, value: self.filter_map_expr.clone().into() } + } + fn as_any(&self) -> &(dyn Any + Send + 'static) { + self + } +} + #[cfg(test)] mod tests { use base_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt}; @@ -644,4 +669,19 @@ fn foo() { break; } "#, ); } + + #[test] + fn replace_missing_filter_next_with_find_map() { + check_diagnostics( + r#" + fn foo() { + let m = [1, 2, 3] + .iter() + .filter_map(|x| if *x == 2 { Some (4) } else { None }) + .next(); + //^^^ Replace .filter_map(..).next() with .find_map(..) + } + "#, + ); + } } -- cgit v1.2.3