Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions crates/rustapi-validate/src/v2/rules/sync_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub struct RegexRule {
pub pattern: String,
/// Compiled regex (not serialized)
#[serde(skip)]
compiled: OnceLock<Regex>,
compiled: OnceLock<Result<Regex, String>>,
/// Custom error message
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
Expand Down Expand Up @@ -309,19 +309,15 @@ impl RegexRule {
}

fn get_regex(&self) -> Result<&Regex, RuleError> {
self.compiled.get_or_init(|| {
Regex::new(&self.pattern).unwrap_or_else(|_| Regex::new("^$").unwrap())
let result = self.compiled.get_or_init(|| {
Regex::new(&self.pattern)
.map_err(|_| format!("Invalid regex pattern: {}", self.pattern))
});

// Verify the pattern is valid
if Regex::new(&self.pattern).is_err() {
return Err(RuleError::new(
"regex",
format!("Invalid regex pattern: {}", self.pattern),
));
match result {
Ok(regex) => Ok(regex),
Err(msg) => Err(RuleError::new("regex", msg.clone())),
}

Ok(self.compiled.get().unwrap())
}
Comment on lines 311 to 321
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling path for invalid regex patterns is not covered by tests. Consider adding a test case that validates the behavior when an invalid regex pattern is provided, such as a pattern with unbalanced brackets. This would ensure that the new error caching logic works correctly and that the error message is properly formatted and returned.

Copilot uses AI. Check for mistakes.
}

Expand Down
Loading