-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-4606 New rule S7158: use String.isEmpty() instead of String…
….length() == 0 (#4925) Co-authored-by: Alban Auzeill <alban.auzeill@sonarsource.com>
- Loading branch information
1 parent
708fe30
commit 8994f83
Showing
13 changed files
with
571 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
6 changes: 6 additions & 0 deletions
6
its/autoscan/src/test/resources/autoscan/diffs/diff_S7158.json
This file contains 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": "S7158", | ||
"hasTruePositives": true, | ||
"falseNegatives": 0, | ||
"falsePositives": 0 | ||
} |
46 changes: 46 additions & 0 deletions
46
its/ruling/src/test/resources/commons-beanutils/java-S7158.json
This file contains 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,46 @@ | ||
{ | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/BeanPropertyValueChangeClosure.java": [ | ||
137 | ||
], | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/BeanPropertyValueEqualsPredicate.java": [ | ||
167 | ||
], | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/BeanToPropertyValueTransformer.java": [ | ||
122 | ||
], | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/JDBCDynaClass.java": [ | ||
193 | ||
], | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/MappedPropertyDescriptor.java": [ | ||
90, | ||
157, | ||
203, | ||
326 | ||
], | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/PropertyUtilsBean.java": [ | ||
477, | ||
833, | ||
1578, | ||
1963 | ||
], | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/converters/CharacterConverter.java": [ | ||
72 | ||
], | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/converters/DateTimeConverter.java": [ | ||
329 | ||
], | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/converters/NumberConverter.java": [ | ||
261 | ||
], | ||
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/expression/DefaultResolver.java": [ | ||
80, | ||
93, | ||
118, | ||
144, | ||
167, | ||
182, | ||
205, | ||
228, | ||
266 | ||
] | ||
} |
72 changes: 72 additions & 0 deletions
72
its/ruling/src/test/resources/eclipse-jetty-similar-to-main/java-S7158.json
This file contains 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,72 @@ | ||
{ | ||
"org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/HttpCookie.java": [ | ||
267, | ||
298, | ||
315, | ||
317, | ||
382, | ||
396, | ||
400, | ||
508 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/PathMappings.java": [ | ||
202 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/ServletPathSpec.java": [ | ||
69 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-io/src/main/java/org/eclipse/jetty/io/AbstractEndPoint.java": [ | ||
469 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/Cookies.java": [ | ||
67 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/CustomRequestLog.java": [ | ||
427, | ||
476 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/PushBuilderImpl.java": [ | ||
166, | ||
174 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/RequestLogWriter.java": [ | ||
80 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java": [ | ||
262, | ||
388 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/Response.java": [ | ||
1299 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java": [ | ||
1624, | ||
1822, | ||
2228 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/handler/jmx/AbstractHandlerMBean.java": [ | ||
79, | ||
83 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/session/DefaultSessionIdManager.java": [ | ||
236 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java": [ | ||
425, | ||
468 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/HttpConnectionTest.java": [ | ||
719, | ||
735, | ||
768, | ||
784 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java": [ | ||
919, | ||
934, | ||
964, | ||
1479, | ||
1652, | ||
1667 | ||
] | ||
} |
161 changes: 161 additions & 0 deletions
161
its/ruling/src/test/resources/eclipse-jetty/java-S7158.json
This file contains 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,161 @@ | ||
{ | ||
"org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/HttpCookie.java": [ | ||
267, | ||
298, | ||
315, | ||
317, | ||
382, | ||
396, | ||
400, | ||
508 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/PathMappings.java": [ | ||
202 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/ServletPathSpec.java": [ | ||
69 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-io/src/main/java/org/eclipse/jetty/io/AbstractEndPoint.java": [ | ||
469 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/Cookies.java": [ | ||
67 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/CustomRequestLog.java": [ | ||
427, | ||
476 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/PushBuilderImpl.java": [ | ||
166, | ||
174 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/RequestLogWriter.java": [ | ||
80 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java": [ | ||
262, | ||
388 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/Response.java": [ | ||
1299 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java": [ | ||
1624, | ||
1822, | ||
2228 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/handler/jmx/AbstractHandlerMBean.java": [ | ||
79, | ||
83 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/main/java/org/eclipse/jetty/server/session/DefaultSessionIdManager.java": [ | ||
236 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/ConnectorTimeoutTest.java": [ | ||
425, | ||
468 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/DumpHandler.java": [ | ||
158 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/HttpConnectionTest.java": [ | ||
719, | ||
735, | ||
768, | ||
784 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java": [ | ||
919, | ||
934, | ||
964, | ||
1479, | ||
1652, | ||
1667 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java": [ | ||
1149 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/StopTest.java": [ | ||
210 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/handler/InetAccessHandlerTest.java": [ | ||
93, | ||
100, | ||
107, | ||
114, | ||
123 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLCloseTest.java": [ | ||
79 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLSelectChannelConnectorLoadTest.java": [ | ||
290 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLoggerConfiguration.java": [ | ||
180 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-slf4j-impl/src/main/java/org/eclipse/jetty/logging/JettyLoggerFactory.java": [ | ||
85, | ||
120 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/MultiReleaseJarFile.java": [ | ||
243 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/PathWatcher.java": [ | ||
420 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/QuotedStringTokenizer.java": [ | ||
278, | ||
342, | ||
595 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java": [ | ||
174 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java": [ | ||
66, | ||
300, | ||
303, | ||
344, | ||
347, | ||
625, | ||
631, | ||
680, | ||
686, | ||
1029, | ||
1122 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java": [ | ||
131, | ||
208, | ||
242, | ||
282, | ||
365, | ||
443, | ||
588 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/component/AbstractLifeCycle.java": [ | ||
339 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarFileResource.java": [ | ||
126 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/resource/PathResource.java": [ | ||
341 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java": [ | ||
103, | ||
210, | ||
251 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/security/Password.java": [ | ||
219, | ||
223, | ||
234 | ||
], | ||
"org.eclipse.jetty:jetty-project:jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java": [ | ||
1324, | ||
1333, | ||
1485 | ||
] | ||
} |
This file contains 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,28 @@ | ||
{ | ||
"com.google.guava:guava:src/com/google/common/base/Splitter.java": [ | ||
176 | ||
], | ||
"com.google.guava:guava:src/com/google/common/base/Strings.java": [ | ||
78 | ||
], | ||
"com.google.guava:guava:src/com/google/common/io/Files.java": [ | ||
730 | ||
], | ||
"com.google.guava:guava:src/com/google/common/net/InetAddresses.java": [ | ||
233, | ||
247, | ||
250 | ||
], | ||
"com.google.guava:guava:src/com/google/common/net/InternetDomainName.java": [ | ||
259 | ||
], | ||
"com.google.guava:guava:src/com/google/common/primitives/ParseRequest.java": [ | ||
36 | ||
], | ||
"com.google.guava:guava:src/com/google/common/primitives/UnsignedLongs.java": [ | ||
304 | ||
], | ||
"com.google.guava:guava:src/com/google/thirdparty/publicsuffix/TrieParser.java": [ | ||
85 | ||
] | ||
} |
This file contains 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,5 @@ | ||
{ | ||
"org.sonarsource.sonarqube:sonar-server:src/main/java/org/sonar/server/user/UserUpdater.java": [ | ||
325 | ||
] | ||
} |
54 changes: 54 additions & 0 deletions
54
java-checks-test-sources/default/src/main/java/checks/StringIsEmptyCheckSample.java
This file contains 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,54 @@ | ||
package checks; | ||
|
||
public class StringIsEmptyCheckSample { | ||
public boolean sample(String s, String t) { | ||
boolean b; | ||
|
||
// test `length() == 0` and equivalent code | ||
b = s.length() == 0; // Noncompliant | ||
b = s.length() <= 0; // Noncompliant | ||
b = s.length() < 1; // Noncompliant | ||
|
||
// test `length() != 0` and equivalent code | ||
b = s.length() != 0; // Noncompliant | ||
b = s.length() > 0; // Noncompliant | ||
b = s.length() >= 1; // Noncompliant | ||
|
||
// reversed order | ||
b = 0 == s.length(); // Noncompliant | ||
b = 0 >= s.length(); // Noncompliant | ||
b = 1 > s.length(); // Noncompliant | ||
b = 0 != s.length(); // Noncompliant | ||
b = 0 < s.length(); // Noncompliant | ||
b = 1 <= s.length(); // Noncompliant | ||
|
||
// extra parentheses | ||
b = (s.length()) == 0; // Noncompliant | ||
|
||
// chained method calls | ||
b = s.toUpperCase().length() == 0; // Noncompliant | ||
|
||
// problem in a nested expression | ||
b = "abc".equals(s) || s.length() == 0; // Noncompliant | ||
|
||
b = s.length() == 1; | ||
b = s.length() > 3; | ||
b = s.length() <= 10; | ||
b = 2 < s.length(); | ||
|
||
b = s.trim().length() >= 8; | ||
|
||
b = s.length() == t.length(); | ||
|
||
b = s.isEmpty(); | ||
b = !s.isEmpty(); | ||
|
||
b = 1 < 0; | ||
|
||
// StringBuilder does not have `isEmpty()` | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
b = stringBuilder.length() == 0; | ||
|
||
return b; | ||
} | ||
} |
Oops, something went wrong.