diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S106.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S106.html index 6a3e8f1db39..9db35e9790d 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S106.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S106.html @@ -1,30 +1,35 @@

Why is this an issue?

-

When logging a message, the following requirements should be fulfilled:

+

In software development, logs serve as a record of events within an application, providing crucial insights for debugging. When logging, it is +essential to ensure that the logs are:

-

Simply printing messages to System.out or System.err does not fulfill these needs. A dedicated logger should be used -instead.

-

Noncompliant code example

+

Those requirements are not met if a program directly writes to the standard outputs (e.g., System.out, System.err). That is why defining and using +a dedicated logger is highly recommended.

+

Code examples

+

The following noncompliant code:

-class PrintMessage {
-  public void print() {
+class MyClass {
+  public void doSomething() {
     System.out.println("My Message");  // Noncompliant, output directly to System.out without a logger
   }
 }
 
-

Compliant solution

+

Could be replaced by:

 import java.util.logging.Logger;
 
-class PrintMessage {
+class MyClass {
 
   Logger logger = Logger.getLogger(getClass().getName());
 
-  public void print() {
+  public void doSomething() {
+    // ...
     logger.info("My Message");  // Compliant, output via logger
+    // ...
   }
 }
 
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.html index 66cbe52247b..ae2343b2064 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.html @@ -1,16 +1,36 @@

Why is this an issue?

-

Merging collapsible if statements increases the code’s readability.

-

Noncompliant code example

+

Nested code - blocks of code inside blocks of code - is eventually necessary, but increases complexity. This is why keeping the code as flat as +possible, by avoiding unnecessary nesting, is considered a good practice.

+

Merging if statements when possible will decrease the nesting of the code and improve its readability.

+

Code like

+
+if (condition1) {
+  if (condition2) {             // Noncompliant
+    /* ... */
+  }
+}
+
+

Will be more readable as

+
+if (condition1 && condition2) { // Compliant
+  /* ... */
+}
+
+

How to fix it

+

If merging the conditions seems to result in a more complex code, extracting the condition or part of it in a named function or variable is a +better approach to fix readability.

+

Code examples

+

Noncompliant code example

 if (file != null) {
-  if (file.isFile() || file.isDirectory()) {
+  if (file.isFile() || file.isDirectory()) {  // Noncompliant
     /* ... */
   }
 }
 
-

Compliant solution

+

Compliant solution

-if (file != null && isFileOrDirectory(file)) {
+if (file != null && isFileOrDirectory(file)) { // Compliant
   /* ... */
 }
 
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.json
index c3c1ca9b08c..b7f8fc91b8c 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.json
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.json
@@ -1,5 +1,5 @@
 {
-  "title": "Collapsible \"if\" statements should be merged",
+  "title": "Mergeable \"if\" statements should be combined",
   "type": "CODE_SMELL",
   "code": {
     "impacts": {
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1075.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1075.html
index 1f07840f0fe..bfa52b4f018 100644
--- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1075.html
+++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1075.html
@@ -1,12 +1,19 @@
 

Why is this an issue?

-

Hard coding a URI makes it difficult to test a program: path literals are not always portable across operating systems, a given absolute path may -not exist on a specific test environment, a specified Internet URL may not be available when executing the tests, production environment filesystems -usually differ from the development environment, …​etc. For all those reasons, a URI should never be hard coded. Instead, it should be replaced by -customizable parameter.

-

Further even if the elements of a URI are obtained dynamically, portability can still be limited if the path-delimiters are hard-coded.

-

This rule raises an issue when URI’s or path delimiters are hard coded.

-

Noncompliant code example

-
+

Hard-coding a URI makes it difficult to test a program for a variety of reasons:

+ +

In addition, hard-coded URIs can contain sensitive information, like IP addresses, and they should not be stored in the code.

+

For all those reasons, a URI should never be hard coded. Instead, it should be replaced by a customizable parameter.

+

Further, even if the elements of a URI are obtained dynamically, portability can still be limited if the path delimiters are hard-coded.

+

This rule raises an issue when URIs or path delimiters are hard-coded.

+

How to fix it

+

Code examples

+

Noncompliant code example

+
 public class Foo {
   public Collection<User> listUsers() {
     File userList = new File("/home/mylogin/Dev/users.txt"); // Noncompliant
@@ -15,8 +22,8 @@ 

Noncompliant code example

} }
-

Compliant solution

-
+

Compliant solution

+
 public class Foo {
   // Configuration is a class that returns customizable properties: it can be mocked to be injected during tests.
   private Configuration config;
@@ -33,8 +40,4 @@ 

Compliant solution

} }
-

Resources

- diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S109.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S109.html index 91120b958b0..129dd97623c 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S109.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S109.html @@ -2,8 +2,8 @@ readable and maintainable.

Why is this an issue?

Magic numbers make the code more complex to understand as it requires the reader to have knowledge about the global context to understand the -number itself. Their usage may seem obvious at the moment you’re writing the code, but it may not be the case for another developer or later once the -context faded away. -1, 0 and 1 are not considered magic numbers.

+number itself. Their usage may seem obvious when writing the code, but it may not be the case for another developer or later once the context faded +away. -1, 0, and 1 are not considered magic numbers.

Exceptions

This rule ignores hashCode methods.

How to fix it

diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1110.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1110.html index de7eeaf8a62..9bd44f7d504 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1110.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1110.html @@ -1,24 +1,24 @@

Why is this an issue?

-

The use of parentheses, even those not required to enforce a desired order of operations, can clarify the intent behind a piece of code. But -redundant pairs of parentheses could be misleading, and should be removed.

-

Noncompliant code example

+

Parentheses can disambiguate the order of operations in complex expressions and make the code easier to understand.

-int x = (y / 2 + 1);   //Compliant even if the parenthesis are ignored by the compiler
+a = (b * c) + (d * e); // Compliant: the intent is clear.
+
+

Redundant parentheses are parenthesis that do not change the behavior of the code, and do not clarify the intent. They can mislead and complexify +the code. They should be removed.

+

Noncompliant code example

+
+int x = ((y / 2 + 1)); // Noncompliant
 
-if (a && ((x+y > 0))) {  // Noncompliant
-  //...
+if (a && ((x + y > 0))) { // Noncompliant
+  return ((x + 1)); // Noncompliant
 }
-
-return ((x + 1));  // Noncompliant
 

Compliant solution

-
+
 int x = (y / 2 + 1);
 
-if (a && (x+y > 0)) {
-  //...
+if (a && (x + y > 0)) {
+  return (x + 1);
 }
-
-return (x + 1);
 
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1116.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1116.html index f98f80a5496..e9bedc56c37 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1116.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1116.html @@ -1,32 +1,32 @@

Why is this an issue?

-

Empty statements, i.e. ;, are usually introduced by mistake, for example because:

-
    -
  • It was meant to be replaced by an actual statement, but this was forgotten.
  • -
  • There was a typo which lead the semicolon to be doubled, i.e. ;;.
  • -
-

Noncompliant code example

-
+

Empty statements represented by a semicolon ; are statements that do not perform any operation. They are often the result of a typo or +a misunderstanding of the language syntax. It is a good practice to remove empty statements since they don’t add value and lead to confusion and +errors.

+

Code examples

+

Noncompliant code example

+
 void doSomething() {
-  ;                                                       // Noncompliant - was used as a kind of TODO marker
+  ; // Noncompliant - was used as a kind of TODO marker
 }
 
 void doSomethingElse() {
-  System.out.println("Hello, world!");;                     // Noncompliant - double ;
-  ...
+  System.out.println("Hello, world!");; // Noncompliant - double ;
+  // ...
 }
 
-

Compliant solution

-
+

Compliant solution

+
 void doSomething() {}
 
 void doSomethingElse() {
   System.out.println("Hello, world!");
-  ...
-  for (int i = 0; i < 3; i++) ; // compliant if unique statement of a loop
-  ...
+  // ...
+  for (int i = 0; i < 3; i++) ; // Compliant if unique statement of a loop
+  // ...
 }
 

Resources

+

Documentation

  • CERT, MSC12-C. - Detect and remove code that has no effect or is never executed
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1117.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1117.html index 5942c2b9bc0..4bb618fc797 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1117.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1117.html @@ -1,22 +1,36 @@

    Why is this an issue?

    -

    Overriding or shadowing a variable declared in an outer scope can strongly impact the readability, and therefore the maintainability, of a piece of -code. Further, it could lead maintainers to introduce bugs because they think they’re using one variable but are really using another.

    +

    Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.

    +

    This can lead to three main problems:

    +
      +
    • Confusion: The same name can refer to different variables in different parts of the scope, making the code hard to read and understand.
    • +
    • Unintended Behavior: You might accidentally use the wrong variable, leading to hard-to-detect bugs.
    • +
    • Maintenance Issues: If the inner variable is removed or renamed, the code’s behavior might change unexpectedly because the outer variable is + now being used.
    • +
    +

    To avoid these problems, rename the shadowing, shadowed, or both identifiers to accurately represent their purpose with unique and meaningful +names.

    +

    This rule focuses on variables in methods that shadow a field.

    +

    Noncompliant code example

     class Foo {
       public int myField;
     
       public void doSomething() {
         int myField = 0; // Noncompliant
    -    ...
    +    // ...
       }
     }
     

    Resources

    Documentation

    +

    Related rules

    +
      +
    • {rule:java:S2176} - Class names should not shadow interfaces or superclasses
    • +
    • {rule:java:S2387} - Child class fields should not shadow parent class fields
    • +
    • {rule:java:S4977} - Type parameters should not shadow other type parameters
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1117.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1117.json index 9ae0e80dafd..53a3db8457a 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1117.json +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1117.json @@ -1,5 +1,4 @@ { - "title": "Local variables should not shadow class fields", "type": "CODE_SMELL", "code": { "impacts": { @@ -22,10 +21,10 @@ "sqKey": "S1117", "scope": "All", "quickfix": "unknown", + "title": "Local variables should not shadow class fields", "securityStandards": { "CERT": [ - "DCL51-J.", - "DCL01-C." + "DCL51-J." ] } } diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1118.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1118.html index d009768e2ee..d95ef6297e7 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1118.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1118.html @@ -1,9 +1,14 @@

    Why is this an issue?

    -

    Utility classes, which are collections of static members, are not meant to be instantiated. Even abstract utility classes, which can -be extended, should not have public constructors.

    -

    Java adds an implicit public constructor to every class which does not define at least one explicitly. Hence, at least one non-public constructor -should be defined.

    -

    Noncompliant code example

    +

    Whenever there are portions of code that are duplicated and do not depend on the state of their container class, they can be centralized inside a +"utility class". A utility class is a class that only has static members, hence it should not be instantiated.

    +

    Exceptions

    +

    When a class contains public static void main(String[] args) method it is not considered as a utility class and will be ignored by +this rule.

    +

    How to fix it

    +

    To prevent the class from being instantiated, you should define a non-public constructor. This will prevent the compiler from implicitly generating +a public parameterless constructor.

    +

    Code examples

    +

    Noncompliant code example

     class StringUtils { // Noncompliant
     
    @@ -13,7 +18,7 @@ 

    Noncompliant code example

    }
    -

    Compliant solution

    +

    Compliant solution

     class StringUtils { // Compliant
     
    @@ -27,7 +32,4 @@ 

    Compliant solution

    }
    -

    Exceptions

    -

    When class contains public static void main(String[] args) method it is not considered as utility class and will be ignored by this -rule.

    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S112.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S112.html index 91d808c869f..f347446dfe5 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S112.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S112.html @@ -1,35 +1,63 @@ +

    This rule raises an issue when a generic exception (such as Error, RuntimeException, Throwable, or +Exception) is thrown.

    Why is this an issue?

    -

    Using such generic exceptions as Error, RuntimeException, Throwable, and Exception prevents -calling methods from handling true, system-generated exceptions differently than application-generated errors.

    -

    Noncompliant code example

    -
    -public void foo(String bar) throws Throwable {  // Noncompliant
    -  throw new RuntimeException("My Message");     // Noncompliant
    -}
    -
    -

    Compliant solution

    -
    -public void foo(String bar) {
    -  throw new MyOwnRuntimeException("My Message");
    -}
    -
    +

    Throwing generic exceptions such as Error, RuntimeException, Throwable, and Exception will have +a negative impact on any code trying to catch these exceptions.

    +

    From a consumer perspective, it is generally a best practice to only catch exceptions you intend to handle. Other exceptions should ideally be let +to propagate up the stack trace so that they can be dealt with appropriately. When a generic exception is thrown, it forces consumers to catch +exceptions they do not intend to handle, which they then have to re-throw.

    +

    Besides, when working with a generic type of exception, the only way to distinguish between multiple exceptions is to check their message, which is +error-prone and difficult to maintain. Legitimate exceptions may be unintentionally silenced and errors may be hidden.

    +

    For instance, when a Throwable is caught and not re-thrown, it may mask errors such as OutOfMemoryError and prevent the +program from terminating gracefully.

    +

    When throwing an exception, it is therefore recommended to throw the most specific exception possible so that it can be handled intentionally by +consumers.

    Exceptions

    -

    Generic exceptions in the signatures of overriding methods are ignored, because overriding method has to follow signature of the throw declaration -in the superclass. The issue will be raised on superclass declaration of the method (or won’t be raised at all if superclass is not part of the -analysis).

    +

    Generic exceptions in the signatures of overriding methods are ignored, because an overriding method has to follow the signature of the throw +declaration in the superclass. The issue will be raised on superclass declaration of the method (or won’t be raised at all if superclass is not part +of the analysis).

     @Override
     public void myMethod() throws Exception {...}
     

    Generic exceptions are also ignored in the signatures of methods that make calls to methods that throw generic exceptions.

    -public void myOtherMethod throws Exception {
    +public void myOtherMethod() throws Exception {
       doTheThing();  // this method throws Exception
     }
     
    +

    How to fix it

    +

    To fix this issue, make sure to throw specific exceptions that are relevant to the context in which they arise. It is recommended to either:

    +
      +
    • Raise a specific exception from the Java standard library when one matches. For example an IllegalArgumentException should be + thrown when a method receives an invalid argument.
    • +
    • Create a custom exception class deriving from Exception or one of its subclasses.
    • +
    +

    Code examples

    +

    Noncompliant code example

    +
    +void checkValue(int value) throws Throwable { // Noncompliant: signature is too broad
    +    if (value == 42) {
    +        throw new RuntimeException("Value is 42"); // Noncompliant: This will be difficult for consumers to handle
    +    }
    +}
    +
    +

    Compliant solution

    +
    +void checkValue(int value) {
    +    if (value == 42) {
    +        throw new IllegalArgumentException("Value is 42"); // Compliant
    +    }
    +}
    +

    Resources

    +

    Standards

    + +

    Related rules

      -
    • MITRE, CWE-397 - Declaration of Throws for Generic Exception
    • -
    • CERT, ERR07-J. - Do not throw RuntimeException, Exception, or Throwable
    • +
    • {rule:java:S1181} - Generic exceptions should not be caught
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1121.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1121.html index 62fc817b7d1..6d7f6345993 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1121.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1121.html @@ -1,35 +1,53 @@

    Why is this an issue?

    -

    Assignments within sub-expressions are hard to spot and therefore make the code less readable. Ideally, sub-expressions should not have -side-effects.

    -

    Noncompliant code example

    -
    -if ((str = cont.substring(pos1, pos2)).isEmpty()) {  // Noncompliant
    -  //...
    -
    -

    Compliant solution

    -
    -str = cont.substring(pos1, pos2);
    -if (str.isEmpty()) {
    -  //...
    -
    +

    A common code smell that can hinder the clarity of source code is making assignments within sub-expressions. This practice involves assigning a +value to a variable inside a larger expression, such as within a loop or a conditional statement.

    +

    This practice essentially gives a side-effect to a larger expression, thus making it less readable. This often leads to confusion and potential +errors.

    Exceptions

    -

    Assignments in while statement conditions, and assignments enclosed in relational expressions are ignored.

    +

    This rule ignores assignments in conditions of while statements and assignments enclosed in relational expressions.

    -BufferedReader br = new BufferedReader(/* ... */);
    -String line;
    -while ((line = br.readLine()) != null) {...}
    -if ((i = j) >= 1) {...}
    +void processInput(BufferedReader br) {
    +  String line;
    +  while ((line = br.readLine()) != null) {
    +    processLine(line);
    +  }
    +}
    +
    +Object foo;
    +if ((foo = bar()) != null) {
    +  // do something with "foo"
    +}
     
    -

    Chained assignments, including compound assignments, are ignored.

    +

    This rule also ignores chained assignments, including compound assignments.

    -int i = j = 0;
    +int j, i = j = 0;
     int k = (j += 1);
    +byte[] result, bresult;
     result = (bresult = new byte[len]);
     
    +

    How to fix it

    +

    Making assignments within sub-expressions can hinder the clarity of source code.

    +

    This practice essentially gives a side-effect to a larger expression, thus making it less readable. This often leads to confusion and potential +errors.

    +

    Extracting assignments into separate statements is encouraged to keep the code clear and straightforward.

    +

    Code examples

    +

    Noncompliant code example

    +
    +String str;
    +if (!(str = cont.substring(pos1, pos2)).isEmpty()) {  // Noncompliant
    +  // do something with "str"
    +}
    +
    +

    Compliant solution

    +
    +String str = cont.substring(pos1, pos2);
    +if (!str.isEmpty()) {
    +  // do something with "str"
    +}
    +

    Resources

    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1125.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1125.html index e530943ad97..232071a945c 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1125.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1125.html @@ -1,7 +1,14 @@

    Why is this an issue?

    -

    Redundant Boolean literals should be removed from expressions to improve readability.

    -

    Noncompliant code example

    -
    +

    A boolean literal can be represented in two different ways: true or false. They can be combined with logical operators +(!, &&, ||, ==, !=) to produce logical expressions that represent truth values. However, comparing a boolean literal to a +variable or expression that evaluates to a boolean value is unnecessary and can make the code harder to read and understand. The more complex a +boolean expression is, the harder it will be for developers to understand its meaning and expected behavior, and it will favour the introduction of +new bugs.

    +

    How to tix it

    +

    Remove redundant boolean literals from expressions to improve readability and make the code more maintainable.

    +

    Code examples

    +

    Noncompliant code example

    +
     if (booleanMethod() == true) { /* ... */ }
     if (booleanMethod() == false) { /* ... */ }
     if (booleanMethod() || false) { /* ... */ }
    @@ -14,8 +21,8 @@ 

    Noncompliant code example

    booleanVariable = booleanMethod() ? exp : true; booleanVariable = booleanMethod() ? exp : false;
    -

    Compliant solution

    -
    +

    Compliant solution

    +
     if (booleanMethod()) { /* ... */ }
     if (!booleanMethod()) { /* ... */ }
     if (booleanMethod()) { /* ... */ }
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1155.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1155.html
    index f43a958cd18..0f57c82054b 100644
    --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1155.html
    +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1155.html
    @@ -1,17 +1,26 @@
     

    Why is this an issue?

    -

    Using Collection.size() to test for emptiness works, but using Collection.isEmpty() makes the code more readable and can -be more performant. The time complexity of any isEmpty() method implementation should be O(1) whereas some implementations -of size() can be O(n).

    -

    Noncompliant code example

    -
    -if (myCollection.size() == 0) {  // Noncompliant
    -  /* ... */
    +

    When you call isEmpty(), it clearly communicates the code’s intention, which is to check if the collection is empty. Using +size() == 0 for this purpose is less direct and makes the code slightly more complex.

    +

    Moreover, depending on the implementation, the size() method can have a time complexity of O(n) where n is +the number of elements in the collection. On the other hand, isEmpty() simply checks if there is at least one element in the collection, +which is a constant time operation, O(1).

    +
    +public class MyClass {
    +  public void doSomething(Collection<String> myCollection) {
    +    if (myCollection.size() == 0) { // Noncompliant
    +      doSomethingElse();
    +    }
    +  }
     }
     
    -

    Compliant solution

    -
    -if (myCollection.isEmpty()) {
    -  /* ... */
    +

    Prefer using isEmpty() to test for emptiness over size().

    +
    +public class MyClass {
    +  public void doSomething(Collection<String> myCollection) {
    +    if (myCollection.isEmpty()) {
    +      doSomethingElse();
    +    }
    +  }
     }
     
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1155.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1155.json index d0578f80372..8a96ab05033 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1155.json +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1155.json @@ -1,5 +1,5 @@ { - "title": "Collection.isEmpty() should be used to test for emptiness", + "title": "\"Collection.isEmpty()\" should be used to test for emptiness", "type": "CODE_SMELL", "code": { "impacts": { diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S117.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S117.html index b67712986e4..4669d0ef28b 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S117.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S117.html @@ -1,21 +1,26 @@ +

    Local variables and method parameters should be named consistently to communicate intent and improve maintainability. Rename your local variable or +method parameter to follow your project’s naming convention to address this issue.

    Why is this an issue?

    -

    Shared naming conventions allow teams to collaborate effectively. This rule raises an issue when a local variable or function parameter name does -not match the provided regular expression.

    -

    Noncompliant code example

    -

    With the default regular expression ^[a-z][a-zA-Z0-9]*$:

    -
    -public void doSomething(int my_param) {
    -  int LOCAL;
    -  ...
    -}
    -
    -

    Compliant solution

    -
    -public void doSomething(int myParam) {
    -  int local;
    -  ...
    -}
    -
    +

    A naming convention in software development is a set of guidelines for naming code elements like variables, functions, and classes.
    Local +variables and method parameters hold the meaning of the written code. Their names should be meaningful and follow a consistent and easily recognizable +pattern.
    Adhering to a consistent naming convention helps to make the code more readable and understandable, which makes it easier to maintain and +debug. It also ensures consistency in the code, especially when multiple developers are working on the same project.

    +

    This rule checks that local variable and method parameter names match a provided regular expression.

    +

    What is the potential impact?

    +

    Inconsistent naming of local variables and method parameters can lead to several issues in your code:

    +
      +
    • Reduced Readability: inconsistent local variable and method parameter names make the code harder to read and understand; consequently, it is + more difficult to identify the purpose of each variable, spot errors, or comprehend the logic.
    • +
    • Difficulty in Identifying Variables: local variables and method parameters that don’t adhere to a standard naming convention are challenging to + identify; thus, the coding process slows down, especially when dealing with a large codebase.
    • +
    • Increased Risk of Errors: inconsistent or unclear local variable and method parameter names lead to misunderstandings about what the variable + represents. This ambiguity leads to incorrect assumptions and, consequently, bugs in the code.
    • +
    • Collaboration Difficulties: in a team setting, inconsistent naming conventions lead to confusion and miscommunication among team members.
    • +
    • Difficulty in Code Maintenance: inconsistent naming leads to an inconsistent codebase. The code is difficult to understand, and making changes + feels like refactoring constantly, as you face different naming methods. Ultimately, it makes the codebase harder to maintain.
    • +
    +

    In summary, not adhering to a naming convention for local variables and method parameters can lead to confusion, errors, and inefficiencies, making +the code harder to read, understand, and maintain.

    Exceptions

    Loop counters are ignored by this rule.

    @@ -30,4 +35,50 @@ 

    Exceptions

    } catch (Exception e) { // Compliant }
    +

    How to fix it

    +

    First, familiarize yourself with the particular naming convention of the project in question. Then, update the name to match the convention, as +well as all usages of the name. For many IDEs, you can use built-in renaming and refactoring features to update all usages at once.

    +

    Code examples

    +

    Noncompliant code example

    +

    With the default regular expression ^[a-z][a-zA-Z0-9]*$:

    +
    +public class MyClass {
    +    public void doSomething(int myParam) {
    +      int LOCAL;    // Noncompliant
    +      // ...
    +    }
    +}
    +
    +

    Compliant solution

    +
    +public class MyClass {
    +    public void doSomething(int my_param) {
    +      int local;
    +      // ...
    +    }
    +}
    +
    +

    Resources

    +

    Documentation

    + +

    Related rules

    +
      +
    • {rule:java:S100} - Method names should comply with a naming convention
    • +
    • {rule:java:S101} - Class names should comply with a naming convention
    • +
    • {rule:java:S114} - Interface names should comply with a naming convention
    • +
    • {rule:java:S115} - Constant names should comply with a naming convention
    • +
    • {rule:java:S116} - Field names should comply with a naming convention
    • +
    • {rule:java:S118} - Abstract class names should comply with a naming convention
    • +
    • {rule:java:S119} - Type parameter names should comply with a naming convention
    • +
    • {rule:java:S120} - Package names should comply with a naming convention
    • +
    • {rule:java:S1312} - Loggers should be "private static final" and should share a naming convention
    • +
    • {rule:java:S3008} - Static non-final field names should comply with a naming convention
    • +
    • {rule:java:S3577} - Test classes should comply with a naming convention
    • +
    • {rule:java:S3578} - Test methods should comply with a naming convention
    • +
    • {rule:java:S4174} - Local constants should follow naming conventions for constants
    • +
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1176.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1176.html index 4760081b610..d188190428d 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1176.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1176.html @@ -1,27 +1,43 @@ +

    A good API documentation is a key factor in the usability and success of a software API. It ensures that developers can effectively use, maintain, +and collaborate on the API.

    Why is this an issue?

    -

    Try to imagine using the standard Java API (Collections, JDBC, IO, …​) without Javadoc. It would be a nightmare, because Javadoc is the only way to -understand of the contract of the API. Documenting an API with Javadoc increases the productivity of the developers consuming it.

    -

    On top of a main description for each member of a public API, the following Javadoc elements are required to be described:

    +

    Undocumented APIs pose significant challenges in software development for several reasons:

    +
      +
    • Lack of Clarity: developers struggling to understand how to use the API correctly. This can lead to misuse and unexpected + results.
    • +
    • Increased Development Time: developers spending extra time reading and understanding the source code, which slows down the + development process.
    • +
    • Error Prone: developers are more likely to make mistakes that lead to bugs or system crashes when the intent or the error + handling of an API is not clear.
    • +
    • Difficult Maintenance and Updates: developers may not understand the existing functionality well enough to add new features + without breaking the existing ones.
    • +
    • Poor Collaboration: collaboration, when there is lack of documentation, leads to confusion and inconsistencies.
    • +
    +

    It is recommended to document the API using JavaDoc to clarify what is the contract of the API. This is especially important for +public APIs, as they are used by other developers.

    +

    Exceptions

    +

    The following public methods and constructors are not taken into account by this rule:

    +
      +
    • Getters and setters.
    • +
    • Methods overriding another method (usually annotated with @Override).
    • +
    • Empty constructors.
    • +
    • Static constants.
    • +
    +

    How to fix it

    +

    On top of a main description for each member of a public API, the following Javadoc elements are required to be described:

    • Parameters, using @param parameterName.
    • Thrown exceptions, using @throws exceptionName.
    • Method return values, using @return.
    • Generic types, using @param <T>.
    -

    Furthermore the following guidelines should be followed:

    +

    Furthermore, the following guidelines should be followed:

    • At least 1 line of description.
    • All parameters documented with @param, and names should match.
    • -
    • All checked exceptions documented with @throws
    • -
    • @return present and documented when not void.
    • -
    • Placeholders like "TODO", "FIXME", "..." should be avoided.
    • -
    -

    The following public methods and constructors are not taken into account by this rule:

    -
      -
    • Getters and setters.
    • -
    • Methods overriding another method (usually decorated with @Override).
    • -
    • Empty constructors.
    • -
    • Static constants.
    • +
    • All checked exceptions should be documented with @throws
    • +
    • @return present and documented when method return type is not void.
    • +
    • Placeholders like "TODO", "FIXME", "…​" should be avoided.

    For the parameters of the rule, the following rules are applied:

      @@ -35,50 +51,51 @@

      Why is this an issue?

    • java.internal.* will match any member of java.internal package.
    • java.internal.** same as above, but including sub-packages.
    -

    Noncompliant code example

    -
    +

    Code examples

    +

    Noncompliant code example

    +
     /**
       * This is a Javadoc comment
       */
    -public class MyClass<T> implements Runnable {    // Noncompliant - missing '@param <T>'
    +public class MyClass<T> implements Runnable {   // Noncompliant - missing '@param <T>'
     
    -  public static final DEFAULT_STATUS = 0;    // Compliant - static constant
    +  public static final int DEFAULT_STATUS = 0;   // Compliant - static constant
       private int status;                           // Compliant - not public
     
    -  public String message;                  // Noncompliant
    +  public String message;                        // Noncompliant
     
    -  public MyClass() {                         // Noncompliant - missing documentation
    +  public MyClass() {                            // Noncompliant - missing documentation
         this.status = DEFAULT_STATUS;
       }
     
    -  public void setStatus(int status) {  // Compliant - setter
    +  public void setStatus(int status) {           // Compliant - setter
         this.status = status;
       }
     
       @Override
    -  public void run() {                          // Compliant - has @Override annotation
    +  public void run() {                           // Compliant - has @Override annotation
       }
     
    -  protected void doSomething() {    // Compliant - not public
    +  protected void doSomething() {                // Compliant - not public
       }
     
    -  public void doSomething2(int value) {  // Noncompliant
    +  public void doSomething2(int value) {         // Noncompliant
       }
     
    -  public int doSomething3(int value) {  // Noncompliant
    +  public int doSomething3(int value) {          // Noncompliant
         return value;
       }
     }
     
    -

    Compliant solution

    -
    +

    Compliant solution

    +
     /**
       * This is a Javadoc comment
       * @param <T> the parameter of the class
       */
     public class MyClass<T> implements Runnable {
     
    -  public static final DEFAULT_STATUS = 0;
    +  public static final int DEFAULT_STATUS = 0;
       private int status;
     
       /**
    @@ -109,6 +126,7 @@ 

    Compliant solution

    * @param value the value to be used */ public void doSomething(int value) { + } /** * {@inheritDoc} @@ -118,4 +136,16 @@

    Compliant solution

    } }
    +

    Resources

    +

    Documentation

    + +

    Articles & blog posts

    + diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1186.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1186.html index c0000397ee1..c0b28831a41 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1186.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1186.html @@ -1,30 +1,12 @@

    Why is this an issue?

    -

    There are several reasons for a method not to have a method body:

    +

    An empty method is generally considered bad practice and can lead to confusion, readability, and maintenance issues. Empty methods bring no +functionality and are misleading to others as they might think the method implementation fulfills a specific and identified requirement.

    +

    There are several reasons for a method not to have a body:

    • It is an unintentional omission, and should be fixed to prevent an unexpected behavior in production.
    • -
    • It is not yet, or never will be, supported. In this case an UnsupportedOperationException should be thrown.
    • +
    • It is not yet, or never will be, supported. In this case an exception should be thrown.
    • The method is an intentionally-blank override. In this case a nested comment should explain the reason for the blank override.
    -

    Noncompliant code example

    -
    -public void doSomething() {
    -}
    -
    -public void doSomethingElse() {
    -}
    -
    -

    Compliant solution

    -
    -@Override
    -public void doSomething() {
    -  // Do nothing because of X and Y.
    -}
    -
    -@Override
    -public void doSomethingElse() {
    -  throw new UnsupportedOperationException();
    -}
    -

    Exceptions

    This does not raise an issue in the following cases:

      @@ -39,4 +21,33 @@

      Exceptions

      } }
    +

    How to fix it

    +

    Code examples

    +

    Noncompliant code example

    +
    +public void shouldNotBeEmpty() {  // Noncompliant - method is empty
    +}
    +
    +public void notImplemented() {  // Noncompliant - method is empty
    +}
    +
    +@Override
    +public void emptyOnPurpose() {  // Noncompliant - method is empty
    +}
    +
    +

    Compliant solution

    +
    +public void doSomething() {
    +  doSomething();
    +}
    +
    +public void notImplemented() {
    +  throw new UnsupportedOperationException("notImplemented() cannot be performed because ...");
    +}
    +
    +@Override
    +public void emptyOnPurpose() {
    +  // comment explaining why the method is empty
    +}
    +
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S120.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S120.html index c5aeef2108d..f72684c762a 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S120.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S120.html @@ -1,12 +1,16 @@

    Why is this an issue?

    -

    Shared coding conventions allow teams to collaborate efficiently. This rule checks that all package names match a provided regular expression.

    -

    Noncompliant code example

    +

    Shared naming conventions improve readability and allow teams to collaborate efficiently. This rule checks that all package names match a provided +regular expression.

    +

    How to fix it

    +

    Rename packages with the expected naming convention

    +

    Code examples

    +

    Noncompliant code example

    With the default regular expression ^[a-z_]+(\.[a-z_][a-z0-9_]*)*$:

    -
    +
     package org.exAmple; // Noncompliant
     
    -

    Compliant solution

    -
    +

    Compliant solution

    +
     package org.example;
     
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S125.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S125.html index 86475f084cb..0d83de02ce7 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S125.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S125.html @@ -1,4 +1,5 @@

    Why is this an issue?

    -

    Programmers should not comment out code as it bloats programs and reduces readability.

    -

    Unused code should be deleted and can be retrieved from source control history if required.

    +

    Commented-out code distracts the focus from the actual executed code. It creates a noise that increases maintenance code. And because it is never +executed, it quickly becomes out of date and invalid.

    +

    Commented-out code should be deleted and can be retrieved from source control history if required.

    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1481.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1481.html index 992330e5ba0..080e96453ce 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1481.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1481.html @@ -1,15 +1,36 @@

    Why is this an issue?

    -

    If a local variable is declared but not used, it is dead code and should be removed. Doing so will improve maintainability because developers will -not wonder what the variable is used for.

    -

    Noncompliant code example

    -
    +

    An unused local variable is a variable that has been declared but is not used anywhere in the block of code where it is defined. It is dead code, +contributing to unnecessary complexity and leading to confusion when reading the code. Therefore, it should be removed from your code to maintain +clarity and efficiency.

    +

    What is the potential impact?

    +

    Having unused local variables in your code can lead to several issues:

    +
      +
    • Decreased Readability: Unused variables can make your code more difficult to read. They add extra lines and complexity, which can distract from + the main logic of the code.
    • +
    • Misunderstanding: When other developers read your code, they may wonder why a variable is declared but not used. This can lead to confusion and + misinterpretation of the code’s intent.
    • +
    • Potential for Bugs: If a variable is declared but not used, it might indicate a bug or incomplete code. For example, if you declared a variable + intending to use it in a calculation, but then forgot to do so, your program might not work as expected.
    • +
    • Maintenance Issues: Unused variables can make code maintenance more difficult. If a programmer sees an unused variable, they might think it is + a mistake and try to 'fix' the code, potentially introducing new bugs.
    • +
    • Memory Usage: Although modern compilers are smart enough to ignore unused variables, not all compilers do this. In such cases, unused variables + take up memory space, leading to inefficient use of resources.
    • +
    +

    In summary, unused local variables can make your code less readable, more confusing, and harder to maintain, and they can potentially lead to bugs +or inefficient memory use. Therefore, it is best to remove them.

    +

    How to fix it

    +

    The fix for this issue is straightforward. Once you ensure the unused variable is not part of an incomplete implementation leading to bugs, you +just need to remove it.

    +

    Code examples

    +

    Noncompliant code example

    +
     public int numberOfMinutes(int hours) {
    -  int seconds = 0;   // seconds is never used
    +  int seconds = 0;   // Noncompliant - seconds is unused
       return hours * 60;
     }
     
    -

    Compliant solution

    -
    +

    Compliant solution

    +
     public int numberOfMinutes(int hours) {
       return hours * 60;
     }
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1488.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1488.html
    index caf1f8de54a..f1bceb76327 100644
    --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1488.html
    +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1488.html
    @@ -1,26 +1,34 @@
     

    Why is this an issue?

    -

    Declaring a variable only to immediately return or throw it is a bad practice.

    -

    Some developers argue that the practice improves code readability, because it enables them to explicitly name what is being returned. However, this -variable is an internal implementation detail that is not exposed to the callers of the method. The method name should be sufficient for callers to -know exactly what will be returned.

    -

    Noncompliant code example

    -
    +

    Declaring a variable only to immediately return or throw it is considered a bad practice because it adds unnecessary complexity to the code. This +practice can make the code harder to read and understand, as it introduces an extra step that doesn’t add any value. Instead of declaring a variable +and then immediately returning or throwing it, it is generally better to return or throw the value directly. This makes the code cleaner, simpler, and +easier to understand.

    +

    How to fix it

    +

    Declaring a variable only to immediately return or throw it is considered a bad practice because it adds unnecessary complexity to the code. To fix +the issue, return or throw the value directly.

    +

    Code examples

    +

    Noncompliant code example

    +
     public long computeDurationInMilliseconds() {
    -  long duration = (((hours * 60) + minutes) * 60 + seconds ) * 1000 ;
    +  long duration = (((hours * 60) + minutes) * 60 + seconds) * 1000;
       return duration;
     }
    -
    +
    +

    Compliant solution

    +
    +public long computeDurationInMilliseconds() {
    +  return (((hours * 60) + minutes) * 60 + seconds) * 1000;
    +}
    +
    +

    Noncompliant code example

    +
     public void doSomething() {
       RuntimeException myException = new RuntimeException();
       throw myException;
     }
     
    -

    Compliant solution

    -
    -public long computeDurationInMilliseconds() {
    -  return (((hours * 60) + minutes) * 60 + seconds ) * 1000 ;
    -}
    -
    +

    Compliant solution

    +
     public void doSomething() {
       throw new RuntimeException();
     }
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1774.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1774.html
    index 341e8c3f42a..5d892f1ca5b 100644
    --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1774.html
    +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1774.html
    @@ -1,11 +1,13 @@
     

    Why is this an issue?

    -

    While the ternary operator is pleasingly compact, its use can make code more difficult to read. It should therefore be avoided in favor of the more -verbose if/else structure.

    -

    Noncompliant code example

    +

    Ternary expressions, while concise, can often lead to code that is difficult to read and understand, especially when they are nested or complex. +Prioritizing readability fosters maintainability and reduces the likelihood of bugs. Therefore, they should be removed in favor of more explicit +control structures, such as if/else statements, to improve the clarity and readability of the code.

    +

    Code examples

    +

    Noncompliant code example

    -System.out.println(i>10?"yes":"no");
    +System.out.println(i>10?"yes":"no");  // Noncompliant
     
    -

    Compliant solution

    +

    Compliant solution

     if (i > 10) {
       System.out.println("yes");
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1854.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1854.html
    index c4f96d3e1de..2b6c94f0c7d 100644
    --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1854.html
    +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1854.html
    @@ -1,23 +1,41 @@
     

    Why is this an issue?

    -

    A dead store happens when a local variable is assigned a value that is not read by any subsequent instruction. Calculating or retrieving a value -only to then overwrite it or throw it away, could indicate a serious error in the code. Even if it’s not an error, it is at best a waste of resources. -Therefore all calculated values should be used.

    -

    Noncompliant code example

    -
    -i = a + b; // Noncompliant; calculation result not used before value is overwritten
    -i = compute();
    +

    Dead stores refer to assignments made to local variables that are subsequently never used or immediately overwritten. Such assignments are +unnecessary and don’t contribute to the functionality or clarity of the code. They may even negatively impact performance. Removing them enhances code +cleanliness and readability. Even if the unnecessary operations do not do any harm in terms of the program’s correctness, they are - at best - a waste +of computing resources.

    +

    Exceptions

    +

    This rule ignores initializations to -1, 0, 1, null, true, false and +"".

    +

    How to fix it

    +

    Remove the unnecesarry assignment, then test the code to make sure that the right-hand side of a given assignment had no side effects (e.g. a +method that writes certain data to a file and returns the number of written bytes).

    +

    Code examples

    +

    Noncompliant code example

    +
    +int foo(int y) {
    +  int x = 100; // Noncompliant: dead store
    +  x = 150;     // Noncompliant: dead store
    +  x = 200;
    +  return x + y;
    +}
     
    -

    Compliant solution

    -
    -i = a + b;
    -i += compute();
    +

    Compliant solution

    +
    +int foo(int y) {
    +  int x = 200; // Compliant: no unnecessary assignment
    +  return x + y;
    +}
     
    -

    Exceptions

    -

    This rule ignores initializations to -1, 0, 1, null, true, false and "".

    Resources

    +

    Standards

    + +

    Related rules

      -
    • MITRE, CWE-563 - Assignment to Variable without Use ('Unused Variable')
    • -
    • CERT, MSC13-C. - Detect and remove unused values
    • -
    • CERT, MSC56-J. - Detect and remove superfluous code and values
    • +
    • {rule:java:S2583} - Conditionally executed code should be reachable
    • +
    • {rule:java:S2589} - Boolean expressions should not be gratuitous
    • +
    • {rule:java:S3516} - Methods returns should not be invariant
    • +
    • {rule:java:S3626} - Jump statements should not be redundant
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1854.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1854.json index 9ce31ac84f5..3dced8dacb7 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1854.json +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1854.json @@ -10,7 +10,7 @@ "status": "ready", "remediation": { "func": "Constant\/Issue", - "constantCost": "15min" + "constantCost": "1min" }, "tags": [ "cwe", diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2077.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2077.html index 4e0e13f92fa..4d281b6610f 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2077.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2077.html @@ -80,7 +80,6 @@

    See

  • MITRE, CWE-20 - Improper Input Validation
  • MITRE, CWE-943 - Improper Neutralization of Special Elements in Data Query Logic
  • CERT, IDS00-J. - Prevent SQL injection
  • -
  • SANS Top 25 - Insecure Interaction Between Components
  • Derived from FindSecBugs rules Potential SQL/JPQL Injection (JPA), Potential SQL/JDOQL Injection (JDO), Potential SQL/HQL Injection (Hibernate)
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2092.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2092.html index afff950a96d..075daeaa004 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2092.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2092.html @@ -38,6 +38,5 @@

    See

  • MITRE, CWE-311 - Missing Encryption of Sensitive Data
  • MITRE, CWE-315 - Cleartext Storage of Sensitive Information in a Cookie
  • MITRE, CWE-614 - Sensitive Cookie in HTTPS Session Without 'Secure' Attribute
  • -
  • SANS Top 25 - Porous Defenses
diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2184.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2184.html index 1148cbb39bd..cc5e5012f1c 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2184.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2184.html @@ -61,6 +61,5 @@

Resources

  • CERT, INT18-C. - Evaluate integer expressions in a larger size before comparing or assigning to that size
  • -
  • SANS Top 25 - Risky Resource Management
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2208.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2208.html index e3bd459bcbd..9fcd0027a5a 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2208.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2208.html @@ -1,24 +1,43 @@

    Why is this an issue?

    -

    Blindly importing all the classes in a package clutters the class namespace and could lead to conflicts between classes in different packages with -the same name. On the other hand, specifically listing the necessary classes avoids that problem and makes clear which versions were wanted.

    -

    Noncompliant code example

    +

    Using wildcards in imports may look cleaner as it reduces the number of lines in the import section and simplifies the code.
    On the other hand, +it makes the code harder to maintain:

    +
      +
    • It reduces code readability as developers will have a hard time knowing where names come from.
    • +
    • It could lead to conflicts between names defined locally and the ones imported.
    • +
    • It could later raise conflicts on dependency upgrade or Java version migration, as a wildcard import that works today might be broken tomorrow. +
    • +
    +

    That is why it is better to import only the specific classes or modules you need.

    +

    Exceptions

    +

    Static imports are ignored by this rule. For example:

    +import static java.lang.Math.*;
    +
    +

    will not raise an issue;

    +

    How to fix it

    +

    Depending on your IDE you can solve this issue almost automatically:
    Look for Organize/Optimize imports +actions. These actions can also often be applied automatically on save.
    Note: To make this work properly, you must adjust IDE settings to +use a very high allowed class count usage before using wildcards.

    +

    Resolving this issue manually will require a step-by-step approach:

    +
      +
    1. Remove one wildcard import and note down compilation failures.
    2. +
    3. For each missing class, import it back with the package prefix.
    4. +
    5. When the code compiles again, proceed with the next wildcard import.
    6. +
    +

    Code examples

    +

    Noncompliant code example

    +
     import java.sql.*; // Noncompliant
     import java.util.*; // Noncompliant
     
     private Date date; // Date class exists in java.sql and java.util. Which one is this?
     
    -

    Compliant solution

    -
    +

    Compliant solution

    +
     import java.sql.Date;
     import java.util.List;
     import java.util.ArrayList;
     
     private Date date;
     
    -

    Exceptions

    -

    Static imports are ignored by this rule. E.G.

    -
    -import static java.lang.Math.*;
    -
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2230.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2230.html index 7436f978c3b..7e9a6abe8a8 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2230.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2230.html @@ -1,33 +1,39 @@

    Why is this an issue?

    -

    Marking a non-public method @Transactional is both useless and misleading because Spring does not recognize -non-public methods, and so makes no provision for their proper invocation. Nor does Spring make provision for the methods invoked by the -method it called.

    -

    Therefore marking a private method, for instance, @Transactional can only result in a runtime error or exception if the -method is annotated as @Transactional.

    +

    Marking a non-public method @Async or @Transactional is misleading because Spring does not recognize non-public methods, +and so makes no provision for their proper invocation. Nor does Spring make provision for the methods invoked by the method it called.

    +

    Therefore marking a private method, for instance, @Transactional can only result in a runtime error or exception if the method is annotated as +@Transactional.

    How to fix it

    -

    Make the method public or remove the @Transactional annotation.

    +

    Declare the method public. Note that this action alone does not resolve the issue of direct instance calls from within the same class +(see rule {rule:java:S6809}), but it is a required precondition to fix it.

    Code examples

    Noncompliant code example

    -@Transactional  // Noncompliant
    -void doTheThing(ArgClass arg) {
    -  // ...
    -}
    -
    -@Transactional  // Noncompliant
    -private void doTheOtherThing(ArgClass arg) {
    -  // ...
    +@Async
    +private Future<String> asyncMethodWithReturnType() { // Noncompliant, no proxy generated and
    +    return "Hellow, world!";                         // can only be invoked from same class
     }
     

    Compliant solution

    -@Transactional
    -public void doTheThing(ArgClass arg) {
    -  // ...
    -}
    -
    -private void doTheOtherThing(ArgClass arg) {
    -  // ...
    +@Async
    +public Future<String> asyncMethodWithReturnType() { // Compliant
    +    return "Hellow, world!";
     }
     
    +

    Resources

    +

    Documentation

    + +

    Articles & blog posts

    + diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2230.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2230.json index cbbd266ce20..60ca4fb5b96 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2230.json +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2230.json @@ -1,20 +1,21 @@ { - "title": "Non-public methods should not be \"@Transactional\"", + "title": "Methods with Spring proxying annotations should be public", "type": "BUG", - "code": { - "impacts": { - "RELIABILITY": "MEDIUM" - }, - "attribute": "CONVENTIONAL" - }, "status": "ready", "remediation": { "func": "Constant\/Issue", - "constantCost": "20min" + "constantCost": "5min" }, "tags": [ "spring" ], + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM" + }, + "attribute": "CONVENTIONAL" + }, "defaultSeverity": "Major", "ruleSpecification": "RSPEC-2230", "sqKey": "S2230", diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2254.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2254.html index 75976ca3194..a664e189805 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2254.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2254.html @@ -49,6 +49,5 @@

    Standards

  • OWASP Top 10 2017 Category A2 - Broken Authentication
  • MITRE, CWE-807 - Reliance on Untrusted Inputs in a Security Decision
  • -
  • SANS Top 25 - Porous Defenses
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2257.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2257.html index fdd1ded420b..9a72989626c 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2257.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2257.html @@ -21,7 +21,6 @@

    See

  • OWASP Top 10 2017 Category A3 - Sensitive Data Exposure
  • MITRE, CWE-327 - Use of a Broken or Risky Cryptographic Algorithm
  • -
  • SANS Top 25 - Porous Defenses
  • Derived from FindSecBugs rule MessageDigest is Custom
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2589.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2589.html index c7747e49204..39ad6000814 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2589.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2589.html @@ -1,42 +1,66 @@ +

    Gratuitous boolean expressions are conditions that do not change the evaluation of a program. This issue can indicate logical errors and affect the +correctness of an application, as well as its maintainability.

    Why is this an issue?

    -

    If a boolean expression doesn’t change the evaluation of the condition, then it is entirely unnecessary, and can be removed. If it is gratuitous -because it does not match the programmer’s intent, then it’s a bug and the expression should be fixed.

    -

    Noncompliant code example

    -
    -a = true;
    -if (a) { // Noncompliant
    -  doSomething();
    -}
    +

    Control flow constructs like if-statements allow the programmer to direct the flow of a program depending on a boolean expression. +However, if the condition is always true or always false, only one of the branches will ever be executed. In that case, the control flow construct and +the condition no longer serve a purpose; they become gratuitous.

    +

    What is the potential impact?

    +

    The presence of gratuitous conditions can indicate a logical error. For example, the programmer intended to have the program branch into +different paths but made a mistake when formulating the branching condition. In this case, this issue might result in a bug and thus affect the +reliability of the application. For instance, it might lead to the computation of incorrect results.

    +

    Additionally, gratuitous conditions and control flow constructs introduce unnecessary complexity. The source code becomes harder to understand, and +thus, the application becomes more difficult to maintain.

    +

    How to fix it

    +

    Gratuitous boolean expressions are suspicious and should be carefully removed from the code.

    +

    First, the boolean expression in question should be closely inspected for logical errors. If a mistake was made, it can be corrected so the +condition is no longer gratuitous.

    +

    If it becomes apparent that the condition is actually unnecessary, it can be removed. The associated control flow construct (e.g., the +if-statement containing the condition) will be adapted or even removed, leaving only the necessary branches.

    +

    Code examples

    +

    Noncompliant code example

    +
    +public class MyClass {
    +    public void doThings(boolean b, boolean c) {
    +        a = true;
    +        if (a) {                // Noncompliant
    +          doSomething();
    +        }
     
    -if (b && a) { // Noncompliant; "a" is always "true"
    -  doSomething();
    -}
    +        if (b && a) {           // Noncompliant; "a" is always "true"
    +          doSomething();
    +        }
     
    -if (c || !a) { // Noncompliant; "!a" is always "false"
    -  doSomething();
    -}
    +        if (c || !a) {          // Noncompliant; "!a" is always "false"
    +          doSomething();
    +        }
     
    -if (c || (!c && b)) { // Noncompliant; c || (!c && b) is equal to c || b
    -  doSomething();
    +        if (c || (!c && b)) {   // Noncompliant; c || (!c && b) is equal to c || b
    +          doSomething();
    +        }
    +    }
     }
     
    -

    Compliant solution

    -
    -a = true;
    -if (foo(a)) {
    -  doSomething();
    -}
    +

    Compliant solution

    +
    +public class MyClass {
    +    public void doThings(boolean b, boolean c) {
    +        a = true;
    +        if (foo(a)) {
    +          doSomething();
    +        }
     
    -if (b) {
    -  doSomething();
    -}
    +        if (b) {
    +          doSomething();
    +        }
     
    -if (c) {
    -  doSomething();
    -}
    +        if (c) {
    +          doSomething();
    +        }
     
    -if (c || b) {
    -  doSomething();
    +        if (c || b) {
    +          doSomething();
    +        }
    +    }
     }
     

    Resources

    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2612.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2612.html index a4ec70420e7..e6f33c4f3fc 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2612.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2612.html @@ -73,6 +73,5 @@

    See

    Create files with appropriate access permissions
  • CERT, FIO06-C. - Create files with appropriate access permissions
  • -
  • SANS Top 25 - Porous Defenses
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2647.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2647.html index 5bf8e31a736..b9e350e729f 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2647.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S2647.html @@ -104,6 +104,5 @@

    Standards

  • OWASP Web Service Security Cheat Sheet
  • MITRE, CWE-522 - Insufficiently Protected Credentials
  • -
  • SANS Top 25 - Porous Defenses
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3252.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3252.html index c941a87509b..1b11e7fa7e0 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3252.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3252.html @@ -1,8 +1,10 @@

    Why is this an issue?

    -

    In the interest of code clarity, static members of a base class should never be accessed using a derived type’s name. -Doing so is confusing and could create the illusion that two different static members exist.

    +

    In object-oriented programming, inappropriately accessing static members of a base class via derived types is considered a code smell.

    +

    Static members are associated with the class itself, not with any specific instance of the class or its children classes. Accessing through the +wrong type suggests a misunderstanding of the ownership and role of this member. This can make the maintenance of the code more complicated.

    +

    Therefore, the access should be done directly through the base class to maintain clarity and avoid potential misunderstandings.

    Noncompliant code example

    -
    +
     class Parent {
       public static int counter;
     }
    @@ -14,7 +16,7 @@ 

    Noncompliant code example

    }

    Compliant solution

    -
    +
     class Parent {
       public static int counter;
     }
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3330.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3330.html
    index e8d3047ff78..010fb4a62f5 100644
    --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3330.html
    +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3330.html
    @@ -38,7 +38,6 @@ 

    See

  • OWASP Top 10 2017 Category A7 - Cross-Site Scripting (XSS)
  • MITRE, CWE-1004 - Sensitive Cookie Without 'HttpOnly' Flag
  • -
  • SANS Top 25 - Insecure Interaction Between Components
  • Derived from FindSecBugs rule HTTPONLY_COOKIE
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3415.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3415.html index 4f3971d9e79..c7546fd492a 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3415.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3415.html @@ -1,22 +1,28 @@

    Why is this an issue?

    The standard assertions library methods such as org.junit.Assert.assertEquals, and org.junit.Assert.assertSame expect the -first argument to be the expected value and the second argument to be the actual value. For AssertJ, it’s the other way around, the argument of -org.assertj.core.api.Assertions.assertThat is the actual value, and the subsequent calls contain the expected values. Swap them, and your -test will still have the same outcome (succeed/fail when it should) but the error messages will be confusing.

    +first argument to be the expected value and the second argument to be the actual value. For AssertJ instead, the argument of +org.assertj.core.api.Assertions.assertThat is the actual value, and the subsequent calls contain the expected values.

    +

    What is the potential impact?

    +

    Having the expected value and the actual value in the wrong order will not alter the outcome of tests, (succeed/fail when it should) but the error +messages will contain misleading information.

    This rule raises an issue when the actual argument to an assertions library method is a hard-coded value and the expected argument is not.

    +

    How to fix it

    +

    You should provide the assertion methods with a hard-coded value as the expected value, while the actual value of the assertion should derive from +the portion of code that you want to test.

    Supported frameworks:

    -

    Noncompliant code example

    -
    +

    Code examples

    +

    Noncompliant code example

    +
     org.junit.Assert.assertEquals(runner.exitCode(), 0, "Unexpected exit code");  // Noncompliant; Yields error message like: Expected:<-1>. Actual:<0>.
     org.assertj.core.api.Assertions.assertThat(0).isEqualTo(runner.exitCode()); // Noncompliant
     
    -

    Compliant solution

    -
    +

    Compliant solution

    +
     org.junit.Assert.assertEquals(0, runner.exitCode(), "Unexpected exit code");
     org.assertj.core.api.Assertions.assertThat(runner.exitCode()).isEqualTo(0);
     
    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3457.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3457.html index 3b6078840ae..a1c8c0847c1 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3457.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3457.html @@ -1,68 +1,56 @@

    Why is this an issue?

    +

    A printf--style format string is a string that contains placeholders, usually represented by special characters such as "%s" or "{}", +depending on the technology in use. These placeholders are replaced by values when the string is printed or logged.

    Because printf-style format strings are interpreted at runtime, rather than validated by the compiler, they can contain errors that -result in the wrong strings being created. This rule statically validates the correlation of printf-style format strings to their -arguments when calling the format(...) methods of java.util.Formatter, java.lang.String, -java.io.PrintStream, MessageFormat, and java.io.PrintWriter classes and the printf(...) methods of -java.io.PrintStream or java.io.PrintWriter classes.

    -

    Noncompliant code example

    -
    -String.format("First {0} and then {1}", "foo", "bar");  //Noncompliant. Looks like there is a confusion with the use of {{java.text.MessageFormat}}, parameters "foo" and "bar" will be simply ignored here
    -String.format("Display %3$d and then %d", 1, 2, 3);   //Noncompliant; the second argument '2' is unused
    -String.format("Too many arguments %d and %d", 1, 2, 3);  //Noncompliant; the third argument '3' is unused
    -String.format("First Line\n");   //Noncompliant; %n should be used in place of \n to produce the platform-specific line separator
    -String.format("Is myObject null ? %b", myObject);   //Noncompliant; when a non-boolean argument is formatted with %b, it prints true for any nonnull value, and false for null. Even if intended, this is misleading. It's better to directly inject the boolean value (myObject == null in this case)
    -String.format("value is " + value); // Noncompliant
    -String s = String.format("string without arguments"); // Noncompliant
    -
    -MessageFormat.format("Result '{0}'.", value); // Noncompliant; String contains no format specifiers. (quote are discarding format specifiers)
    -MessageFormat.format("Result {0}.", value, value);  // Noncompliant; 2nd argument is not used
    -MessageFormat.format("Result {0}.", myObject.toString()); // Noncompliant; no need to call toString() on objects
    -
    -java.util.Logger logger;
    -logger.log(java.util.logging.Level.SEVERE, "Result {0}.", myObject.toString()); // Noncompliant; no need to call toString() on objects
    -logger.log(java.util.logging.Level.SEVERE, "Result.", new Exception()); // compliant, parameter is an exception
    -logger.log(java.util.logging.Level.SEVERE, "Result '{0}'", 14); // Noncompliant - String contains no format specifiers.
    -logger.log(java.util.logging.Level.SEVERE, "Result " + param, exception); // Noncompliant; Lambda should be used to differ string concatenation.
    +result in the wrong strings being created.

    +

    This rule checks whether every format string specifier can be correctly matched with one of the additional arguments when calling the following +methods:

    + +

    How to fix it

    +

    A printf--style format string is a string that contains placeholders, which are replaced by values when the string is printed or +logged. Mismatch in the format specifiers and the arguments provided can lead to incorrect strings being created.

    +

    To avoid issues, a developer should ensure that the provided arguments match format specifiers.

    +

    Code examples

    +

    Noncompliant code example

    +
    +String.format("Too many arguments %d and %d", 1, 2, 3); // Noncompliant; the third argument '3' is unused
    +String.format("First {0} and then {1}", "foo", "bar");  //Noncompliant. It appears there is confusion with the use of "java.text.MessageFormat"; parameters "foo" and "bar" will be ignored here
     
     org.slf4j.Logger slf4jLog;
    -org.slf4j.Marker marker;
    -
    -slf4jLog.debug(marker, "message {}");
    -slf4jLog.debug(marker, "message", 1); // Noncompliant - String contains no format specifiers.
    -
    -org.apache.logging.log4j.Logger log4jLog;
    -log4jLog.debug("message", 1); // Noncompliant - String contains no format specifiers.
    +slf4jLog.debug("The number: ", 1); // Noncompliant - String contains no format specifiers.
     
    -

    Compliant solution

    -
    +

    Compliant solution

    +
    +String.format("Too many arguments %d and %d", 1, 2);
     String.format("First %s and then %s", "foo", "bar");
    -String.format("Display %2$d and then %d", 1, 3);
    -String.format("Too many arguments %d %d", 1, 2);
    -String.format("First Line%n");
    -String.format("Is myObject null ? %b", myObject == null);
    -String.format("value is %d", value);
    -String s = "string without arguments";
    -
    -MessageFormat.format("Result {0}.", value);
    -MessageFormat.format("Result '{0}'  =  {0}", value);
    -MessageFormat.format("Result {0}.", myObject);
    -
    -java.util.Logger logger;
    -logger.log(java.util.logging.Level.SEVERE, "Result {0}.", myObject);
    -logger.log(java.util.logging.Level.SEVERE, "Result {0}'", 14);
    -logger.log(java.util.logging.Level.SEVERE, exception, () -> "Result " + param);
     
     org.slf4j.Logger slf4jLog;
    -org.slf4j.Marker marker;
    -
    -slf4jLog.debug(marker, "message {}");
    -slf4jLog.debug(marker, "message {}", 1);
    -
    -org.apache.logging.log4j.Logger log4jLog;
    -log4jLog.debug("message {}", 1);
    +slf4jLog.debug("The number: {}", 1);
     

    Resources

    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3457.json b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3457.json index 3a47617c1fa..4a35edbd5f2 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3457.json +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3457.json @@ -1,5 +1,5 @@ { - "title": "Printf-style format strings should be used correctly", + "title": "Format strings should be used correctly", "type": "CODE_SMELL", "code": { "impacts": { @@ -10,7 +10,7 @@ "status": "ready", "remediation": { "func": "Constant\/Issue", - "constantCost": "10min" + "constantCost": "1min" }, "tags": [ "cert", diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3752.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3752.html index 01c892c8d87..97cae1b3c99 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3752.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S3752.html @@ -45,7 +45,6 @@

    See

    Broken Access Control
  • MITRE, CWE-352 - Cross-Site Request Forgery (CSRF)
  • OWASP: Cross-Site Request Forgery
  • -
  • SANS Top 25 - Insecure Interaction Between Components
  • Spring Security Official Documentation: Use proper HTTP verbs (CSRF protection)
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4423.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4423.html index 956d7b44b4e..f0e7d1dfc97 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4423.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4423.html @@ -132,7 +132,6 @@

    Standards

  • OWASP Top 10 2017 Category A6 - Security Misconfiguration
  • MITRE, CWE-327 - Use of a Broken or Risky Cryptographic Algorithm
  • -
  • SANS Top 25 - Porous Defenses
  • Mobile AppSec Verification Standard - Cryptography Requirements
  • OWASP Mobile Top 10 2016 Category M5 - diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4502.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4502.html index fdf5774e822..aa6e4cb2bdd 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4502.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4502.html @@ -57,6 +57,5 @@

    See

  • OWASP Top 10 2017 Category A6 - Security Misconfiguration
  • OWASP: Cross-Site Request Forgery
  • -
  • SANS Top 25 - Insecure Interaction Between Components
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4790.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4790.html index a957473105b..ea7b4b38017 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4790.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4790.html @@ -35,6 +35,5 @@

    See

  • OWASP Mobile Top 10 2016 Category M5 - Insufficient Cryptography
  • MITRE, CWE-1240 - Use of a Risky Cryptographic Primitive
  • -
  • SANS Top 25 - Porous Defenses
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4792.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4792.html index ab6ab8071a0..faf59a4aeec 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4792.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S4792.html @@ -141,6 +141,5 @@

    See

    Insufficient Logging & Monitoring
  • MITRE, CWE-117 - Improper Output Neutralization for Logs
  • MITRE, CWE-532 - Information Exposure Through Log Files
  • -
  • SANS Top 25 - Porous Defenses
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5122.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5122.html index 722ff4d0c49..5080bbe6374 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5122.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5122.html @@ -154,6 +154,5 @@

    See

    Cheat Sheet - Cross Origin Resource Sharing
  • MITRE, CWE-346 - Origin Validation Error
  • MITRE, CWE-942 - Overly Permissive Cross-domain Whitelist
  • -
  • SANS Top 25 - Porous Defenses
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5322.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5322.html index 9feb2c82003..11f0c7c88f1 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5322.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5322.html @@ -79,7 +79,6 @@

    See

    Platform Usage
  • MITRE, CWE-925 - Improper Verification of Intent by Broadcast Receiver
  • MITRE, CWE-926 - Improper Export of Android Application Components
  • -
  • SANS Top 25 - Insecure Interaction Between Components
  • Android documentation - Broadcast Overview - Security considerations and best practices
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5324.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5324.html index d533374d54f..68df0d6a1c4 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5324.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5324.html @@ -46,14 +46,12 @@

    Compliant Solution

    See

    diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5344.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5344.html index 9477b9bca1b..59cee25220e 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5344.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5344.html @@ -68,6 +68,5 @@

    Standards

    Exposure
  • MITRE, CWE-256 - Plaintext Storage of a Password
  • MITRE, CWE-916 - Use of Password Hash With Insufficient Computational Effort
  • -
  • SANS Top 25 - Porous Defenses
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5542.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5542.html index b733b9f1e43..87b087a440f 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5542.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5542.html @@ -123,7 +123,6 @@

    Standards

  • OWASP Top 10 2017 Category A6 - Security Misconfiguration
  • MITRE, CWE-327 - Use of a Broken or Risky Cryptographic Algorithm
  • -
  • SANS Top 25 - Porous Defenses
  • Mobile AppSec Verification Standard - Cryptography Requirements
  • OWASP Mobile Top 10 2016 Category M5 - diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5547.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5547.html index 429f5662275..ae6d20cbd9e 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5547.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5547.html @@ -66,6 +66,5 @@

    Standards

  • OWASP Top 10 2017 Category A3 - Sensitive Data Exposure
  • MITRE, CWE-327 - Use of a Broken or Risky Cryptographic Algorithm
  • -
  • SANS Top 25 - Porous Defenses
  • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5693.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5693.html index 18d366ae025..0e38fbf500b 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5693.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5693.html @@ -1,5 +1,5 @@

    Rejecting requests with significant content length is a good practice to control the network traffic intensity and thus resource consumption in -order to prevents DoS attacks.

    +order to prevent DoS attacks.

    Ask Yourself Whether

    • size limits are not defined for the different resources of the web application.
    • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5738.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5738.html index c9bc5e8e9ba..54ce663e6f6 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5738.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S5738.html @@ -1,15 +1,22 @@

      Why is this an issue?

      -

      Java 9 introduced a flag for the @Deprecated annotation, which allows to explicitly say if the deprecated code is planned to be -removed at some point or not. This is done using forRemoval=true as annotation parameter. The javadoc of the annotation explicitly -mention the following:

      +

      With the introduction of Java 9, the standard annotation class java.lang.Deprecated has been updated with new parameters. Notably, a +boolean parameter forRemoval has been added to clearly signify whether the deprecated code is intended to be removed in the future. This +is indicated with forRemoval=true. The javadoc of the annotation explicitly mentions the following:

      -

      If true, it means that this API element is earmarked for removal in a future release.

      -

      If false, the API element is deprecated, but there is currently no intention to remove it in a future release.

      +

      This annotation type has a boolean-valued element forRemoval. A value of true indicates intent to remove the annotated + program element in a future version. A value of false indicates that use of the annotated program element is discouraged, but at the + time the program element was annotated, there was no specific intent to remove it.

      -

      While usually deprecated classes, interfaces, and their deprecated members should be avoided rather than used, inherited or extended, those already -marked for removal are much more sensitive to causing trouble in your code soon. Consequently, any usage of such deprecated code should be avoided or -removed.

      -

      Noncompliant code example

      +

      While it is generally recommended for developers to steer clear of using deprecated classes, interfaces, and their deprecated members, those +already marked for removal will surely block you from upgrading your dependency. Usage of deprecated code should be avoided or eliminated as soon as +possible to prevent accumulation and allow a smooth upgrade of dependencies.

      +

      The deprecated code is usually no longer maintained, can contain some bugs or vulnerabilities, and usually indicates that there is a better way to +do the same thing. Removing it can even lead to significant improvement of your software.

      +

      How to fix it

      +

      Usage of deprecated classes, interfaces, and their methods explicitly marked for removal is discouraged. A developer should either migrate to +alternative methods or refactor the code to avoid the deprecated ones.

      +

      Code examples

      +

      Noncompliant code example

       /**
        * @deprecated As of release 1.3, replaced by {@link #Fee}. Will be dropped with release 1.4.
      diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6288.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6288.html
      index c661c86a8c3..ebce11313c6 100644
      --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6288.html
      +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6288.html
      @@ -41,9 +41,9 @@ 

      Compliant Solution

      See

      How does this work?

      Using Android’s builtin key storage options

      -

      The Android Keystore system allows apps to store encryption keys in a +

      The Android Keystore system allows apps to store encryption keys in a container that is protected on a system level. Additionally, it can restrict when and how the keys are used. For example, it allows the app to require user authentication (for example using a fingerprint) before the key is made available. This is the recommended way to store cryptographic keys on Android.

      @@ -66,7 +66,7 @@

      Compliant solution

    How does this work?

    Using Android’s builtin key storage options

    -

    The Android Keystore system allows apps to store encryption keys in a +

    The Android Keystore system allows apps to store encryption keys in a container that is protected on a system level. Additionally, it can restrict when and how the keys are used. For example, it allows the app to require user authentication (for example using a fingerprint) before the key is made available. This is the recommended way to store cryptographic keys on Android.

    @@ -82,8 +82,8 @@

    Avoid storing sensitive data on user devices

    Resources

    Documentation

      -
    • Android Documentation - Android Keystore system
    • -
    • Android Documentation - Security tips - User data
    • +
    • Android Documentation - Android Keystore system
    • +
    • Android Documentation - Security tips - User data
    • OWASP Mobile Application Security Testing Guide - Data Storage on Android
    • diff --git a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6353.html b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6353.html index 20a56359a8e..64b67bdadf1 100644 --- a/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6353.html +++ b/java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S6353.html @@ -1,7 +1,14 @@

      Why is this an issue?

      -

      With regular expressions syntax, it’s possible to express the same thing in many ways. For example, to match a two-digit number, one could write -[0-9]{2,2} or \d{2}. Latter is not only shorter in terms of expression length, but also easier to read and thus to maintain. -This rule recommends to replace some bulky quantifiers and character classes with more concise equivalents:

      +

      A regular expression is a sequence of characters that specifies a match pattern in text. Among the most important concepts are:

      +
        +
      • Character classes: defines a set of characters, any one of which can occur in an input string for a match to succeed.
      • +
      • Quantifiers: used to specify how many instances of a character, group, or character class must be present in the input for a match.
      • +
      • Wildcard (.): matches all characters except line terminators (also matches them if the s flag is set).
      • +
      +

      Many of these features include shortcuts of widely used expressions, so there is more than one way to construct a regular expression to achieve the +same results. For example, to match a two-digit number, one could write [0-9]{2,2} or \d{2}. The latter is not only shorter +but easier to read and thus to maintain.

      +

      This rule recommends replacing some quantifiers and character classes with more concise equivalents:

      • \d for [0-9] and \D for [^0-9]
      • \w for [A-Za-z0-9_] and \W for [^A-Za-z0-9_]
      • @@ -10,16 +17,15 @@

        Why is this an issue?

      • x? for x{0,1}, x* for x{0,}, x+ for x{1,}, x{N} for x{N,N}
      -

      Noncompliant code example

      -
      -"[0-9]" // Noncompliant - same as "\\d"
      -"[^0-9]" // Noncompliant - same as "\\D"
      +
      +"[0-9]"        // Noncompliant - same as "\\d"
      +"[^0-9]"       // Noncompliant - same as "\\D"
       "[A-Za-z0-9_]" // Noncompliant - same as "\\w"
      -"[\\w\\W]" // Noncompliant - same as "."
      -"a{0,}" // Noncompliant - same as "a*"
      +"[\\w\\W]"     // Noncompliant - same as "."
      +"a{0,}"        // Noncompliant - same as "a*"
       
      -

      Compliant solution

      -
      +

      Use the more concise version to make the regex expression more readable.

      +
       "\\d"
       "\\D"
       "\\w"
      diff --git a/sonarpedia.json b/sonarpedia.json
      index 5180ac26a03..09651911a16 100644
      --- a/sonarpedia.json
      +++ b/sonarpedia.json
      @@ -3,7 +3,7 @@
         "languages": [
           "JAVA"
         ],
      -  "latest-update": "2023-10-13T09:53:32.596491Z",
      +  "latest-update": "2023-10-20T09:15:09.502876096Z",
         "options": {
           "no-language-in-filenames": true,
           "preserve-filenames": false