Extract regex pattern to avoid recompilation in loop - #797
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Extract regex pattern to avoid recompilation in loop#797sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AaAnTW0P-2muSVzVc4CM for java:S9142 rule Generated by SonarQube Agent (task: 1323d451-de96-4dd1-a6ad-08e5ff9e6649)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed a MAJOR SonarQube issue where a regular expression pattern was being compiled repeatedly inside a loop in ComplexityVisitor.java. By extracting the pattern to a pre-compiled Pattern constant, this change improves performance by eliminating redundant regex compilation and follows Java best practices for pattern reuse.
View Project in SonarCloud
Fixed Issues
java:S9142 - Extract this regular expression to a Pattern compiled outside the loop. • MAJOR • View issue
Location:
sonar-html-plugin/src/main/java/org/sonar/plugins/html/analyzers/ComplexityVisitor.java:55Why is this an issue?
Compilation and preparation operations are expensive because they involve parsing, validation, and internal representation building. When these operations are performed inside loops with constant or loop-invariant arguments, the same work is repeated unnecessarily on every iteration.
What changed
Adds the import for java.util.regex.Pattern, which is needed to define the pre-compiled Pattern constant. This supports fixing the issue where a regular expression pattern used in String.split() was being compiled repeatedly inside a loop at line 55 of ComplexityVisitor.java.
SonarQube Remediation Agent uses AI. Check for mistakes.