neo_solidity/
warning.rs

1//! Warning System for Neo Solidity Compiler
2//!
3//! Provides configurable warnings for code quality and security issues.
4
5use crate::error::{ErrorCode, ErrorSeverity, FixSuggestion, SourceLocation};
6use std::collections::HashSet;
7
8/// Warning configuration
9#[derive(Debug, Clone, Default)]
10pub struct WarningConfig {
11    /// Disabled warning codes
12    pub disabled: HashSet<ErrorCode>,
13    /// Warnings treated as errors
14    pub errors: HashSet<ErrorCode>,
15    /// Enable all warnings
16    pub all: bool,
17    /// Enable pedantic warnings
18    pub pedantic: bool,
19}
20
21impl WarningConfig {
22    /// Create config with all warnings enabled
23    pub fn all() -> Self {
24        Self {
25            all: true,
26            ..Default::default()
27        }
28    }
29
30    /// Disable a specific warning
31    pub fn disable(mut self, code: ErrorCode) -> Self {
32        self.disabled.insert(code);
33        self
34    }
35
36    /// Treat warning as error
37    pub fn as_error(mut self, code: ErrorCode) -> Self {
38        self.errors.insert(code);
39        self
40    }
41
42    /// Check if warning is enabled
43    pub fn is_enabled(&self, code: ErrorCode) -> bool {
44        !self.disabled.contains(&code)
45    }
46
47    /// Get severity for a warning code
48    pub fn severity(&self, code: ErrorCode) -> ErrorSeverity {
49        if self.errors.contains(&code) {
50            ErrorSeverity::Error
51        } else {
52            ErrorSeverity::Warning
53        }
54    }
55}
56
57/// A collected warning
58#[derive(Debug, Clone)]
59pub struct Warning {
60    pub code: ErrorCode,
61    pub message: String,
62    pub location: SourceLocation,
63    pub suggestion: Option<FixSuggestion>,
64}
65
66impl Warning {
67    pub fn new(code: ErrorCode, message: impl Into<String>, location: SourceLocation) -> Self {
68        Self {
69            code,
70            message: message.into(),
71            location,
72            suggestion: None,
73        }
74    }
75
76    pub fn with_suggestion(mut self, suggestion: FixSuggestion) -> Self {
77        self.suggestion = Some(suggestion);
78        self
79    }
80}
81
82/// Warning collector
83#[derive(Debug, Default)]
84pub struct WarningCollector {
85    warnings: Vec<Warning>,
86    config: WarningConfig,
87}
88
89impl WarningCollector {
90    pub fn new(config: WarningConfig) -> Self {
91        Self {
92            warnings: Vec::new(),
93            config,
94        }
95    }
96
97    pub fn add(&mut self, warning: Warning) {
98        if self.config.is_enabled(warning.code) {
99            self.warnings.push(warning);
100        }
101    }
102
103    pub fn warnings(&self) -> &[Warning] {
104        &self.warnings
105    }
106
107    pub fn has_errors(&self) -> bool {
108        self.warnings
109            .iter()
110            .any(|w| self.config.errors.contains(&w.code))
111    }
112
113    pub fn count(&self) -> usize {
114        self.warnings.len()
115    }
116
117    pub fn clear(&mut self) {
118        self.warnings.clear();
119    }
120}