Skip to content

Commit eefdd88

Browse files
Update RSPEC before 9.4 release (#7460)
1 parent 6cde32d commit eefdd88

25 files changed

+190
-121
lines changed

analyzers/rspec/cs/S122.html

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
<h2>Why is this an issue?</h2>
2-
<p>For better readability, do not put more than one statement on a single line.</p>
3-
<h3>Noncompliant code example</h3>
2+
<p>Putting multiple statements on a single line lowers the code readability and makes debugging the code more complex.</p>
43
<pre>
5-
if(someCondition) DoSomething();
4+
if (someCondition) DoSomething(); // Noncompliant
65
</pre>
7-
<h3>Compliant solution</h3>
6+
<p>Write one statement per line to improve readability.</p>
87
<pre>
9-
if(someCondition)
8+
if (someCondition)
109
{
1110
DoSomething();
1211
}
1312
</pre>
1413
<h3>Exceptions</h3>
15-
<p>Anonymous functions containing a single statement are ignored. Block statements are not considered either.</p>
14+
<p>The rule ignores:</p>
15+
<ul>
16+
<li> block statements </li>
17+
<li> anonymous functions containing a single statement </li>
18+
</ul>
1619
<pre>
17-
Func&lt;object, bool&gt; item1 = o =&gt; { return true; }; // Compliant
20+
Func&lt;object, bool&gt; item1 = o =&gt; { return true; }; // Compliant by exception
1821
Func&lt;object, bool&gt; item1 = o =&gt; { var r = false; return r; }; // Noncompliant
1922
</pre>
2023

analyzers/rspec/cs/S2198.html

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
<h2>Why is this an issue?</h2>
2-
<p>Certain mathematical comparisons will always return the same value, and should simply not be made.</p>
3-
<p>These comparisons will return either always <code>true</code> or always <code>false</code> depending on the kind of comparison:</p>
2+
<p>Certain <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators">mathematical comparisons</a>
3+
will always return the same value, and should not be performed.</p>
4+
<p>Specifically, the following comparisons will return either always <code>true</code> or always <code>false</code> depending on the kind of
5+
comparison:</p>
46
<ul>
5-
<li> Comparing a <code>char</code> with a numeric constant that is outside of the range of <code>char</code>. </li>
6-
<li> Comparing a <code>float</code> with a numeric constant that is outside of the range of <code>float</code>. </li>
7-
<li> Comparing a <code>long</code> with a numeric constant that is outside of the range of <code>long</code>. </li>
8-
<li> Comparing a <code>ulong</code> with a numeric constant that is outside of the range of <code>ulong</code>. </li>
7+
<li> comparing a <code>char</code> with a numeric constant that is outside of the range of <code>char</code> </li>
8+
<li> comparing a <code>float</code> with a numeric constant that is outside of the range of <code>float</code> </li>
9+
<li> comparing a <code>long</code> with a numeric constant that is outside of the range of <code>long</code> </li>
10+
<li> comparing a <code>ulong</code> with a numeric constant that is outside of the range of <code>ulong</code> </li>
11+
<li> etc. </li>
912
</ul>
1013
<h3>Noncompliant code example</h3>
1114
<pre>
1215
float f = 42.0f;
13-
if (f &lt;= double.MaxValue) { } // Noncompliant
16+
if (f &lt;= double.MaxValue) { } // Noncompliant: always true
17+
if (f &gt; double.MaxValue) { } // Noncompliant: always false
1418
</pre>
19+
<h2>Resources</h2>
20+
<h3>Documentation</h3>
21+
<ul>
22+
<li> Microsoft Learn: <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators">Comparison
23+
operators (C# reference)</a> </li>
24+
<li> Microsoft Learn: <a
25+
href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#characteristics-of-the-integral-types">Ranges for integral numeric types (C# reference)</a> </li>
26+
<li> Microsoft Learn: <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/char">Range for char (C#
27+
reference)</a> </li>
28+
</ul>
1529

analyzers/rspec/cs/S2198.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
"ruleSpecification": "RSPEC-2198",
1414
"sqKey": "S2198",
1515
"scope": "Main",
16-
"quickfix": "unknown"
16+
"quickfix": "targeted"
1717
}

analyzers/rspec/cs/S2583.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
570
2424
]
2525
},
26-
"quickfix": "partial"
26+
"quickfix": "targeted"
2727
}

analyzers/rspec/cs/S2681.html

