Skip to content

Commit fbe494a

Browse files
committed
fix: change default diagnostic range into impl body
1 parent 861e474 commit fbe494a

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

crates/hir/src/diagnostics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,6 @@ pub struct TraitImplMissingAssocItems {
316316
pub struct TraitImplRedundantAssocItems {
317317
pub file_id: HirFileId,
318318
pub trait_: Trait,
319+
pub impl_: AstPtr<ast::Impl>,
319320
pub assoc_item: (Name, AssocItem),
320321
}

crates/hir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ impl Module {
706706
TraitImplRedundantAssocItems {
707707
trait_,
708708
file_id,
709+
impl_: ast_id_map.get(node.ast_id()),
709710
assoc_item: (name, assoc_item),
710711
}
711712
.into(),

crates/ide-diagnostics/src/handlers/trait_impl_redundant_assoc_item.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use hir::{db::ExpandDatabase, Const, Function, HasSource, TypeAlias};
1+
use hir::{Const, Function, HasSource, TypeAlias};
2+
use ide_db::base_db::FileRange;
23

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

@@ -13,31 +14,37 @@ pub(crate) fn trait_impl_redundant_assoc_item(
1314
let assoc_item = d.assoc_item.1;
1415
let db = ctx.sema.db;
1516

16-
let range = db.parse_or_expand(d.file_id).text_range();
17+
let default_range = d.impl_.syntax_node_ptr().text_range();
1718
let trait_name = d.trait_.name(db).to_smol_str();
1819

1920
let (redundant_item_name, diagnostic_range) = match assoc_item {
2021
hir::AssocItem::Function(id) => (
2122
format!("`fn {}`", name.display(db)),
22-
Function::from(id).source(db).map(|it| it.syntax().value.text_range()).unwrap_or(range),
23+
Function::from(id)
24+
.source(db)
25+
.map(|it| it.syntax().value.text_range())
26+
.unwrap_or(default_range),
2327
),
2428
hir::AssocItem::Const(id) => (
2529
format!("`const {}`", name.display(db)),
26-
Const::from(id).source(db).map(|it| it.syntax().value.text_range()).unwrap_or(range),
30+
Const::from(id)
31+
.source(db)
32+
.map(|it| it.syntax().value.text_range())
33+
.unwrap_or(default_range),
2734
),
2835
hir::AssocItem::TypeAlias(id) => (
2936
format!("`type {}`", name.display(db)),
3037
TypeAlias::from(id)
3138
.source(db)
3239
.map(|it| it.syntax().value.text_range())
33-
.unwrap_or(range),
40+
.unwrap_or(default_range),
3441
),
3542
};
3643

3744
Diagnostic::new(
3845
DiagnosticCode::RustcHardError("E0407"),
3946
format!("{redundant_item_name} is not a member of trait `{trait_name}`"),
40-
diagnostic_range,
47+
FileRange { file_id: d.file_id.file_id().unwrap(), range: diagnostic_range },
4148
)
4249
}
4350

0 commit comments

Comments
 (0)