Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use hir::{db::ExpandDatabase, Const, Function, HasSource, TypeAlias};

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 range = db.parse_or_expand(d.file_id).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),
        ),
        hir::AssocItem::Const(id) => (
            format!("`const {}`", name.display(db)),
            Const::from(id).source(db).map(|it| it.syntax().value.text_range()).unwrap_or(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),
        ),
    };

    Diagnostic::new(
        DiagnosticCode::RustcHardError("E0407"),
        format!("{redundant_item_name} is not a member of trait `{trait_name}`"),
        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() {}
}
            "#,
        )
    }
}