]>
Commit | Line | Data |
---|---|---|
add651ee | 1 | use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext}; |
064997fb FG |
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`. | |
7 | pub(crate) fn invalid_derive_target( | |
8 | ctx: &DiagnosticsContext<'_>, | |
9 | d: &hir::InvalidDeriveTarget, | |
10 | ) -> Diagnostic { | |
4b012472 | 11 | let display_range = ctx.sema.diagnostics_display_range(d.node.clone()); |
064997fb FG |
12 | |
13 | Diagnostic::new( | |
add651ee | 14 | DiagnosticCode::RustcHardError("E0774"), |
064997fb FG |
15 | "`derive` may only be applied to `struct`s, `enum`s and `union`s", |
16 | display_range, | |
17 | ) | |
064997fb FG |
18 | } |
19 | ||
20 | #[cfg(test)] | |
21 | mod tests { | |
22 | use crate::tests::check_diagnostics; | |
23 | ||
24 | #[test] | |
25 | fn fails_on_function() { | |
26 | check_diagnostics( | |
27 | r#" | |
28 | //- minicore:derive | |
29 | mod __ { | |
30 | #[derive()] | |
31 | //^^^^^^^^^^^ error: `derive` may only be applied to `struct`s, `enum`s and `union`s | |
32 | fn main() {} | |
33 | } | |
34 | "#, | |
35 | ); | |
36 | } | |
37 | } |