ide_diagnostics/handlers/
invalid_derive_target.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: invalid-derive-target
4//
5// This diagnostic is shown when the derive attribute is used on an item other than a `struct`,
6// `enum` or `union`.
7pub(crate) fn invalid_derive_target(
8    ctx: &DiagnosticsContext<'_>,
9    d: &hir::InvalidDeriveTarget,
10) -> Diagnostic {
11    let display_range = ctx.sema.diagnostics_display_range_for_range(d.range);
12
13    Diagnostic::new(
14        DiagnosticCode::RustcHardError("E0774"),
15        "`derive` may only be applied to `struct`s, `enum`s and `union`s",
16        display_range,
17    )
18    .stable()
19}
20
21#[cfg(test)]
22mod tests {
23    use crate::tests::check_diagnostics;
24
25    #[test]
26    fn fails_on_function() {
27        check_diagnostics(
28            r#"
29//- minicore:derive
30mod __ {
31    #[derive()]
32   // ^^^^^^ error: `derive` may only be applied to `struct`s, `enum`s and `union`s
33    fn main() {}
34}
35            "#,
36        );
37    }
38}