Skip to content

Commit

Permalink
Auto merge of #15990 - Young-Flash:trait_impl_reduntant_assoc_item, r…
Browse files Browse the repository at this point in the history
…=Veykril

feat: add trait_impl_reduntant_assoc_item diagnostic

part of #15958, will try to add quickfix for the diagnostic if this PR is ok with you guys
  • Loading branch information
bors committed Dec 7, 2023
2 parents 07d3128 + fbe494a commit 421a0a4
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
11 changes: 10 additions & 1 deletion crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use hir_def::path::ModPath;
use hir_expand::{name::Name, HirFileId, InFile};
use syntax::{ast, AstPtr, SyntaxError, SyntaxNodePtr, TextRange};

use crate::{AssocItem, Field, Local, MacroKind, Type};
use crate::{AssocItem, Field, Local, MacroKind, Trait, Type};

macro_rules! diagnostics {
($($diag:ident,)*) => {
Expand Down Expand Up @@ -55,6 +55,7 @@ diagnostics![
ReplaceFilterMapNextWithFindMap,
TraitImplIncorrectSafety,
TraitImplMissingAssocItems,
TraitImplRedundantAssocItems,
TraitImplOrphan,
TypedHole,
TypeMismatch,
Expand Down Expand Up @@ -310,3 +311,11 @@ pub struct TraitImplMissingAssocItems {
pub impl_: AstPtr<ast::Impl>,
pub missing: Vec<(Name, AssocItem)>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct TraitImplRedundantAssocItems {
pub file_id: HirFileId,
pub trait_: Trait,
pub impl_: AstPtr<ast::Impl>,
pub assoc_item: (Name, AssocItem),
}
20 changes: 20 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,26 @@ impl Module {
},
));

let redundant = impl_assoc_items_scratch
.iter()
.filter(|(id, name)| {
!items.iter().any(|(impl_name, impl_item)| {
discriminant(impl_item) == discriminant(id) && impl_name == name
})
})
.map(|(item, name)| (name.clone(), AssocItem::from(*item)));
for (name, assoc_item) in redundant {
acc.push(
TraitImplRedundantAssocItems {
trait_,
file_id,
impl_: ast_id_map.get(node.ast_id()),
assoc_item: (name, assoc_item),
}
.into(),
)
}

let missing: Vec<_> = required_items
.filter(|(name, id)| {
!impl_assoc_items_scratch.iter().any(|(impl_item, impl_name)| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use hir::{Const, Function, HasSource, TypeAlias};
use ide_db::base_db::FileRange;

use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: trait-impl-redundant-assoc_item
//
// Diagnoses redundant trait items in a trait impl.
pub(crate) fn trait_impl_redundant_assoc_item(
ctx: &DiagnosticsContext<'_>,
d: &hir::TraitImplRedundantAssocItems,
) -> Diagnostic {
let name = d.assoc_item.0.clone();
let assoc_item = d.assoc_item.1;
let db = ctx.sema.db;

let default_range = d.impl_.syntax_node_ptr().text_range();
let trait_name = d.trait_.name(db).to_smol_str();

let (redundant_item_name, diagnostic_range) = match assoc_item {
hir::AssocItem::Function(id) => (
format!("`fn {}`", name.display(db)),
Function::from(id)
.source(db)
.map(|it| it.syntax().value.text_range())
.unwrap_or(default_range),
),
hir::AssocItem::Const(id) => (
format!("`const {}`", name.display(db)),
Const::from(id)
.source(db)
.map(|it| it.syntax().value.text_range())
.unwrap_or(default_range),
),
hir::AssocItem::TypeAlias(id) => (
format!("`type {}`", name.display(db)),
TypeAlias::from(id)
.source(db)
.map(|it| it.syntax().value.text_range())
.unwrap_or(default_range),
),
};

Diagnostic::new(
DiagnosticCode::RustcHardError("E0407"),
format!("{redundant_item_name} is not a member of trait `{trait_name}`"),
FileRange { file_id: d.file_id.file_id().unwrap(), range: diagnostic_range },
)
}

#[cfg(test)]
mod tests {
use crate::tests::check_diagnostics;

#[test]
fn trait_with_default_value() {
check_diagnostics(
r#"
trait Marker {
const FLAG: bool = false;
fn boo();
fn foo () {}
}
struct Foo;
impl Marker for Foo {
type T = i32;
//^^^^^^^^^^^^^ error: `type T` is not a member of trait `Marker`
const FLAG: bool = true;
fn bar() {}
//^^^^^^^^^^^ error: `fn bar` is not a member of trait `Marker`
fn boo() {}
}
"#,
)
}
}
2 changes: 2 additions & 0 deletions crates/ide-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod handlers {
pub(crate) mod trait_impl_orphan;
pub(crate) mod trait_impl_incorrect_safety;
pub(crate) mod trait_impl_missing_assoc_item;
pub(crate) mod trait_impl_redundant_assoc_item;
pub(crate) mod typed_hole;
pub(crate) mod type_mismatch;
pub(crate) mod unimplemented_builtin_macro;
Expand Down Expand Up @@ -364,6 +365,7 @@ pub fn diagnostics(
AnyDiagnostic::ReplaceFilterMapNextWithFindMap(d) => handlers::replace_filter_map_next_with_find_map::replace_filter_map_next_with_find_map(&ctx, &d),
AnyDiagnostic::TraitImplIncorrectSafety(d) => handlers::trait_impl_incorrect_safety::trait_impl_incorrect_safety(&ctx, &d),
AnyDiagnostic::TraitImplMissingAssocItems(d) => handlers::trait_impl_missing_assoc_item::trait_impl_missing_assoc_item(&ctx, &d),
AnyDiagnostic::TraitImplRedundantAssocItems(d) => handlers::trait_impl_redundant_assoc_item::trait_impl_redundant_assoc_item(&ctx, &d),
AnyDiagnostic::TraitImplOrphan(d) => handlers::trait_impl_orphan::trait_impl_orphan(&ctx, &d),
AnyDiagnostic::TypedHole(d) => handlers::typed_hole::typed_hole(&ctx, &d),
AnyDiagnostic::TypeMismatch(d) => handlers::type_mismatch::type_mismatch(&ctx, &d),
Expand Down

0 comments on commit 421a0a4

Please sign in to comment.