miniextendr_lint/lint_code.rs
1//! Stable lint rule identifiers.
2//!
3//! Each rule has a code like `MXL008` that is grep-able and CI-friendly.
4
5use std::fmt;
6
7/// Stable lint rule identifier.
8///
9/// Display format is `MXL###`, derived directly from the variant name.
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
11pub enum LintCode {
12 // region: Source-side validation
13 /// Trait impl class system incompatible with inherent impl class system.
14 MXL008,
15 /// Multiple impl blocks for one type without labels.
16 MXL009,
17 /// Duplicate labels on impl blocks for one type.
18 MXL010,
19 // endregion
20
21 // region: P0: High Impact
22 /// Registered top-level function is not `pub`.
23 MXL106,
24 /// Parameter name is an R reserved word; codegen will produce invalid R syntax.
25 MXL110,
26 /// `s4_*` method name on `#[miniextendr(s4)]` impl — codegen auto-prepends `s4_`.
27 MXL111,
28 /// vctrs constructor returns `Self` / named type, or impl has an instance-method receiver.
29 ///
30 /// Mirror: `miniextendr-macros/src/miniextendr_impl.rs` (proc-macro hard error).
31 /// Both checks must fire on the same source; keep them in sync.
32 MXL120,
33 // endregion
34
35 // region: P1: Important
36 /// `internal` + `noexport` redundancy.
37 MXL203,
38 // endregion
39
40 // region: P2: Safety
41 /// Direct `Rf_error`/`Rf_errorcall` call in user code.
42 MXL300,
43 /// `_unchecked` FFI call outside guard context.
44 MXL301,
45 /// `into_sexp()` call inside a `vec!`/array literal — unprotected SEXP across allocations (UAF).
46 MXL302,
47 /// Two `#[miniextendr]` trait impls collapse to the same vtable symbol
48 /// (`__VTABLE_{TRAIT}_FOR_{TYPE}`) after the macro's case-folding.
49 MXL303,
50 // endregion
51}
52
53impl fmt::Display for LintCode {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 // Variant names are already `MXL###`, so Debug output works.
56 fmt::Debug::fmt(self, f)
57 }
58}
59
60impl LintCode {
61 /// Default severity for this rule.
62 pub fn default_severity(self) -> super::diagnostic::Severity {
63 use super::diagnostic::Severity;
64 match self {
65 // Source-side checks are errors (CI-blocking).
66 Self::MXL008 | Self::MXL009 | Self::MXL010 => Severity::Error,
67
68 // Codegen-breaking: reserved words produce syntactically invalid R wrappers.
69 Self::MXL110 => Severity::Error,
70
71 // Runtime-breaking: vctrs constructors returning Self produce EXTPTRSXP
72 // which vctrs::new_vctr() rejects; instance-method receivers panic at runtime.
73 Self::MXL120 => Severity::Error,
74
75 // Build-breaking: colliding trait impls emit duplicate `#[no_mangle]`
76 // vtable statics → cryptic linker error divorced from the source.
77 Self::MXL303 => Severity::Error,
78
79 // Everything else is a warning.
80 Self::MXL106
81 | Self::MXL111
82 | Self::MXL203
83 | Self::MXL300
84 | Self::MXL301
85 | Self::MXL302 => Severity::Warning,
86 }
87 }
88}