refine: reduce false positives in keyword validation

- Exclude method declarations from type pattern matching
- Focus on Java 9+ module keywords for type declarations
- Prevents false positives on valid code like 'void testMethod()'
- Still catches all actual keyword usage issues
This commit is contained in:
Mohamed Ashraf 2026-02-05 16:40:43 +00:00
parent 05761b768d
commit 2fc46418cb

View file

@ -341,9 +341,11 @@ def _validate_no_reserved_keywords(code: str) -> tuple[bool, str | None]:
# e.g., "public class provides {" or "class provides {"
class_pattern = re.compile(r'\bclass\s+(\w+)\s*[{<]')
# Pattern to match type names (variable declarations, parameters)
# e.g., "private provides instance;" or "provides param"
type_pattern = re.compile(r'\b(\w+)\s+\w+\s*[;=,(]')
# Pattern to match type names in variable declarations and parameters
# But exclude method declarations (type followed by identifier with parentheses)
# e.g., "private provides instance;" -> match, but "void testMethod()" -> skip
# Negative lookahead to exclude method declarations: \s+\w+\s*\(
type_pattern = re.compile(r'\b(\w+)\s+\w+\s*(?![(\s]*\()([;=,])')
violations: list[str] = []
@ -351,23 +353,22 @@ def _validate_no_reserved_keywords(code: str) -> tuple[bool, str | None]:
for match in import_pattern.finditer(code):
class_name = match.group(1)
if class_name.lower() in JAVA_RESERVED_KEYWORDS:
violations.append(f"import uses reserved keyword '{class_name}' as class name at position {match.start()}")
violations.append(f"import uses reserved keyword '{class_name}' as class name")
# Check class declarations
for match in class_pattern.finditer(code):
class_name = match.group(1)
if class_name.lower() in JAVA_RESERVED_KEYWORDS:
violations.append(f"class declaration uses reserved keyword '{class_name}' at position {match.start()}")
violations.append(f"class declaration uses reserved keyword '{class_name}'")
# Check type names in declarations (more prone to false positives, so be more conservative)
# Check type names in declarations (more conservative to avoid false positives)
for match in type_pattern.finditer(code):
type_name = match.group(1)
# Only flag if it's an exact match (not lowercase check) to reduce false positives
if type_name in JAVA_RESERVED_KEYWORDS:
# Skip if it's a known valid keyword in this context (like 'new', 'public', etc.)
# These are already syntax errors in this position, so Java compiler would catch them
if type_name not in {'new', 'public', 'private', 'protected', 'static', 'final', 'abstract'}:
violations.append(f"type declaration uses reserved keyword '{type_name}' at position {match.start()}")
# Only flag Java 9+ module keywords which are less likely to be valid in type positions
# Core keywords like 'int', 'void', etc. are already handled by Java syntax
module_keywords = {'provides', 'requires', 'exports', 'opens', 'uses', 'with', 'to', 'transitive', 'module', 'open'}
if type_name.lower() in module_keywords:
violations.append(f"type declaration uses reserved keyword '{type_name}'")
if violations:
error_msg = "Java reserved keywords used as identifiers:\n" + "\n".join(f" - {v}" for v in violations)