+15-28
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,30 @@
11
<h2>Why is this an issue?</h2>
2-
<p>Curly braces can be omitted from a one-line block, such as with an <code>if</code> statement or <code>for</code> loop, but doing so can be
3-
misleading and induce bugs.</p>
4-
<p>This rule raises an issue when the whitespacing of the lines after a one line block indicates an intent to include those lines in the block, but
2+
<p>Having inconsistent indentation and omitting curly braces from a control structure, such as an <code>if</code> statement or <code>for</code> loop,
3+
is misleading and can induce bugs.</p>
4+
<p>This rule raises an issue when the indentation of the lines after a control structure indicates an intent to include those lines in the block, but
55
the omission of curly braces means the lines will be unconditionally executed once.</p>
6-
<p>Note that this rule considers tab characters to be equivalent to 1 space. If you mix spaces and tabs you will sometimes see issues in code which
7-
look fine in your editor but are confusing when you change the size of tabs.</p>
8-
<h3>Noncompliant code example</h3>
6+
<p>The following patterns are recognized:</p>
97
<pre>
108
if (condition)
119
FirstActionInBlock();
12-
SecondAction(); // Noncompliant; executed unconditionally
10+
SecondAction(); // Noncompliant: SecondAction is executed unconditionally
1311
ThirdAction();
14-
15-
if(condition) FirstActionInBlock(); SecondAction(); // Noncompliant; secondAction executed unconditionally
16-
17-
if(condition) FirstActionInBlock(); // Noncompliant
18-
SecondAction(); // Executed unconditionally
19-
20-
string str = null;
21-
for (int i = 0; i &lt; array.Length; i++)
22-
str = array[i];
23-
DoTheThing(str); // Noncompliant; executed only on last array element
2412
</pre>
25-
<h3>Compliant solution</h3>
2613
<pre>
27-
if (condition)
28-
{
29-
FirstActionInBlock();
30-
SecondAction();
31-
}
32-
ThirdAction();
33-
14+
if(condition) FirstActionInBlock(); SecondAction(); // Noncompliant: SecondAction is executed unconditionally
15+
</pre>
16+
<pre>
17+
if (condition) FirstActionInBlock();
18+
SecondAction(); // Noncompliant: SecondAction is executed unconditionally
19+
</pre>
20+
<pre>
3421
string str = null;
3522
for (int i = 0; i &lt; array.Length; i++)
36-
{
3723
str = array[i];
38-
DoTheThing(str);
39-
}
24+
DoTheThing(str); // Noncompliant: executed only on the last element
4025
</pre>
26+
<p>Note that this rule considers tab characters to be equivalent to 1 space. When mixing spaces and tabs, a code may look fine in one editor but be
27+
confusing in another configured differently.</p>
4128
<h2>Resources</h2>
4229
<ul>
4330
<li> <a href="https://cwe.mitre.org/data/definitions/483">MITRE, CWE-483</a> - Incorrect Block Delimitation </li>

analyzers/rspec/cs/S2761.html

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
<h2>Why is this an issue?</h2>
2-
<p>Calling the <code>!</code> or <code>~</code> prefix operator twice does nothing: the second invocation undoes the first. Such mistakes are
3-
typically caused by accidentally double-tapping the key in question without noticing.</p>
4-
<p>Either this is a bug, if the operator was actually meant to be called once, or misleading if done on purpose.</p>
5-
<h3>Noncompliant code example</h3>
2+
<p>The repetition of a prefix operator (<code>!</code>, or <code>~</code>) is usually a typo. The second operator invalidates the first one.</p>
63
<pre>
74
int v1 = 0;
85
bool v2 = false;
96

10-
var v3 = !!v1; // Noncompliant
11-
var v4 = ~~v2; // Noncompliant
12-
</pre>
13-
<h3>Compliant solution</h3>
14-
<pre>
15-
int v1 = 0;
16-
bool v2 = false;
17-
18-
var v3 = !v1;
19-
var v4 = ~v2;
7+
var v3 = !!v1; // Noncompliant: equivalent to "v1"
8+
var v4 = ~~v2; // Noncompliant: equivalent to "v2"
209
</pre>
2110

