Repository navigation
SONARJAVA-7188 SONARJAVA-4606 Improvements for the rule S7158 #6359
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
5 commits
Select commit
Hold shift + click to select a range
4373bf4
SONARJAVA-7188 S7158: Detect zero literals written in any base
asya-vorobeva b049767
SONARJAVA-4606 S7158: Suggest "isBlank()" for "trim().length()" compa…
asya-vorobeva 4dae062
SONARJAVA-4606 S7158: Also suggest "isBlank()" for "strip().length()"…
asya-vorobeva e7435b5
Fix for QG
asya-vorobeva 9095404
SONARJAVA-4606 S7158: Cover the pre-Java-11 behaviour with a test
asya-vorobeva 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
106 changes: 106 additions & 0 deletions
106
java-checks-test-sources/default/src/main/java/checks/StringIsBlankCheckSample.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,106 @@ | ||
| package checks; | ||
|
|
||
| public class StringIsBlankCheckSample { | ||
| public boolean sample(String s, StringBuilder sb) { | ||
| boolean b; | ||
|
|
||
| // test `strip().length() == 0` and equivalent code | ||
|
|
||
| b = s.strip().length() == 0; // Noncompliant {{Use "isBlank()" to check whether a "String" is blank or not.}} [[quickfixes=qf1]] | ||
| // fix@qf1 {{Replace with "isBlank()"}} | ||
| // edit@qf1 [[sc=11;ec=32]]{{isBlank()}} | ||
|
|
||
| b = s.strip().length() <= 0; // Noncompliant [[quickfixes=qf2]] | ||
| // fix@qf2 {{Replace with "isBlank()"}} | ||
| // edit@qf2 [[sc=11;ec=32]]{{isBlank()}} | ||
|
|
||
| b = s.strip().length() < 1; // Noncompliant [[quickfixes=qf3]] | ||
| // fix@qf3 {{Replace with "isBlank()"}} | ||
| // edit@qf3 [[sc=11;ec=31]]{{isBlank()}} | ||
|
|
||
| // test `strip().length() != 0` and equivalent code | ||
|
|
||
| b = s.strip().length() != 0; // Noncompliant [[quickfixes=qf4]] | ||
| // fix@qf4 {{Replace with "isBlank()"}} | ||
| // edit@qf4 [[sc=9;ec=9]]{{!}} | ||
| // edit@qf4 [[sc=11;ec=32]]{{isBlank()}} | ||
|
|
||
| b = s.strip().length() > 0; // Noncompliant [[quickfixes=qf5]] | ||
| // fix@qf5 {{Replace with "isBlank()"}} | ||
| // edit@qf5 [[sc=9;ec=9]]{{!}} | ||
| // edit@qf5 [[sc=11;ec=31]]{{isBlank()}} | ||
|
|
||
| b = s.strip().length() >= 1; // Noncompliant [[quickfixes=qf6]] | ||
| // fix@qf6 {{Replace with "isBlank()"}} | ||
| // edit@qf6 [[sc=9;ec=9]]{{!}} | ||
| // edit@qf6 [[sc=11;ec=32]]{{isBlank()}} | ||
|
|
||
| // reversed order | ||
|
|
||
| b = 0 == s.strip().length(); // Noncompliant [[quickfixes=qf7]] | ||
| // fix@qf7 {{Replace with "isBlank()"}} | ||
| // edit@qf7 [[sc=9;ec=14]]{{}} | ||
| // edit@qf7 [[sc=16;ec=32]]{{isBlank()}} | ||
|
|
||
| b = 0 != s.strip().length(); // Noncompliant [[quickfixes=qf8]] | ||
| // fix@qf8 {{Replace with "isBlank()"}} | ||
| // edit@qf8 [[sc=9;ec=14]]{{!}} | ||
| // edit@qf8 [[sc=16;ec=32]]{{isBlank()}} | ||
|
|
||
| // extra parentheses | ||
|
|
||
| b = (s.strip().length()) == 0; // Noncompliant [[quickfixes=qf9]] | ||
| // fix@qf9 {{Replace with "isBlank()"}} | ||
| // edit@qf9 [[sc=9;ec=10]]{{}} | ||
| // edit@qf9 [[sc=12;ec=34]]{{isBlank()}} | ||
|
|
||
| // the `strip()` call is not directly chained, so only emptiness can be simplified | ||
| b = (s.strip()).length() == 0; // Noncompliant {{Use "isEmpty()" to check whether a "String" is empty or not.}} [[quickfixes=qf10]] | ||
| // fix@qf10 {{Replace with "isEmpty()"}} | ||
| // edit@qf10 [[sc=21;ec=34]]{{isEmpty()}} | ||
|
|
||
| // chained method calls | ||
|
|
||
| b = s.toUpperCase().strip().length() == 0; // Noncompliant [[quickfixes=qf11]] | ||
| // fix@qf11 {{Replace with "isBlank()"}} | ||
| // edit@qf11 [[sc=25;ec=46]]{{isBlank()}} | ||
|
|
||
| b = sb.toString().strip().length() == 0; // Noncompliant [[quickfixes=qf12]] | ||
| // fix@qf12 {{Replace with "isBlank()"}} | ||
| // edit@qf12 [[sc=23;ec=44]]{{isBlank()}} | ||
|
|
||
| // `trim()` does not remove the same characters as `isBlank()` considers white space, | ||
| // so the message and the quick fix spell out what changes | ||
|
|
||
| b = s.trim().length() == 0; // Noncompliant {{Use "isBlank()" to check whether a "String" is blank or not. Use this if Unicode whitespace should be treated as blank and control characters as non-blank.}} [[quickfixes=qf13]] | ||
| // fix@qf13 {{Replace with "isBlank()" (Unicode whitespace is treated as blank, control characters as non-blank)}} | ||
| // edit@qf13 [[sc=11;ec=31]]{{isBlank()}} | ||
|
|
||
| b = s.trim().length() > 0; // Noncompliant [[quickfixes=qf14]] | ||
| // fix@qf14 {{Replace with "isBlank()" (Unicode whitespace is treated as blank, control characters as non-blank)}} | ||
| // edit@qf14 [[sc=9;ec=9]]{{!}} | ||
| // edit@qf14 [[sc=11;ec=30]]{{isBlank()}} | ||
|
|
||
| b = 0 == s.trim().length(); // Noncompliant [[quickfixes=qf15]] | ||
| // fix@qf15 {{Replace with "isBlank()" (Unicode whitespace is treated as blank, control characters as non-blank)}} | ||
| // edit@qf15 [[sc=9;ec=14]]{{}} | ||
| // edit@qf15 [[sc=16;ec=31]]{{isBlank()}} | ||
|
|
||
| b = s.toLowerCase().trim().length() == 0; // Noncompliant [[quickfixes=qf16]] | ||
| // fix@qf16 {{Replace with "isBlank()" (Unicode whitespace is treated as blank, control characters as non-blank)}} | ||
| // edit@qf16 [[sc=25;ec=45]]{{isBlank()}} | ||
|
|
||
| b = s.trim().length() == 1; | ||
| b = s.trim().length() >= 8; | ||
|
|
||
| b = s.strip().length() == 1; | ||
| b = s.strip().length() > 3; | ||
| b = s.strip().length() >= 8; | ||
| b = s.strip().length() == sb.length(); | ||
|
|
||
| b = s.isBlank(); | ||
| b = !s.isBlank(); | ||
|
|
||
| return b; | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
java-checks-test-sources/default/src/main/java/checks/StringIsBlankCheckSample_java10.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,29 @@ | ||
| package checks; | ||
|
|
||
| public class StringIsBlankCheckSample_java10 { | ||
| public boolean sample(String s) { | ||
| boolean b; | ||
|
|
||
| // `String.isBlank()` does not exist before Java 11, so these stay emptiness checks on the trimmed string | ||
|
|
||
| b = s.strip().length() == 0; // Noncompliant {{Use "isEmpty()" to check whether a "String" is empty or not.}} [[quickfixes=qf1]] | ||
| // fix@qf1 {{Replace with "isEmpty()"}} | ||
| // edit@qf1 [[sc=19;ec=32]]{{isEmpty()}} | ||
|
|
||
| b = s.strip().length() > 0; // Noncompliant {{Use "isEmpty()" to check whether a "String" is empty or not.}} [[quickfixes=qf2]] | ||
| // fix@qf2 {{Replace with "isEmpty()"}} | ||
| // edit@qf2 [[sc=9;ec=9]]{{!}} | ||
| // edit@qf2 [[sc=19;ec=31]]{{isEmpty()}} | ||
|
|
||
| b = s.trim().length() == 0; // Noncompliant {{Use "isEmpty()" to check whether a "String" is empty or not.}} [[quickfixes=qf3]] | ||
| // fix@qf3 {{Replace with "isEmpty()"}} | ||
| // edit@qf3 [[sc=18;ec=31]]{{isEmpty()}} | ||
|
|
||
| b = 0 == s.trim().length(); // Noncompliant {{Use "isEmpty()" to check whether a "String" is empty or not.}} [[quickfixes=qf4]] | ||
| // fix@qf4 {{Replace with "isEmpty()"}} | ||
| // edit@qf4 [[sc=9;ec=14]]{{}} | ||
| // edit@qf4 [[sc=23;ec=31]]{{isEmpty()}} | ||
|
|
||
| return b; | ||
| } | ||
| } |
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
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
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
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
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.