Skip to content

Commit

Permalink
Update RSPEC before 9.2 release (#7297)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource authored May 30, 2023
1 parent d2ca13a commit 9d55571
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 40 deletions.
23 changes: 7 additions & 16 deletions analyzers/rspec/cs/S3981.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
<h2>Why is this an issue?</h2>
<p>The size of a collection and the length of an array are always greater than or equal to zero. So testing that a size or length is greater than or
equal to zero doesn’t make sense, since the result is always <code>true</code>. Similarly testing that it is less than zero will always return
<code>false</code>. Perhaps the intent was to check the non-emptiness of the collection or array instead.</p>
<h3>Noncompliant code example</h3>
<p>The size of a collection and the length of an array are always greater than or equal to zero. Testing it doesn’t make sense, since the result is
always <code>true</code>.</p>
<pre>
if(collection.Count &gt;= 0){...}
if(collection.Count &gt;= 0){...} // Noncompliant: always true

if(enumerable.Count() &lt; 0){...}

if(array.Length &gt;= 0){...}

bool result = array.Length &gt;=0;
if(array.Length &gt;= 0){...} // Noncompliant: always true
</pre>
<h3>Compliant solution</h3>
<p>Similarly testing that it is less than zero will always return <code>false</code>.</p>
<pre>
if (list.Any()) { ... }

if (list.Count &gt; 0) { ... }

if (array.Length &gt;= 42) { ... }
if(enumerable.Count() &lt; 0){...} // Noncompliant: always false
</pre>
<p>Fix the code to properly check for emptiness if it was the intent, or remove the redundant code to keep the current behavior.</p>

14 changes: 7 additions & 7 deletions analyzers/rspec/cs/S4487.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<h2>Why is this an issue?</h2>
<p>Private fields only used to store values without reading them later is a case of dead store. So changing the value of such field is useless and
most probably indicates a serious error in the code.</p>
<h3>Noncompliant code example</h3>
<pre>
<p>Private fields which are written but never read are a case of "dead store". Changing the value of such a field is useless and most probably
indicates an error in the code.</p>
<pre data-diff-id="1" data-diff-type="noncompliant">
public class Rectangle
{
private readonly int length;
private readonly int width; // width is written but never read
private readonly int width; // Noncompliant: width is written but never read

public Rectangle(int length, int width)
{
Expand All @@ -23,8 +22,8 @@ <h3>Noncompliant code example</h3>
}
}
</pre>
<h3>Compliant solution</h3>
<pre>
<p>Remove this field if it doesn’t need to be read, or fix the code to read it.</p>
<pre data-diff-id="1" data-diff-type="compliant">
public class Rectangle
{
private readonly int length;
Expand All @@ -46,6 +45,7 @@ <h3>Compliant solution</h3>
}
</pre>
<h2>Resources</h2>
<h3>Standards</h3>
<ul>
<li> <a href="https://cwe.mitre.org/data/definitions/563">MITRE, CWE-563</a> - Assignment to Variable without Use ('Unused Variable') </li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion analyzers/rspec/cs/S6603.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"constantCost": "5min"
},
"tags": [
"perfomance"
"performance"
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-6603",
Expand Down
1 change: 1 addition & 0 deletions analyzers/rspec/cs/Sonar_way_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
"S3926",
"S3927",
"S3928",
"S3949",
"S3963",
"S3966",
"S3971",
Expand Down
22 changes: 9 additions & 13 deletions analyzers/rspec/vbnet/S3981.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
<h2>Why is this an issue?</h2>
<p>The size of a collection and the length of an array are always greater than or equal to zero. So testing that a size or length is greater than or
equal to zero doesn’t make sense, since the result is always <code>true</code>. Similarly testing that it is less than zero will always return
<code>false</code>. Perhaps the intent was to check the non-emptiness of the collection or array instead.</p>
<h3>Noncompliant code example</h3>
<p>The size of a collection and the length of an array are always greater than or equal to zero. Testing it doesn’t make sense, since the result is
always <code>true</code>.</p>
<pre>
If Collection.Count &gt;= 0 Then ...
If Collection.Count &gt;= 0 Then ... 'Noncompliant always true

If Enumerable.Count &lt; 0 Then ...

If array.Length &gt;= 0 Then ...

Dim result As Boolean = Array.Length &gt;= 0
If array.Length &gt;= 0 Then ... 'Noncompliant always true
</pre>
<h3>Compliant solution</h3>
<p>Similarly testing that it is less than zero will always return <code>false</code>.</p>
<pre>
If list.Count = 0 Then ...
If array.Length &gt;= 42 Then ...
If Enumerable.Count &lt; 0 Then ... 'Noncompliant always false

Dim result As Boolean = Array.Length &gt;= 0 'Noncompliant always true
</pre>
<p>Fix the code to properly check for emptiness if it was the intent, or remove the redundant code to keep the current behavior.</p>

2 changes: 1 addition & 1 deletion analyzers/rspec/vbnet/S6603.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"constantCost": "5min"
},
"tags": [
"perfomance"
"performance"
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-6603",
Expand Down
2 changes: 1 addition & 1 deletion analyzers/src/SonarAnalyzer.CSharp/sonarpedia.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"languages": [
"CSH"
],
"latest-update": "2023-05-26T08:18:45.873798200Z",
"latest-update": "2023-05-30T08:21:27.671874200Z",
"options": {
"no-language-in-filenames": true
}
Expand Down
2 changes: 1 addition & 1 deletion analyzers/src/SonarAnalyzer.VisualBasic/sonarpedia.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"languages": [
"VBNET"
],
"latest-update": "2023-05-26T08:23:03.863046500Z",
"latest-update": "2023-05-30T08:23:24.735391300Z",
"options": {
"no-language-in-filenames": true
}
Expand Down

0 comments on commit 9d55571

Please sign in to comment.