analyzers/rspec/cs/S3451.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ <h2>Why is this an issue?</h2>
22
<p><a href="https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.defaultvalueattribute">DefaultValue</a> does not make the compiler set
33
the default value, as its name may suggest. What you probably wanted to use is <a
44
href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.defaultparametervalueattribute">DefaultParameterValue</a>.</p>
5-
<p>The <a href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.defaultparametervalueattribute">DefaultValue</a> attribute
6-
from the <code>System.ComponentModel</code> namespace, is sometimes used to declare a member’s default value. This can be used, for instance, by the
7-
reset feature of a visual designer or by a code generator.</p>
5+
<p>The <a href="https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.defaultvalueattribute">DefaultValue</a> attribute from the
6+
<code>System.ComponentModel</code> namespace, is sometimes used to declare a member’s default value. This can be used, for instance, by the reset
7+
feature of a visual designer or by a code generator.</p>
88
<pre>
99
public void DoStuff([DefaultValue(4)] int i)
1010
{

analyzers/rspec/cs/S3776.html

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
11
<h2>Why is this an issue?</h2>
2-
<p>Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be
3-
difficult to maintain.</p>
2+
<p><a href="https://www.sonarsource.com/docs/CognitiveComplexity.pdf">Cognitive Complexity</a> Complexity is a measure of how hard the control flow of
3+
a method is to understand.</p>
4+
<p>Methods with high Cognitive Complexity will be difficult to maintain.</p>
5+
<pre data-diff-id="1" data-diff-type="noncompliant">
6+
int Abs(int n) // Noncompliant: cognitive complexity = 5
7+
{
8+
if (n &gt;= 0) // +1
9+
{
10+
return n;
11+
}
12+
else // +2, due to nesting
13+
{
14+
if (n == int.MinValue) // +1
15+
{
16+
throw new ArgumentException("The absolute value of int.MinValue is outside of int boundaries");
17+
}
18+
else // +1
19+
{
20+
return -n;
21+
}
22+
}
23+
}
24+
</pre>
25+
<p>They should be refactored to have lower complexity:</p>
26+
<pre data-diff-id="1" data-diff-type="compliant">
27+
int Abs(int n) // Compliant: cognitive complexity = 3
28+
{
29+
if (n == int.MinValue) // +1
30+
{
31+
throw new ArgumentException("The absolute value of int.MinValue is outside of int boundaries");
32+
}
33+
else if (n &gt;= 0) // +1
34+
{
35+
return n;
36+
}
37+
else // +1
38+
{
39+
return -n;
40+
}
41+
}
42+
</pre>
443
<h2>Resources</h2>
44+
<h3>Documentation</h3>
545
<ul>
646
<li> <a href="https://www.sonarsource.com/docs/CognitiveComplexity.pdf">Cognitive Complexity</a> </li>
747
</ul>

analyzers/rspec/cs/S3776.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
"ruleSpecification": "RSPEC-3776",
1414
"sqKey": "S3776",
1515
"scope": "All",
16-
"quickfix": "unknown"
16+
"quickfix": "infeasible"
1717
}

