Skip to content

Commit b4ff76a

Browse files
authored
SONARJAVA-4661 Update Rules Metadata (#4496)
A batch of updates from the LaYC update
1 parent f106053 commit b4ff76a

58 files changed

Lines changed: 734 additions & 444 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S106.html

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
<h2>Why is this an issue?</h2>
2-
<p>When logging a message, the following requirements should be fulfilled:</p>
2+
<p>In software development, logs serve as a record of events within an application, providing crucial insights for debugging. When logging, it is
3+
essential to ensure that the logs are:</p>
34
<ul>
4-
<li> The user can easily record logged data and retrieve the logs. </li>
5-
<li> The format of all logged messages is uniform and provides context about the message creator. This improves the readability of the logs. </li>
6-
<li> Sensitive data is logged securely. </li>
5+
<li> easily accessible </li>
6+
<li> uniformly formatted for readability </li>
7+
<li> properly recorded </li>
8+
<li> securely logged when dealing with sensitive data </li>
79
</ul>
8-
<p>Simply printing messages to <code>System.out</code> or <code>System.err</code> does not fulfill these needs. A dedicated logger should be used
9-
instead.</p>
10-
<h3>Noncompliant code example</h3>
10+
<p>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
11+
a dedicated logger is highly recommended.</p>
12+
<h3>Code examples</h3>
13+
<p>The following noncompliant code:</p>
1114
<pre data-diff-id="1" data-diff-type="noncompliant">
12-
class PrintMessage {
13-
public void print() {
15+
class MyClass {
16+
public void doSomething() {
1417
System.out.println("My Message"); // Noncompliant, output directly to System.out without a logger
1518
}
1619
}
1720
</pre>
18-
<h3>Compliant solution</h3>
21+
<p>Could be replaced by:</p>
1922
<pre data-diff-id="1" data-diff-type="compliant">
2023
import java.util.logging.Logger;
2124

22-
class PrintMessage {
25+
class MyClass {
2326

2427
Logger logger = Logger.getLogger(getClass().getName());
2528

26-
public void print() {
29+
public void doSomething() {
30+
// ...
2731
logger.info("My Message"); // Compliant, output via logger
32+
// ...
2833
}
2934
}
3035
</pre>

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.html

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
<h2>Why is this an issue?</h2>
2-
<p>Merging collapsible <code>if</code> statements increases the code’s readability.</p>
3-
<h3>Noncompliant code example</h3>
2+
<p>Nested code - blocks of code inside blocks of code - is eventually necessary, but increases complexity. This is why keeping the code as flat as
3+
possible, by avoiding unnecessary nesting, is considered a good practice.</p>
4+
<p>Merging <code>if</code> statements when possible will decrease the nesting of the code and improve its readability.</p>
5+
<p>Code like</p>
6+
<pre>
7+
if (condition1) {
8+
if (condition2) { // Noncompliant
9+
/* ... */
10+
}
11+
}
12+
</pre>
13+
<p>Will be more readable as</p>
14+
<pre>
15+
if (condition1 &amp;&amp; condition2) { // Compliant
16+
/* ... */
17+
}
18+
</pre>
19+
<h2>How to fix it</h2>
20+
<p>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
21+
better approach to fix readability.</p>
22+
<h3>Code examples</h3>
23+
<h4>Noncompliant code example</h4>
424
<pre>
525
if (file != null) {
6-
if (file.isFile() || file.isDirectory()) {
26+
if (file.isFile() || file.isDirectory()) { // Noncompliant
727
/* ... */
828
}
929
}
1030
</pre>
11-
<h3>Compliant solution</h3>
31+
<h4>Compliant solution</h4>
1232
<pre>
13-
if (file != null &amp;&amp; isFileOrDirectory(file)) {
33+
if (file != null &amp;&amp; isFileOrDirectory(file)) { // Compliant
1434
/* ... */
1535
}
1636

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1066.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"title": "Collapsible \"if\" statements should be merged",
2+
"title": "Mergeable \"if\" statements should be combined",
33
"type": "CODE_SMELL",
44
"code": {
55
"impacts": {
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
<h2>Why is this an issue?</h2>
2-
<p>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
3-
not exist on a specific test environment, a specified Internet URL may not be available when executing the tests, production environment filesystems
4-
usually differ from the development environment, …​etc. For all those reasons, a URI should never be hard coded. Instead, it should be replaced by
5-
customizable parameter.</p>
6-
<p>Further even if the elements of a URI are obtained dynamically, portability can still be limited if the path-delimiters are hard-coded.</p>
7-
<p>This rule raises an issue when URI’s or path delimiters are hard coded.</p>
8-
<h3>Noncompliant code example</h3>
9-
<pre>
2+
<p>Hard-coding a URI makes it difficult to test a program for a variety of reasons:</p>
3+
<ul>
4+
<li> path literals are not always portable across operating systems </li>
5+
<li> a given absolute path may not exist in a specific test environment </li>
6+
<li> a specified Internet URL may not be available when executing the tests </li>
7+
<li> production environment filesystems usually differ from the development environment </li>
8+
</ul>
9+
<p>In addition, hard-coded URIs can contain sensitive information, like IP addresses, and they should not be stored in the code.</p>
10+
<p>For all those reasons, a URI should never be hard coded. Instead, it should be replaced by a customizable parameter.</p>
11+
<p>Further, even if the elements of a URI are obtained dynamically, portability can still be limited if the path delimiters are hard-coded.</p>
12+
<p>This rule raises an issue when URIs or path delimiters are hard-coded.</p>
13+
<h2>How to fix it</h2>
14+
<h3>Code examples</h3>
15+
<h4>Noncompliant code example</h4>
16+
<pre data-diff-id="1" data-diff-type="noncompliant">
1017
public class Foo {
1118
public Collection&lt;User&gt; listUsers() {
1219
File userList = new File("/home/mylogin/Dev/users.txt"); // Noncompliant
@@ -15,8 +22,8 @@ <h3>Noncompliant code example</h3>
1522
}
1623
}
1724
</pre>
18-
<h3>Compliant solution</h3>
19-
<pre>
25+
<h4>Compliant solution</h4>
26+
<pre data-diff-id="1" data-diff-type="compliant">
2027
public class Foo {
2128
// Configuration is a class that returns customizable properties: it can be mocked to be injected during tests.
2229
private Configuration config;
@@ -33,8 +40,4 @@ <h3>Compliant solution</h3>
3340
}
3441
}
3542
</pre>
36-
<h2>Resources</h2>
37-
<ul>
38-
<li> <a href="https://wiki.sei.cmu.edu/confluence/x/OjdGBQ">CERT, MSC03-J.</a> - Never hard code sensitive information </li>
39-
</ul>
4043

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S109.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
readable and maintainable.</p>
33
<h2>Why is this an issue?</h2>
44
<p>Magic numbers make the code more complex to understand as it requires the reader to have knowledge about the global context to understand the
5-
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
6-
context faded away. -1, 0 and 1 are not considered magic numbers.</p>
5+
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
6+
away. -1, 0, and 1 are not considered magic numbers.</p>
77
<h3>Exceptions</h3>
88
<p>This rule ignores <code>hashCode</code> methods.</p>
99
<h2>How to fix it</h2>
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<h2>Why is this an issue?</h2>
2-
<p>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
3-
redundant pairs of parentheses could be misleading, and should be removed.</p>
4-
<h3>Noncompliant code example</h3>
2+
<p>Parentheses can disambiguate the order of operations in complex expressions and make the code easier to understand.</p>
53
<pre>
6-
int x = (y / 2 + 1); //Compliant even if the parenthesis are ignored by the compiler
4+
a = (b * c) + (d * e); // Compliant: the intent is clear.
5+
</pre>
6+
<p>Redundant parentheses are parenthesis that do not change the behavior of the code, and do not clarify the intent. They can mislead and complexify
7+
the code. They should be removed.</p>
8+
<h3>Noncompliant code example</h3>
9+
<pre data-diff-id="1" data-diff-type="noncompliant">
10+
int x = ((y / 2 + 1)); // Noncompliant
711

8-
if (a &amp;&amp; ((x+y &gt; 0))) { // Noncompliant
9-
//...
12+
if (a &amp;&amp; ((x + y &gt; 0))) { // Noncompliant
13+
return ((x + 1)); // Noncompliant
1014
}
11-
12-
return ((x + 1)); // Noncompliant
1315
</pre>
1416
<h3>Compliant solution</h3>
15-
<pre>
17+
<pre data-diff-id="1" data-diff-type="compliant">
1618
int x = (y / 2 + 1);
1719

18-
if (a &amp;&amp; (x+y &gt; 0)) {
19-
//...
20+
if (a &amp;&amp; (x + y &gt; 0)) {
21+
return (x + 1);
2022
}
21-
22-
return (x + 1);
2323
</pre>
2424

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1116.html

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
<h2>Why is this an issue?</h2>
2-
<p>Empty statements, i.e. <code>;</code>, are usually introduced by mistake, for example because:</p>
3-
<ul>
4-
<li> It was meant to be replaced by an actual statement, but this was forgotten. </li>
5-
<li> There was a typo which lead the semicolon to be doubled, i.e. <code>;;</code>. </li>
6-
</ul>
7-
<h3>Noncompliant code example</h3>
8-
<pre>
2+
<p>Empty statements represented by a semicolon <code>;</code> are statements that do not perform any operation. They are often the result of a typo or
3+
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
4+
errors.</p>
5+
<h3>Code examples</h3>
6+
<h4>Noncompliant code example</h4>
7+
<pre data-diff-id="1" data-diff-type="noncompliant">
98
void doSomething() {
10-
; // Noncompliant - was used as a kind of TODO marker
9+
; // Noncompliant - was used as a kind of TODO marker
1110
}
1211

1312
void doSomethingElse() {
14-
System.out.println("Hello, world!");; // Noncompliant - double ;
15-
...
13+
System.out.println("Hello, world!");; // Noncompliant - double ;
14+
// ...
1615
}
1716
</pre>
18-
<h3>Compliant solution</h3>
19-
<pre>
17+
<h4>Compliant solution</h4>
18+
<pre data-diff-id="1" data-diff-type="compliant">
2019
void doSomething() {}
2120

2221
void doSomethingElse() {
2322
System.out.println("Hello, world!");
24-
...
25-
for (int i = 0; i &lt; 3; i++) ; // compliant if unique statement of a loop
26-
...
23+
// ...
24+
for (int i = 0; i &lt; 3; i++) ; // Compliant if unique statement of a loop
25+
// ...
2726
}
2827
</pre>
2928
<h2>Resources</h2>
29+
<h3>Documentation</h3>
3030
<ul>
3131
<li> <a href="https://wiki.sei.cmu.edu/confluence/x/5dUxBQ">CERT, MSC12-C.</a> - Detect and remove code that has no effect or is never executed
3232
</li>
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
<h2>Why is this an issue?</h2>
2-
<p>Overriding or shadowing a variable declared in an outer scope can strongly impact the readability, and therefore the maintainability, of a piece of
3-
code. Further, it could lead maintainers to introduce bugs because they think they’re using one variable but are really using another.</p>
2+
<p>Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.</p>
3+
<p>This can lead to three main problems:</p>
4+
<ul>
5+
<li> Confusion: The same name can refer to different variables in different parts of the scope, making the code hard to read and understand. </li>
6+
<li> Unintended Behavior: You might accidentally use the wrong variable, leading to hard-to-detect bugs. </li>
7+
<li> Maintenance Issues: If the inner variable is removed or renamed, the code’s behavior might change unexpectedly because the outer variable is
8+
now being used. </li>
9+
</ul>
10+
<p>To avoid these problems, rename the shadowing, shadowed, or both identifiers to accurately represent their purpose with unique and meaningful
11+
names.</p>
12+
<p>This rule focuses on variables in methods that shadow a field.</p>
13+
<h3>Noncompliant code example</h3>
414
<pre>
515
class Foo {
616
public int myField;
717

818
public void doSomething() {
919
int myField = 0; // Noncompliant
10-
...
20+
// ...
1121
}
1222
}
1323
</pre>
1424
<h2>Resources</h2>
1525
<h3>Documentation</h3>
1626
<ul>
17-
<li> <a href="https://wiki.sei.cmu.edu/confluence/display/c/DCL01-C.+Do+not+reuse+variable+names+in+subscopes">CERT, DCL01-C.</a> - Do not reuse
18-
variable names in subscopes </li>
19-
<li> <a href="https://wiki.sei.cmu.edu/confluence/display/java/DCL51-J.+Do+not+shadow+or+obscure+identifiers+in+subscopes">CERT, DCL51-J.</a> - Do
20-
not shadow or obscure identifiers in subscopes </li>
27+
<li> CERT - <a href="https://wiki.sei.cmu.edu/confluence/display/java/DCL51-J.+Do+not+shadow+or+obscure+identifiers+in+subscopes">DCL51-J. Do not
28+
shadow or obscure identifiers in subscopes</a> </li>
29+
</ul>
30+
<h3>Related rules</h3>
31+
<ul>
32+
<li> {rule:java:S2176} - Class names should not shadow interfaces or superclasses </li>
33+
<li> {rule:java:S2387} - Child class fields should not shadow parent class fields </li>
34+
<li> {rule:java:S4977} - Type parameters should not shadow other type parameters </li>
2135
</ul>
2236

java-checks/src/main/resources/org/sonar/l10n/java/rules/java/S1117.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"title": "Local variables should not shadow class fields",
32
"type": "CODE_SMELL",
43
"code": {
54
"impacts": {
@@ -22,10 +21,10 @@
2221
"sqKey": "S1117",
2322
"scope": "All",
2423
"quickfix": "unknown",
24+
"title": "Local variables should not shadow class fields",
2525
"securityStandards": {
2626
"CERT": [
27-
"DCL51-J.",
28-
"DCL01-C."
27+
"DCL51-J."
2928
]
3029
}
3130
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<h2>Why is this an issue?</h2>
2-
<p>Utility classes, which are collections of <code>static</code> members, are not meant to be instantiated. Even abstract utility classes, which can
3-
be extended, should not have public constructors.</p>
4-
<p>Java adds an implicit public constructor to every class which does not define at least one explicitly. Hence, at least one non-public constructor
5-
should be defined.</p>
6-
<h3>Noncompliant code example</h3>
2+
<p>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
3+
"utility class". A utility class is a class that only has static members, hence it should not be instantiated.</p>
4+
<h3>Exceptions</h3>
5+
<p>When a class contains <code>public static void main(String[] args)</code> method it is not considered as a utility class and will be ignored by
6+
this rule.</p>
7+
<h2>How to fix it</h2>
8+
<p>To prevent the class from being instantiated, you should define a non-public constructor. This will prevent the compiler from implicitly generating
9+
a public parameterless constructor.</p>
10+
<h3>Code examples</h3>
11+
<h4>Noncompliant code example</h4>
712
<pre data-diff-id="1" data-diff-type="noncompliant">
813
class StringUtils { // Noncompliant
914

@@ -13,7 +18,7 @@ <h3>Noncompliant code example</h3>
1318

1419
}
1520
</pre>
16-
<h3>Compliant solution</h3>
21+
<h4>Compliant solution</h4>
1722
<pre data-diff-id="1" data-diff-type="compliant">
1823
class StringUtils { // Compliant
1924

@@ -27,7 +32,4 @@ <h3>Compliant solution</h3>
2732

2833
}
2934
</pre>
30-
<h3>Exceptions</h3>
31-
<p>When class contains <code>public static void main(String[] args)</code> method it is not considered as utility class and will be ignored by this
32-
rule.</p>
3335

0 commit comments

Comments
 (0)