-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9164a51
commit 6bc8c18
Showing
11 changed files
with
96 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
<p>Certain mathematical comparisons will always return the same value, and should simply not be made.</p> | ||
<p>This comparison will always return <code>false</code>:</p> | ||
<p>These comparisons will return either always <code>true</code> or always <code>false</code> depending on the kind of comparison:</p> | ||
<ul> | ||
<li> comparing a <code>float</code> with a <code>double</code> constant that’s outside the <code>float</code> range </li> | ||
</ul> | ||
<p>These will always return <code>true</code>:</p> | ||
<ul> | ||
<li> comparing <code>aByte <= Byte.MaxValue</code> and <code>aByte >= Byte.MinValue</code> </li> | ||
<li> comparing <code>anInt <= int.MaxValue</code> and <code>anInt >= int.MinValue</code> </li> | ||
<li> comparing <code>aLong <= long.MaxValue</code> and <code>aLong >= long.MinValue</code> </li> | ||
<li> Comparing a <code>char</code> with a numeric constant that is outside of the range of <code>char</code>. </li> | ||
<li> Comparing a <code>float</code> with a numeric constant that is outside of the range of <code>float</code>. </li> | ||
<li> Comparing a <code>long</code> with a numeric constant that is outside of the range of <code>long</code>. </li> | ||
<li> Comparing a <code>ulong</code> with a numeric constant that is outside of the range of <code>ulong</code>. </li> | ||
</ul> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
float f = 42.0f; | ||
const double d = float.MaxValue + 1; | ||
if (f <= d) { } // Noncompliant | ||
if (f <= double.MaxValue) { } // Noncompliant | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,43 @@ | ||
<p><code>StringBuilder</code> instances that are <code>append</code>ed but never <code>toString</code>ed needlessly clutter the code, and worse are a | ||
drag on performance. Either they should be removed, or the missing <code>toString</code> call added.</p> | ||
<p><code>StringBuilder</code> instances that never build a <code>string</code> clutter the code and worse are a drag on performance. Either they | ||
should be removed, or the missing <code>ToString()</code> call should be added.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
public void doSomething(List<string> strings) { | ||
StringBuilder sb = new StringBuilder(); // Noncompliant | ||
public void DoSomething(List<string> strings) { | ||
var sb = new StringBuilder(); // Noncompliant | ||
sb.Append("Got: "); | ||
foreach(string str in strings) { | ||
foreach(var str in strings) { | ||
sb.Append(str).Append(", "); | ||
// ... | ||
} | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
public void doSomething(List<string> strings) { | ||
foreach(string str in strings) { | ||
public void DoSomething(List<string> strings) { | ||
foreach(var str in strings) { | ||
// ... | ||
} | ||
} | ||
</pre> | ||
<p>or</p> | ||
<pre> | ||
public void doSomething(List<string> strings) { | ||
StringBuilder sb = new StringBuilder(); // Noncompliant | ||
public void DoSomething(List<string> strings) { | ||
var sb = new StringBuilder(); | ||
sb.Append("Got: "); | ||
foreach(string str in strings) { | ||
foreach(var str in strings) { | ||
sb.Append(str).Append(", "); | ||
// ... | ||
} | ||
_logger.LogInformation(sb.toString, DateTimeOffset.UtcNow); | ||
logger.LogInformation(sb.ToString()); | ||
} | ||
</pre> | ||
<h2>Exceptions</h2> | ||
<p>This rule ignores <code>StringBuilder</code>s that are passed as method arguments on the grounds that they are likely <code>toString</code>ed | ||
there.</p> | ||
<p>No issue is reported when <code>StringBuilder</code> is:</p> | ||
<ul> | ||
<li> Accessed through <code>sb.CopyTo()</code>, <code>sb.GetChunks()</code>, <code>sb.Length</code>, or <code>sb[index]</code>. </li> | ||
<li> Passed as a method argument, on the grounds that it will likely be accessed through a <code>ToString()</code> invocation there. </li> | ||
<li> Passed in as a parameter to the current method, on the grounds that the callee will materialize the string. </li> | ||
<li> Retrieved by a custom function (<code>var sb = GetStringBuilder();</code>). </li> | ||
<li> Returned by the method. </li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
<p>There’s no point in creating an array solely for the purpose of passing it as a params (<code>...</code>) argument; params keyword allow to pass a | ||
variable number of parameters that will behave exactly like an array variable inside the method implementation. Simply pass the elements directly.</p> | ||
<p>There’s no point in creating an array solely for the purpose of passing it to a <code>params</code> parameter. Simply pass the elements directly. | ||
They will be consolidated into an array automatically.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
public void CallTheThing() { | ||
//... | ||
DoTheThing(new string[] { "s1", "s2"}); // Noncompliant: unnecessary | ||
DoTheThing(new string[12]); // Compliant | ||
// ... | ||
public void Base() | ||
{ | ||
Method(new string[] { "s1", "s2" }); // Noncompliant: unnecessary | ||
Method(new string[] { }); // Noncompliant | ||
Method(new string[12]); // Compliant | ||
} | ||
|
||
public void DoTheThing (params string[] args) { | ||
// ... | ||
public void Method(params string[] args) | ||
{ | ||
// ... | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
public void CallTheThing() | ||
public void Base() | ||
{ | ||
//... | ||
DoTheThing("s1", "s2"); | ||
DoTheThing(new string[12]); | ||
// ... | ||
Method("s1", "s2"); | ||
Method(); | ||
Method(new string[12]); | ||
} | ||
|
||
public void DoTheThing(params string[] args) | ||
public void Method(params string[] args) | ||
{ | ||
// ... | ||
// ... | ||
} | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,12 @@ <h2>Noncompliant Code Example</h2> | |
<pre> | ||
// | ||
|
||
/* | ||
*/ | ||
|
||
/// | ||
|
||
/** | ||
|
||
*/ | ||
|
||
/* | ||
|
||
*/ | ||
*/ | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,29 @@ | ||
<p>There’s no point in creating an array solely for the purpose of passing it as a ParamArray (<code>...</code>) argument; ParamArray keyword allow to | ||
pass a variable number of parameters that will behave exactly like an array variable inside the method implementation. Simply pass the elements | ||
directly.</p> | ||
<p>There’s no point in creating an array solely for the purpose of passing it to a <code>ParamArray</code> parameter. Simply pass the elements | ||
directly. They will be consolidated into an array automatically.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
Class SurroundingClass | ||
Public Sub CallTheThing() | ||
DoTheThing(New String() {"s1", "s2"}) | ||
DoTheThing(New String(11) {}) | ||
End Sub | ||
Public Sub Base() | ||
Method(New String() { "s1", "s2" }) ' Noncompliant: unnecessary | ||
Method(New String(12) {}) ' Compliant | ||
End Sub | ||
|
||
Public Sub DoTheThing(ParamArray args As String()) | ||
' Do something | ||
End Sub | ||
Public Sub Method(ParamArray args As String()) | ||
' Do something | ||
End Sub | ||
End Class | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
Class SurroundingClass | ||
Public Sub CallTheThing() | ||
DoTheThing("s1", "s2") | ||
DoTheThing(New String(11) {}) | ||
End Sub | ||
Public Sub Base() | ||
Method("s1", "s2") | ||
Method(New String(12) {}) | ||
End Sub | ||
|
||
Public Sub DoTheThing(ParamArray args As String()) | ||
' Do something | ||
End Sub | ||
Public Sub Method(ParamArray args As String()) | ||
' Do something | ||
End Sub | ||
End Class | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters