Skip to content

Commit

Permalink
fix: change default diagnostic range into impl body
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Dec 7, 2023
1 parent 861e474 commit fbe494a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
1 change: 1 addition & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ impl Module {
TraitImplRedundantAssocItems {
trait_,
file_id,
impl_: ast_id_map.get(node.ast_id()),
assoc_item: (name, assoc_item),
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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 },
)
}

Expand Down

0 comments on commit fbe494a

Please sign in to comment.