Skip to main content

perl_diagnostics/codes/
category.rs

1use std::fmt;
2
3use super::DiagnosticCode;
4
5/// Category of diagnostic codes.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub enum DiagnosticCategory {
9    /// Parser-related diagnostics (PL001-PL099)
10    Parser,
11    /// Strict/warnings pragmas and scope analysis (PL100-PL199)
12    StrictWarnings,
13    /// Package/module issues (PL200-PL299)
14    PackageModule,
15    /// Subroutine issues (PL300-PL399)
16    Subroutine,
17    /// Best practices and common mistakes (PL400-PL499)
18    BestPractices,
19    /// Deprecated syntax (PL500-PL599)
20    Deprecated,
21    /// Security anti-patterns (PL600-PL699)
22    Security,
23    /// Import/use diagnostics (PL700-PL799)
24    Import,
25    /// Heredoc anti-patterns (PL800-PL899)
26    Heredoc,
27    /// Perl::Critic violations (PC001-PC005)
28    PerlCritic,
29}
30
31impl fmt::Display for DiagnosticCategory {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::Parser => write!(f, "Parser"),
35            Self::StrictWarnings => write!(f, "Strict/Warnings"),
36            Self::PackageModule => write!(f, "Package/Module"),
37            Self::Subroutine => write!(f, "Subroutine"),
38            Self::BestPractices => write!(f, "Best Practices"),
39            Self::Deprecated => write!(f, "Deprecated"),
40            Self::Security => write!(f, "Security"),
41            Self::Import => write!(f, "Import"),
42            Self::Heredoc => write!(f, "Heredoc"),
43            Self::PerlCritic => write!(f, "Perl::Critic"),
44        }
45    }
46}
47
48impl DiagnosticCode {
49    /// Get the category of this diagnostic code.
50    pub fn category(&self) -> DiagnosticCategory {
51        match self {
52            Self::ParseError | Self::SyntaxError | Self::UnexpectedEof => {
53                DiagnosticCategory::Parser
54            }
55
56            Self::MissingStrict
57            | Self::MissingWarnings
58            | Self::UnusedVariable
59            | Self::UndefinedVariable
60            | Self::VariableShadowing
61            | Self::VariableRedeclaration
62            | Self::DuplicateParameter
63            | Self::ParameterShadowsGlobal
64            | Self::UnusedParameter
65            | Self::UnquotedBareword
66            | Self::UninitializedVariable
67            | Self::MisspelledPragma
68            | Self::CaptureVarWithoutRegexMatch
69            | Self::PhaseScopedStrictPragma
70            | Self::PhaseScopedWarningsPragma => DiagnosticCategory::StrictWarnings,
71
72            Self::MissingPackageDeclaration | Self::DuplicatePackage => {
73                DiagnosticCategory::PackageModule
74            }
75
76            Self::DuplicateSubroutine
77            | Self::MissingReturn
78            | Self::InvalidPrototype
79            | Self::RoleConflict
80            | Self::MissingPodCoverage => DiagnosticCategory::Subroutine,
81
82            Self::BarewordFilehandle
83            | Self::TwoArgOpen
84            | Self::ImplicitReturn
85            | Self::AssignmentInCondition
86            | Self::NumericComparisonWithUndef
87            | Self::PrintfFormatMismatch
88            | Self::UnreachableCode
89            | Self::EvalErrorFlow
90            | Self::DuplicateHashKey
91            | Self::GotoUndefinedLabel
92            | Self::LoopControlUndefinedLabel
93            | Self::VersionIncompatFeature => DiagnosticCategory::BestPractices,
94
95            Self::DeprecatedDefined | Self::DeprecatedArrayBase => DiagnosticCategory::Deprecated,
96
97            Self::SecurityStringEval
98            | Self::SecurityBacktickExec
99            | Self::SecuritySignalHandler
100            | Self::SecuritySystemCall
101            | Self::SecurityExecCall
102            | Self::SecurityPipeOpen
103            | Self::SecurityReadpipe => DiagnosticCategory::Security,
104
105            Self::UnusedImport | Self::ModuleNotFound => DiagnosticCategory::Import,
106
107            Self::HeredocInFormat
108            | Self::HeredocInBegin
109            | Self::HeredocDynamicDelimiter
110            | Self::HeredocInSourceFilter
111            | Self::HeredocInRegexCode
112            | Self::HeredocInEval
113            | Self::HeredocTiedHandle => DiagnosticCategory::Heredoc,
114
115            Self::CriticSeverity1
116            | Self::CriticSeverity2
117            | Self::CriticSeverity3
118            | Self::CriticSeverity4
119            | Self::CriticSeverity5 => DiagnosticCategory::PerlCritic,
120        }
121    }
122}