analyzers/rspec/cs/S3966.html

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ <h4>Noncompliant code example</h4>
1414
var foo = new Disposable();
1515
foo.Dispose();
1616
foo.Dispose(); // Noncompliant
17-
17+
</pre>
18+
<pre data-diff-id="2" data-diff-type="noncompliant">
1819
using (var bar = new Disposable()) // Noncompliant
1920
{
2021
bar.Dispose();
@@ -24,7 +25,8 @@ <h4>Compliant solution</h4>
2425
<pre data-diff-id="1" data-diff-type="compliant">
2526
var foo = new Disposable();
2627
foo.Dispose();
27-
28+
</pre>
29+
<pre data-diff-id="2" data-diff-type="compliant">
2830
using (var bar = new Disposable()) // Compliant
2931
{
3032

analyzers/rspec/cs/S4487.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ <h2>Why is this an issue?</h2>
3232
public Rectangle(int length, int width)
3333
{
3434
this.length = length;
35-
this.width= width;
35+
this.width = width;
3636
}
3737

3838
public int Surface

analyzers/rspec/cs/S4586.html

+20-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ <h2>Why is this an issue?</h2>
44
href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.completedtask"><code>Task.CompletedTask</code></a> or <a
55
href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.fromresult"><code>Task.FromResult&lt;TResult&gt;(null)</code></a>
66
respectively.</p>
7-
<pre data-diff-id="1" data-diff-type="noncompliant">
7+
<pre>
88
public Task DoFooAsync()
99
{
1010
return null; // Noncompliant: Causes a NullReferenceException if awaited.
@@ -15,23 +15,36 @@ <h2>Why is this an issue?</h2>
1515
await DoFooAsync(); // NullReferenceException
1616
}
1717
</pre>
18-
<pre data-diff-id="1" data-diff-type="compliant">
18+
<h2>How to fix it</h2>
19+
<p>Instead of <code>null</code> <a
20+
href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.completedtask"><code>Task.CompletedTask</code></a> or <a
21+
href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.fromresult"><code>Task.FromResult&lt;TResult&gt;(null)</code></a>
22+
should be returned.</p>
23+
<h3>Code examples</h3>
24+
<p>A <code>Task</code> returning method can be fixed like so:</p>
25+
<h4>Noncompliant code example</h4>
26+
<pre data-diff-id="1" data-diff-type="noncompliant">
1927
public Task DoFooAsync()
2028
{
21-
return Task.CompletedTask; // Compliant: Method can be awaited.
29+
return null; // Noncompliant: Causes a NullReferenceException if awaited.
2230
}
23-
24-
public async Task Main()
31+
</pre>
32+
<h4>Compliant solution</h4>
33+
<pre data-diff-id="1" data-diff-type="compliant">
34+
public Task DoFooAsync()
2535
{
26-
await DoFooAsync();
36+
return Task.CompletedTask; // Compliant: Method can be awaited.
2737
}
2838
</pre>
39+
<p>A <code>Task&lt;TResult&gt;</code> returning method can be fixed like so:</p>
40+
<h4>Noncompliant code example</h4>
2941
<pre data-diff-id="2" data-diff-type="noncompliant">
3042
public Task&lt;object&gt; GetFooAsync()
3143
{
3244
return null; // Noncompliant: Causes a NullReferenceException if awaited.
3345
}
3446
</pre>
47+
<h4>Compliant solution</h4>
3548
<pre data-diff-id="2" data-diff-type="compliant">
3649
public Task&lt;object&gt; GetFooAsync()
3750
{
@@ -53,6 +66,6 @@ <h3>Articles &amp; blog posts</h3>
5366
</li>
5467
<li> StackOverflow - Answer by Stephen Cleary for <a href="https://stackoverflow.com/a/27551261">Best way to handle null task inside async
5568
method?</a> </li>
56-
<li> C# Language Design Proposal - <a href="https://github.com/dotnet/csharplang/issues/35">Champion "Null-conditional await"</a> </li>
69+
<li> C# Language Design - <a href="https://github.com/dotnet/csharplang/issues/35">Proposal Champion "Null-conditional await"</a> </li>
5770
</ul>
5871

analyzers/rspec/cs/S4586.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
"ruleSpecification": "RSPEC-4586",
1414
"sqKey": "S4586",
1515
"scope": "All",
16-
"quickfix": "unknown"
16+
"quickfix": "targeted"
1717
}

analyzers/rspec/cs/S4635.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"title": "Seeking characters from a substring should be avoided",
2+
"title": "Start index should be used instead of calling Substring",
33
"type": "CODE_SMELL",
44
"status": "ready",
55
"remediation": {

analyzers/rspec/vbnet/S122.html

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
<h2>Why is this an issue?</h2>
2-
<p>For better readability, do not put more than one statement on a single line.</p>
3-
<h3>Noncompliant code example</h3>
2+
<p>Putting multiple statements on a single line lowers the code readability and makes debugging the code more complex.</p>
43
<pre>
5-
Module Module1
6-
Sub Main()
7-
Dim a = 0 : Dim b = 0 ' Noncompliant
8-
End Sub
9-
End Module
4+
Dim a = 0 : Dim b = 0 ' Noncompliant
105
</pre>
11-
<h3>Compliant solution</h3>
6+
<p>Write one statement per line to improve readability.</p>
127
<pre>
13-
Module Module1
14-
Sub Main()
15-
Dim a = 0 ' Compliant
16-
Dim b = 0 ' Compliant
17-
End Sub
18-
End Module
8+
Dim a = 0
9+
Dim b = 0
1910
</pre>
2011

analyzers/rspec/vbnet/S2761.html

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<h2>Why is this an issue?</h2>
2-
<p>Calling the <code>Not</code> operator twice does nothing: the second invocation undoes the first. Either this is a bug, if the operator was
3-
actually meant to be called once, or misleading if done on purpose.</p>
4-
<h3>Noncompliant code example</h3>
2+
<p>The repetition of the <code>Not</code> operator is usually a typo. The second operator invalidates the first one:</p>
53
<pre>
64
Dim b As Boolean = False
7-
Dim c As Boolean = Not Not b 'Noncompliant
8-
</pre>
9-
<h3>Compliant solution</h3>
10-
<pre>
11-
Dim b As Boolean = False
12-
Dim c As Boolean = b 'Compliant
5+
Dim c As Boolean = Not Not b 'Noncompliant: equivalent to "b"
136
</pre>
147

0 commit comments

Comments
 (0)