-
Notifications
You must be signed in to change notification settings - Fork 724
SONARJAVA-6722 Implement S9142: Expensive compilation or preparation operations should not be performed inside loops #5893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
850fc56
Add rule metadata
NoemieBenard f5ef064
Add failing reproducer
NoemieBenard cee8cea
Implement rule
NoemieBenard 15b7b16
Update ruling expectations
NoemieBenard b82ea91
Fix S9142 messages, split fast path, and loop-invariant false positives.
nathsou 8771e5c
Address review comments and fix sonarqube issues
NoemieBenard c22a79d
Update ruling expectations
NoemieBenard 9024910
Improve test coverage
NoemieBenard ba917e1
Improve test coverage again
NoemieBenard a3a2611
Improve test coverage
NoemieBenard 6a18a32
Address review comments
NoemieBenard 1fb4dd2
Fix false negative for once-per-outer-loop calls
NoemieBenard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
its/autoscan/src/test/resources/autoscan/diffs/diff_S9142.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "ruleKey": "S9142", | ||
| "hasTruePositives": true, | ||
| "falseNegatives": 0, | ||
| "falsePositives": 0 | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
12 changes: 12 additions & 0 deletions
12
...efault/src/main/files/non-compiling/checks/CompilationOrPreparationInLoopCheckSample.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package checks; | ||
|
|
||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| class CompilationOrPreparationInLoopCheckSampleNonCompiling { | ||
| void test(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| Pattern.compile(unknownVar).matcher(input).find(); // Compliant - unresolved symbol is not a variable symbol | ||
| } | ||
| } | ||
| } |
166 changes: 166 additions & 0 deletions
166
...-test-sources/default/src/main/java/checks/CompilationOrPreparationInLoopCheckSample.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| package checks; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.SQLException; | ||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| class CompilationOrPreparationInLoopCheckSample { | ||
|
|
||
| private static final String CONSTANT_PATTERN = "[a-z]+"; | ||
| private String mutablePattern = "[a-z]+"; | ||
|
|
||
| void patternCompileNoncompliant(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| Pattern.compile("[a-z]+").matcher(input).find(); // Noncompliant {{Move this "compile" call outside the loop.}} | ||
| //^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| int i = 0; | ||
| while (i++ < inputs.size()) { | ||
| Pattern.compile("[a-z]+"); // Noncompliant | ||
| } | ||
|
|
||
| for (String input : inputs) { | ||
| Pattern.compile(CONSTANT_PATTERN).matcher(input).find(); // Noncompliant | ||
| } | ||
|
|
||
| String invariantPattern = "[a-z]+"; | ||
| for (String input : inputs) { | ||
| Pattern.compile(invariantPattern).matcher(input).find(); // Noncompliant | ||
| } | ||
|
|
||
| for (String input : inputs) { | ||
| Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE); // Noncompliant | ||
| int flags = input.isEmpty() ? 0 : Pattern.CASE_INSENSITIVE; | ||
| Pattern.compile("[a-z]+", flags); // Compliant: flags vary | ||
| } | ||
| } | ||
|
|
||
| void stringMethodsNoncompliant(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| input.matches("[a-z]+"); // Noncompliant {{Extract this regular expression to a Pattern compiled outside the loop.}} | ||
| input.replaceAll("[a-z]+", "X"); // Noncompliant | ||
| input.replaceFirst("[a-z]+", "X"); // Noncompliant | ||
| input.split("[,;]"); // Noncompliant | ||
| input.split("."); // Noncompliant | ||
| input.split("\\a"); // Noncompliant | ||
| input.split(","); // Compliant: single non-metacharacter fast path | ||
| input.split("\\."); // Compliant: escaped non-alphanumeric fast path | ||
| } | ||
| } | ||
|
|
||
| void prepareStatementNoncompliant(Connection conn, List<Integer> ids) throws SQLException { | ||
| for (int id : ids) { | ||
| PreparedStatement ps = conn.prepareStatement("SELECT * FROM t WHERE id = ?"); // Noncompliant | ||
| ps.setInt(1, id); | ||
| ps.execute(); | ||
| ps.close(); | ||
| } | ||
| } | ||
|
|
||
| void forInitializer(String s) { | ||
| for (Pattern p = Pattern.compile("[a-z]+"); p.matcher(s).find(); ) { // Compliant: initializer runs once | ||
| break; | ||
| } | ||
| for (; Pattern.compile("[a-z]+").matcher(s).find(); ) { // Noncompliant | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| void compliant(List<String> inputs, Connection conn, List<Integer> ids) throws SQLException { | ||
| Pattern p = Pattern.compile("[a-z]+"); | ||
| for (String input : inputs) { | ||
| p.matcher(input).find(); | ||
| } | ||
|
|
||
| for (String input : inputs) { | ||
| input.toLowerCase(); // not a regex method | ||
| } | ||
|
|
||
| PreparedStatement ps = conn.prepareStatement("SELECT * FROM t WHERE id = ?"); | ||
| for (int id : ids) { | ||
| ps.setInt(1, id); | ||
| ps.execute(); | ||
| } | ||
| } | ||
|
|
||
| void patternVariesPerIteration(List<String> patterns, List<String> inputs) { | ||
| for (int i = 0; i < inputs.size(); i++) { | ||
| String pattern = patterns.get(i); | ||
| Pattern.compile(pattern).matcher(inputs.get(i)).find(); // Compliant - pattern changes per iteration | ||
| } | ||
| } | ||
|
|
||
| void mutableFieldPattern(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| Pattern.compile(mutablePattern).matcher(input).find(); // Compliant - non-final field may be mutated via member select or method call | ||
| } | ||
| } | ||
|
|
||
| void localReassignedInLoop(List<String> inputs) { | ||
| String pattern = "[a-z]+"; | ||
| for (String input : inputs) { | ||
| pattern = input; // reassigned each iteration | ||
| Pattern.compile(pattern).matcher(input).find(); // Compliant - pattern changes per iteration | ||
| } | ||
| } | ||
|
|
||
| void doWhileLoop(List<String> inputs) { | ||
| int i = 0; | ||
| do { | ||
| Pattern.compile("[a-z]+").matcher(inputs.get(i)).find(); // Noncompliant | ||
| } while (i++ < inputs.size()); | ||
| } | ||
|
|
||
| void nonConstantArg(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| Pattern.compile(input.trim()).matcher(input).find(); // Compliant - method call result is not a constant | ||
| } | ||
| } | ||
|
|
||
| void nonIncrementUnaryInLoop(List<String> inputs) { | ||
| boolean inverse = false; | ||
| for (String input : inputs) { | ||
| if (!inverse) { // non-increment unary expression | ||
| Pattern.compile("[a-z]+").matcher(input).find(); // Noncompliant | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void nonIdentifierMutationsInLoop(List<String> inputs) { | ||
| int[] counters = new int[2]; | ||
| for (String input : inputs) { | ||
| counters[0] = input.length(); // assignment to non-identifier target | ||
| counters[1]++; // increment on non-identifier target | ||
| Pattern.compile("[a-z]+").matcher(input).find(); // Noncompliant | ||
| } | ||
| } | ||
|
|
||
| void expressionInEnhancedForLoopWithSplit(String text) { | ||
| for (String s : text.split(";")) { // Compliant; text.split(";") evaluated only once | ||
| if (s.isEmpty()) { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void expressionInEnhancedForLoopWithCompile(String text) { | ||
| for (String s : Pattern.compile(";").split(text)) { // Compliant; Pattern.compile(";").split(text) evaluated only once | ||
| if (s.isEmpty()) { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void forEachIterableInsideOuterLoop(List<String> texts) { | ||
| for (String text : texts) { | ||
| for (String s : Pattern.compile(";").split(text)) { // Noncompliant | ||
| if (s.isEmpty()) { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.