Add diagnostic for filter_map followed by next

This commit is contained in:
Phil Ellison 2020-12-28 13:41:15 +00:00
parent eab5db20ed
commit 1316422a7c
5 changed files with 150 additions and 15 deletions

View file

@ -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<ast::Expr>,
pub next_expr: AstPtr<ast::Expr>,
}
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<SyntaxNodePtr> {
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(..)
}
"#,
);
}
}