Skip to content

Commit

Permalink
Update RSPEC before 9.29 release (#9524)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-marichal authored Jul 11, 2024
1 parent 885074e commit 8841016
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
33 changes: 17 additions & 16 deletions analyzers/rspec/cs/S1144.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ <h2>Why is this an issue?</h2>
<p>A type or member that is never called is dead code, and should be removed. Cleaning out dead code decreases the size of the maintained codebase,
making it easier to understand the program and preventing bugs from being introduced.</p>
<p>This rule detects type or members that are never referenced from inside a translation unit, and cannot be referenced from the outside.</p>
<h3>Exceptions</h3>
<p>This rule doesn’t raise issues on:</p>
<ul>
<li> empty constructors </li>
<li> members with attributes </li>
<li> the <code>Main</code> method of the application </li>
<li> <code>void</code> methods with two parameters when the second parameter type derives from <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.eventargs">EventArgs</a> </li>
<li> empty serialization constructor on type with <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute">System.SerializableAttribute</a> attribute. </li>
<li> field and property members of types marked with <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute">System.SerializableAttribute</a> attribute </li>
<li> internal members in assemblies that have a <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.internalsvisibletoattribute">System.Runtime.CompilerServices.InternalsVisibleToAttribute</a> attribute. </li>
<li> types and members decorated with the <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute">System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute</a> attribute (available in .NET 5.0+) or a custom attribute named <code>DynamicallyAccessedMembersAttribute</code>. </li>
</ul>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
Expand Down Expand Up @@ -30,22 +47,6 @@ <h4>Compliant solution</h4>
private class UsedClass {...}
}
</pre>
<h3>Exceptions</h3>
<p>This rule doesn’t raise issues on:</p>
<ul>
<li> empty constructors </li>
<li> members with attributes </li>
<li> the <code>Main</code> method of the application </li>
<li> methods with event handler signature <code>void Foo(object, EventArgs)</code> that are declared in partial class </li>
<li> empty serialization constructor on type with <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute">System.SerializableAttribute</a> attribute. </li>
<li> field and property members of types marked with <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute">System.SerializableAttribute</a> attribute </li>
<li> internal members in assemblies that have a <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.internalsvisibletoattribute">System.Runtime.CompilerServices.InternalsVisibleToAttribute</a> attribute. </li>
<li> types and members decorated with the <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute">System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute</a> attribute (available in .NET 5.0+) or a custom attribute named <code>DynamicallyAccessedMembersAttribute</code>. </li>
</ul>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
Expand Down
4 changes: 3 additions & 1 deletion analyzers/rspec/cs/S2699.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ <h2>Why is this an issue?</h2>
<li> <code>FluentAssertions</code> (4.x and 5.x) </li>
<li> <code>NFluent</code> </li>
<li> <code>NSubstitute</code> </li>
<li> <code>Moq</code> </li>
<li> <code>Shoudly</code> </li>
</ul>
<p>By enforcing the presence of assertions, this rule aims to enhance the reliability and comprehensiveness of tests by ensuring that they provide
Expand All @@ -26,7 +27,8 @@ <h3>Exceptions</h3>
<p>Test methods that include a call to a custom assertion method will not raise any issues.</p>
<h3>How to fix it</h3>
<p>To address this issue, you should include assertions to validate the expected behavior. Choose an appropriate assertion method provided by your
testing framework (such as MSTest, NUnit, xUnit) or select a suitable assertion library like FluentAssertions, NFluent, NSubstitute, or Shouldly.</p>
testing framework (such as MSTest, NUnit, xUnit) or select a suitable assertion library like FluentAssertions, NFluent, NSubstitute, Moq, or
Shouldly.</p>
<p>In addition to using built-in assertion methods, you also have the option to create custom assertion methods. To do this, declare an attribute
named <code>[AssertionMethodAttribute]</code> and apply it to the respective method. This allows you to encapsulate specific validation logic within
your custom assertion methods without raising the issue. Here’s an example:</p>
Expand Down
42 changes: 28 additions & 14 deletions analyzers/rspec/cs/S3247.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
<h2>Why is this an issue?</h2>
<p>Because the <code>is</code> operator performs a cast if the object is not null, using <code>is</code> to check type and then casting the same
argument to that type, necessarily performs two casts. The same result can be achieved more efficiently with a single cast using <code>as</code>,
followed by a null-check.</p>
<h3>Noncompliant code example</h3>
<pre>
<p>In C#, the <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast#is-operator"><code>is</code>
type testing operator</a> can be used to check if the run-time type of an object is compatible with a given type. If the object is not null, then the
<code>is</code> operator performs a cast, and so performing another cast following the check result is redundant.</p>
<p>This can impact:</p>
<ul>
<li> Performance: Performing the type check and cast separately can lead to minor performance issues. While this might not be noticeable in small
applications, it can add up in larger, more complex systems. </li>
<li> Readability: The code is less readable and less clean because it requires two lines (and two operations) to achieve something that could be
done in one. </li>
</ul>
<h2>How to fix it</h2>
<p>Use <a href="https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching">pattern macthing</a> to perform the check
and retrieve the cast result.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
if (x is Fruit) // Noncompliant
{
var f = (Fruit)x; // or x as Fruit
// ...
}
</pre>
<h3>Compliant solution</h3>
<pre>
// C# 6
var f = x as Fruit;
if (f != null)
{
// ...
}
// C# 7
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
if (x is Fruit fruit)
{
// ...
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast">Type-testing
operators and cast expressions - <code>is</code>, <code>as</code>, <code>typeof</code> and casts</a> </li>
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/is">is operator (C# reference)</a>
</li>
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching">Pattern matching
overview</a> </li>
</ul>

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": "2024-06-24T13:02:32.572091600Z",
"latest-update": "2024-07-11T14:36:23.720511300Z",
"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": "2024-06-24T13:03:10.676891100Z",
"latest-update": "2024-07-11T14:37:00.682263800Z",
"options": {
"no-language-in-filenames": true
}
Expand Down

0 comments on commit 8841016

Please sign in to comment.