Skip to content
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

SONARJAVA-4606 New rule: use String.isEmpty() instead of String.length() == 0 #4925

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
orchestrator_LATEST_RELEASE_cache:
<<: *ORCHESTRATOR_CACHE_ELEMENTS_DEFINITION
- env:
SQ_VERSION: DEV
SQ_VERSION: DEV[10.8]
orchestrator_DEV_cache:
<<: *ORCHESTRATOR_CACHE_ELEMENTS_DEFINITION

Expand Down Expand Up @@ -269,7 +269,7 @@
actual_artifacts:
path: "${CIRRUS_WORKING_DIR}/its/autoscan/target/actual/**/*"

promote_task:

Check warning on line 272 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L272

task "promote" depends on task "ws_scan", but their only_if conditions are different

Check warning on line 272 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L272

task "promote" depends on task "ws_scan", but their only_if conditions are different

Check warning on line 272 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L272

task "promote" depends on task "ws_scan", but their only_if conditions are different

Check warning on line 272 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L272

task "promote" depends on task "ws_scan", but their only_if conditions are different

Check warning on line 272 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L272

task "promote" depends on task "ws_scan", but their only_if conditions are different

Check warning on line 272 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L272

task "promote" depends on task "ws_scan", but their only_if conditions are different

Check warning on line 272 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L272

task "promote" depends on task "ws_scan", but their only_if conditions are different

Check warning on line 272 in .cirrus.yml

View check run for this annotation

Cirrus CI / Build Parsing Results

.cirrus.yml#L272

task "promote" depends on task "ws_scan", but their only_if conditions are different
depends_on:
- build
- test_analyze
Expand Down
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 its/ruling/src/test/resources/commons-beanutils/java-S7158.json
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
]
}
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 its/ruling/src/test/resources/eclipse-jetty/java-S7158.json
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
]
}
28 changes: 28 additions & 0 deletions its/ruling/src/test/resources/guava/java-S7158.json
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
]
}
5 changes: 5 additions & 0 deletions its/ruling/src/test/resources/sonar-server/java-S7158.json
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
]
}
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;
}
}
Loading