Unnamed repository; edit this file 'description' to name the repository.
fix: change default diagnostic range into impl body
Young-Flash 2023-12-07
parent 861e474 · commit fbe494a
-rw-r--r--crates/hir/src/diagnostics.rs1
-rw-r--r--crates/hir/src/lib.rs1
-rw-r--r--crates/ide-diagnostics/src/handlers/trait_impl_redundant_assoc_item.rs19
3 files changed, 15 insertions, 6 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs
index 52c1c27a7f..1cb36f9b02 100644
--- a/crates/hir/src/diagnostics.rs
+++ b/crates/hir/src/diagnostics.rs
@@ -316,5 +316,6 @@ pub struct TraitImplMissingAssocItems {
pub struct TraitImplRedundantAssocItems {
pub file_id: HirFileId,
pub trait_: Trait,
+ pub impl_: AstPtr<ast::Impl>,
pub assoc_item: (Name, AssocItem),
}
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 4db1a02c0c..5137bff055 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -706,6 +706,7 @@ impl Module {
TraitImplRedundantAssocItems {
trait_,
file_id,
+ impl_: ast_id_map.get(node.ast_id()),
assoc_item: (name, assoc_item),
}
.into(),
diff --git a/crates/ide-diagnostics/src/handlers/trait_impl_redundant_assoc_item.rs b/crates/ide-diagnostics/src/handlers/trait_impl_redundant_assoc_item.rs
index 6aded11382..8200143914 100644
--- a/crates/ide-diagnostics/src/handlers/trait_impl_redundant_assoc_item.rs
+++ b/crates/ide-diagnostics/src/handlers/trait_impl_redundant_assoc_item.rs
@@ -1,4 +1,5 @@
-use hir::{db::ExpandDatabase, Const, Function, HasSource, TypeAlias};
+use hir::{Const, Function, HasSource, TypeAlias};
+use ide_db::base_db::FileRange;
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
@@ -13,31 +14,37 @@ pub(crate) fn trait_impl_redundant_assoc_item(
let assoc_item = d.assoc_item.1;
let db = ctx.sema.db;
- let range = db.parse_or_expand(d.file_id).text_range();
+ 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(range),
+ 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(range),
+ 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(range),
+ .unwrap_or(default_range),
),
};
Diagnostic::new(
DiagnosticCode::RustcHardError("E0407"),
format!("{redundant_item_name} is not a member of trait `{trait_name}`"),
- diagnostic_range,
+ FileRange { file_id: d.file_id.file_id().unwrap(), range: diagnostic_range },
)
}