From fa9fe66b0cb1b6ab8d7f341cc1f7908897a9cf33 Mon Sep 17 00:00:00 2001
From: benzonico Proper indentation is a simple and effective way to improve the code's readability. Consistent indentation among the developers within a team also
reduces the differences that are committed to source control systems, making code reviews easier. By default this rule checks that each block of code is indented, although it does not check the size of the indent. Parameter "indentSize" allows
-the expected indent size to be defined. Only the first line of a badly indented section is reported. This rule raises an issue when indentation does not match the configured value. Only the first line of a badly indented section is reported. With an indent size of 2: Override and implementation methods are excluded, as are parameters annotated with The rule will not raise issues for unused parameters: Java's garbage collection cannot be relied on to clean up everything. Specifically, connections, streams, files and other classes that implement
-the Connections, streams, files, and other classes that implement the Failure to properly close resources will result in a resource leak which could bring first the application and then perhaps the box it's on to
their knees. If When a parent class references a static member of a subclass during its own initialization, the results will not be what you expect because the
-child class won't exist yet. In a best-case scenario, you'll see immediate failures in the code as a result. Worst-case, the damage will be more insidious and difficult to
-track down. When a parent class references a member of a subclass during its own initialization, the results might not be what you expect because the child
+class might not have been initialized yet. This could create what is known as an "initialisation cycle", or even a deadlock in some extreme cases. To make things worse, these issues are very hard to diagnose so it is highly recommended you avoid creating this kind of dependencies. This rule raises an issue if on methods that contain only one statement: the Methods with annotations, such as Noncompliant Code Example
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1172_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1172_java.html
index 2cf1b8b4025..5dff0661d7d 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1172_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1172_java.html
@@ -12,8 +12,15 @@
Compliant Solution
}
Exceptions
-@Observes
, and methods that are intended to be
-overridden.
+
@javax.enterprise.event.Observes
default
methods throw
or that have empty bodies @SuppressWarning("unchecked")
or @SuppressWarning("rawtypes")
, in
+ which case the annotation will be ignored
@Override
void doSomething(int a, int b) { // no issue reported on b
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1449_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1449_java.json
index 24c09b0326a..2c87caaf3f7 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1449_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1449_java.json
@@ -1,6 +1,6 @@
{
"title": "Locale should be used in String operations",
- "type": "BUG",
+ "type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1751_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1751_java.json
index 57fdd6a70c7..5c42ab8b256 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1751_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1751_java.json
@@ -1,6 +1,6 @@
{
"title": "Jump statements should not be used unconditionally",
- "type": "BUG",
+ "type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1948_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1948_java.json
index 4d3b3fe079b..979fee9beec 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1948_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1948_java.json
@@ -1,6 +1,6 @@
{
"title": "Fields in a \"Serializable\" class should either be transient or serializable",
- "type": "BUG",
+ "type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2095_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2095_java.html
index d33b696e0bc..31b3fcf22ec 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2095_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2095_java.html
@@ -1,6 +1,7 @@
-
Closeable
interface or its super-interface, AutoCloseable
, must be manually closed after creation. Further, that
-close
call must be made in a finally
block, otherwise an exception could keep the call from being made. Closeable
interface or its super-interface,
+AutoCloseable
, needs to be closed after use. Further, that close
call must be made in a finally
block otherwise
+an exception could keep the call from being made. Preferably, when class implements AutoCloseable
, resource should be created using
+"try-with-resources" pattern and will be closed automatically.Noncompliant Code Example
@@ -10,6 +11,8 @@ Noncompliant Code Example
BufferedReader reader = Files.newBufferedReader(path, this.charset)) {
// ...
reader.close(); // Noncompliant
+ // ...
+ Files.lines("input.txt").forEach(System.out::println); // Noncompliant: The stream needs to be closed
}
private void doSomething() {
@@ -28,17 +31,16 @@ Noncompliant Code Example
Compliant Solution
-private void readTheFile() throws IOException {
- Path path = Paths.get(this.fileName);
- BufferedReader reader = null;
- try {
- reader = Files.newBufferedReader(path, this.charset)) {
- // ...
- } finally {
- if (reader != null) {
- reader.close();
+private void readTheFile(String fileName) throws IOException {
+ Path path = Paths.get(fileName);
+ try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
+ reader.readLine();
+ // ...
+ }
+ // ..
+ try (Stream<String> input = Files.lines("input.txt")) {
+ input.forEach(System.out::println);
}
- }
}
private void doSomething() {
@@ -71,5 +73,6 @@
See
Noncompliant Code Example
public void doSometing() {...}
}
+Exceptions
+run()
is not overridden in a class extending Thread
, it means that starting the thread will actually call
+Thread.run()
. However, Thread.run()
does nothing if it has not been fed with a target Runnable
. The rule
+consequently ignore classes extending Thread
if they are calling, in their constructors, the super(...)
constructor with a
+proper Runnable
target.
+class MyThread extends Thread { // Compliant - calling super constructor with a Runnable
+ MyThread(Runnable target) {
+ super(target); // calling super constructor with a Runnable, which will be used for when Thread.run() is executed
+ // ...
+ }
+}
+
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2225_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2225_java.html
index 22ffc4c2379..0fa2f6503f7 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2225_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2225_java.html
@@ -20,5 +20,7 @@ Compliant Solution
See
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2225_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2225_java.json
index 4bf4b4ba01c..30d1c4b6bf1 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2225_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2225_java.json
@@ -7,10 +7,12 @@
"constantCost": "5min"
},
"tags": [
- "cwe"
+ "cwe",
+ "cert"
],
"standards": [
- "CWE"
+ "CWE",
+ "CERT"
],
"defaultSeverity": "Major"
}
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2390_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2390_java.html
index db9e3dd6664..c36dac5806d 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2390_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2390_java.html
@@ -1,15 +1,21 @@
-Noncompliant Code Example
class Parent {
- public static final int childVersion = Child.version;
+ static int field1 = Child.method(); // Noncompliant
+ static int field2 = 42;
+
+ public static void main(String[] args) {
+ System.out.println(Parent.field1); // will display "0" instead of "42"
+ }
}
class Child extends Parent {
- public static final int version = 6;
+ static int method() {
+ return Parent.field2;
+ }
}
See
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2390_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2390_java.json
index 0ae2c6f44d8..543938260ed 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2390_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2390_java.json
@@ -1,6 +1,6 @@
{
- "title": "Classes should not access static members of their own subclasses during initialization",
- "type": "BUG",
+ "title": "Classes should not access their own subclasses during initialization",
+ "type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
@@ -12,5 +12,5 @@
"standards": [
"CERT"
],
- "defaultSeverity": "Major"
+ "defaultSeverity": "Critical"
}
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2444_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2444_java.json
index 037a2df326e..c037dfa3b17 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2444_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2444_java.json
@@ -1,6 +1,6 @@
{
"title": "Lazy initialization of \"static\" fields should be \"synchronized\"",
- "type": "BUG",
+ "type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
@@ -9,5 +9,5 @@
"tags": [
"multi-threading"
],
- "defaultSeverity": "Major"
+ "defaultSeverity": "Critical"
}
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2447_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2447_java.html
index 104e3c42c07..f2791b81c13 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2447_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2447_java.html
@@ -8,4 +8,10 @@ Noncompliant Code Example
return null; // Noncompliant
}
+See
+
+
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2447_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2447_java.json
index b286ad7aa67..a08c12f8379 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2447_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2447_java.json
@@ -7,7 +7,13 @@
"constantCost": "20min"
},
"tags": [
+ "cwe",
+ "cert",
"pitfall"
],
+ "standards": [
+ "CWE",
+ "CERT"
+ ],
"defaultSeverity": "Critical"
}
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2629_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2629_java.html
index c8c202bc08d..57fd678253b 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2629_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2629_java.html
@@ -10,6 +10,8 @@ Noncompliant Code Example
logger.log(Level.DEBUG, "Something went wrong: " + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages
+logger.fine("An exception occurred with message: " + message); // Noncompliant
+
LOG.error("Unable to open file " + csvPath, e); // Noncompliant
Preconditions.checkState(a > 0, "Arg must be positive, but got " + a); // Noncompliant. String concatenation performed even when a > 0
@@ -22,6 +24,8 @@
Compliant Solution
logger.log(Level.SEVERE, "Something went wrong: %s ", message); // String formatting only applied if needed
+logger.log(Level.FINE, "An exception occurred with message: {}", message);
+
logger.log(Level.SEVERE, () -> "Something went wrong: " + message); // since Java 8, we can use Supplier , which will be evaluated lazily
LOG.error("Unable to open file {}", csvPath, e);
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2637_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2637_java.html
index e3631474888..fcc949d6c9a 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2637_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2637_java.html
@@ -25,4 +25,10 @@
+Noncompliant Code Example
return mix; // Noncompliant; return value is Nonnull, but null is returned.}}
}
See
+
+
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2637_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2637_java.json
index 88ee3d650ae..27406771334 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2637_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2637_java.json
@@ -7,7 +7,12 @@
"constantCost": "15min"
},
"tags": [
-
+ "cwe",
+ "cert"
+ ],
+ "standards": [
+ "CWE",
+ "CERT"
],
"defaultSeverity": "Minor"
}
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2693_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2693_java.html
index 0415a168788..8acd1763021 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2693_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2693_java.html
@@ -13,4 +13,9 @@ Noncompliant Code Example
}
}
+See
+
+
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2693_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2693_java.json
index 67ee8d425ea..392ec95dfca 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2693_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2693_java.json
@@ -8,7 +8,11 @@
},
"tags": [
"multi-threading",
+ "cert",
"pitfall"
],
+ "standards": [
+ "CERT"
+ ],
"defaultSeverity": "Blocker"
}
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2699_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2699_java.html
index 6dd20bc00e0..96e175c705d 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2699_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S2699_java.html
@@ -8,10 +8,12 @@
org.springframework.test.web.servlet.ResultActions.andExpect()
Noncompliant Code Example
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3010_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3010_java.json
index 2359262ed06..8ce0e4f4da4 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3010_java.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3010_java.json
@@ -1,6 +1,6 @@
{
"title": "Static fields should not be updated in constructors",
- "type": "BUG",
+ "type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3400_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3400_java.html
index 96aba0e336e..e3f46b74fc5 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3400_java.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3400_java.html
@@ -3,13 +3,13 @@
return
of a constant value. Noncompliant Code Example
-public int getBestNumber() {
+int getBestNumber() {
return 12; // Noncompliant
}
Compliant Solution
-public static int bestNumber = 12;
+static int bestNumber = 12;
Exceptions
@Override
and Spring's @RequestMapping
, are ignored.
This rule raises an issue when a Serializable
class defines a non-transient, non-static field field whose type is a known serializable
value-based class. Known serializable value-based classes are: all the classes in the java.time
package except Clock
; the
-date classes for alternate calendars: HijrahDate
, JapaneseDate
, MinguaDate
, ThaiBuddhistDate
.
HijrahDate
, JapaneseDate
, MinguoDate
, ThaiBuddhistDate
.
class MyClass implements Serializable { diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3958_java.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3958_java.html index 2d0c3793142..a839bb75228 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3958_java.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S3958_java.html @@ -19,4 +19,4 @@Compliant Solution
boolean hasRed = widgets.stream().anyMatch(w -> w.getColor() == RED);+ diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S4034_java.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S4034_java.json index e4537851168..ee7b3dd7e26 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S4034_java.json +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S4034_java.json @@ -10,4 +10,4 @@ "clumsy" ], "defaultSeverity": "Minor" -} \ No newline at end of file +}