]> git.proxmox.com Git - rustc.git/blame - src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/invalid_derive_target.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / ide-diagnostics / src / handlers / invalid_derive_target.rs
CommitLineData
add651ee 1use 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`.
7pub(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)]
21mod tests {
22 use crate::tests::check_diagnostics;
23
24 #[test]
25 fn fails_on_function() {
26 check_diagnostics(
27 r#"
28//- minicore:derive
29mod __ {
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}