-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update RSPECs as part of the LayC second migration (batch#4) (#7540)
- Loading branch information
1 parent
5b9856d
commit 5735c1f
Showing
17 changed files
with
170 additions
and
75 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,38 +1,54 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>Finalizers (historically referred to as destructors) are used to perform any necessary final clean-up when the garbage collector is collecting a | ||
class instance. The programmer has no control over when the destructor is called; the garbage collector decides when to call it, and the destructor | ||
implicitly calls <code>Finalize</code> on the object’s base class.</p> | ||
<p>When you create a destructor, you should never throw an exception in it, as you are risking having your application terminated without a graceful | ||
cleanup.</p> | ||
<p>If a destructor throws an exception, and the runtime is not hosted by an application that overrides the default policy and the behavior of the | ||
<code>UnhandledExceptionEventHandler</code>, then the runtime terminates the process immediately without graceful cleanup (<code>finally</code> blocks | ||
and <code>Finalizer</code> methods are not executed).</p> | ||
<p>The rule raises an issue on <code>throw</code> statements used in destructors.</p> | ||
<h3>Noncompliant code example</h3> | ||
<p>The <a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers">finalizers</a> are used to perform | ||
<a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals#unmanaged-resources">any necessary final clean-up</a> when | ||
the garbage collector is collecting a class instance. The programmer has no control over when the finalizer is called; the garbage collector decides | ||
when to call it.</p> | ||
<p>When creating a finalizer, it should never throw an exception, as there is a high risk of having the application terminated leaving unmanaged | ||
resources without a graceful cleanup.</p> | ||
<p>The rule raises an issue on <code>throw</code> statements used in a finalizer.</p> | ||
<h2>How to fix it</h2> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
class MyClass | ||
{ | ||
~MyClass() | ||
{ | ||
throw new NotImplementedException(); // Noncompliant | ||
throw new NotImplementedException(); // Noncompliant: finalizer throws an exception | ||
} | ||
} | ||
</pre> | ||
<h3>Compliant solution</h3> | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
class MyClass | ||
{ | ||
~MyClass() | ||
{ | ||
|
||
// Compliant: finalizer does not throw an exception | ||
} | ||
} | ||
</pre> | ||
<h3>Going the extra mile</h3> | ||
<p>In general object finalization can be a complex and error-prone operation and should not be implemented except within the <a | ||
href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose">dispose pattern</a>.</p> | ||
<p>When <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/unmanaged">cleaning up unmanaged resources</a>, it is | ||
recommended to implement the dispose pattern or, to cover uncalled <a | ||
href="https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose"><code>Dispose</code></a> method by the consumer, implement <a | ||
href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.safehandle"><code>SafeHandle</code></a>.</p> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> <a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers">Finalizers (destructors)</a> </li> | ||
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.appdomain">App Domain</a> </li> | ||
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.unhandledexception">AppDomain.UnhandledException Event</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals">Fundamentals of garbage | ||
collection</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/unmanaged">Cleaning up unmanaged resources</a> | ||
</li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose">Implement a Dispose | ||
method</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.safehandle"><code>SafeHandle</code> | ||
Class</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose"><code>IDisposable.Dispose</code> Method</a> | ||
</li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers">Finalizers | ||
(destructors)</a> </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
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,16 +1,40 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>There is no good reason to create a new object to not do anything with it. Most of the time, this is due to a missing piece of code and so could | ||
lead to an unexpected behavior in production.</p> | ||
<p>If it was done on purpose because the constructor has side-effects, then that side-effect code should be moved into a separate, static method and | ||
called directly.</p> | ||
<h3>Noncompliant code example</h3> | ||
<pre> | ||
if (x < 0) | ||
new ArgumentException("x must be nonnegative"); | ||
<p>Creating objects that are not used is a vulnerability that can lead to unexpected behavior.</p> | ||
<p>If this was done intentionally due to side effects in the object’s constructor, the code should be moved to a dedicated method.</p> | ||
<h2>How to fix it</h2> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
public void Method(MyObject myObject) | ||
{ | ||
if (myObject is null) | ||
{ | ||
new MyObject(); // Noncompliant | ||
} | ||
|
||
if (myObject.IsCorrupted) | ||
{ | ||
new ArgumentException($"{nameof(myObject)} is corrupted"); // Noncompliant | ||
} | ||
|
||
// ... | ||
} | ||
</pre> | ||
<h3>Compliant solution</h3> | ||
<pre> | ||
if (x < 0) | ||
throw new ArgumentException("x must be nonnegative"); | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
public void Method(MyObject myObject) | ||
{ | ||
if (myObject is null) | ||
{ | ||
myObject = new MyObject(); // Compliant | ||
} | ||
|
||
if (myObject.IsCorrupted) | ||
{ | ||
throw new ArgumentException($"{nameof(myObject)} is corrupted"); // Compliant | ||
} | ||
|
||
// ... | ||
} | ||
</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,17 +1,27 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>Creating a new <code>Exception</code> without actually throwing it is useless and is probably due to a mistake.</p> | ||
<h3>Noncompliant code example</h3> | ||
<pre> | ||
<p>Creating a new <a href="https://learn.microsoft.com/en-us/dotnet/api/system.exception"><code>Exception</code></a> without actually throwing does | ||
not achieve the intended purpose.</p> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
if (x < 0) | ||
{ | ||
new ArgumentException("x must be nonnegative"); | ||
new ArgumentException("x must be nonnegative"); | ||
} | ||
</pre> | ||
<h3>Compliant solution</h3> | ||
<pre> | ||
<p>Ensure to throw the <code>Exception</code> with a <a | ||
href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/exception-handling-statements#the-throw-statement"><code>throw</code> | ||
statement</a>.</p> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
if (x < 0) | ||
{ | ||
throw new ArgumentException("x must be nonnegative"); | ||
throw new ArgumentException("x must be nonnegative"); | ||
} | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.exception"><code>Exception</code> Class</a> </li> | ||
<li> Microsoft Learn - <a | ||
href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/exception-handling-statements">Exception-handling statements - | ||
<code>throw</code>, <code>try-catch</code>, <code>try-finally</code>, and <code>try-catch-finally</code></a> </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
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
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,32 +1,49 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>The Finalize method is used to perform cleanup operations on unmanaged resources held by the current object before the object is destroyed. The | ||
method is protected and therefore is accessible only through this class or through a derived class. When you override <code>Finalize</code>, you | ||
should never throw an exception in it, as you are risking having your application terminated without a graceful cleanup.</p> | ||
<p>If <code>Finalize</code> or an override of <code>Finalize</code> throws an exception and the runtime is not hosted by an application that overrides | ||
the default policy and the behavior of the <code>UnhandledExceptionEventHandler</code>, then the runtime terminates the process immediately without | ||
graceful cleanup (<code>finally</code> blocks and <code>Finalizer</code> methods are not executed).</p> | ||
<p>The rule reports on throw statements used in finalizers.</p> | ||
<h3>Noncompliant code example</h3> | ||
<p>The <a href="https://learn.microsoft.com/en-us/dotnet/api/system.object.finalize">Finalize methods</a> are used to perform <a | ||
href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals#unmanaged-resources">any necessary final clean-up</a> when the | ||
garbage collector is collecting a class instance. The programmer has no control over when the Finalize method is called; the garbage collector decides | ||
when to call it.</p> | ||
<p>When creating a Finalize method, it should never throw an exception, as there is a high risk of having the application terminated leaving unmanaged | ||
resources without a graceful cleanup.</p> | ||
<p>The rule raises an issue on <code>throw</code> statements used in a Finalize method.</p> | ||
<h2>How to fix it</h2> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
Class MyClass | ||
Protected Overrides Sub Finalize() | ||
Throw New NotImplementedException() ' Noncompliant | ||
Throw New NotImplementedException() ' Noncompliant: Finalize method throws an exception | ||
End Sub | ||
End Class | ||
</pre> | ||
<h3>Compliant solution</h3> | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
Class MyClass | ||
Protected Overrides Sub Finalize() | ||
|
||
' Noncompliant: Finalize method does not throw an exception | ||
End Sub | ||
End Class | ||
</pre> | ||
<h3>Going the extra mile</h3> | ||
<p>In general object finalization can be a complex and error-prone operation and should not be implemented except within the <a | ||
href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose">dispose pattern</a>.</p> | ||
<p>When <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/unmanaged">cleaning up unmanaged resources</a>, it is | ||
recommended to implement the dispose pattern or, to cover uncalled <a | ||
href="https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose"><code>Dispose</code></a> method by the consumer, implement <a | ||
href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.safehandle"><code>SafeHandle</code></a>.</p> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.object.finalize">Object.Finalize method</a> </li> | ||
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.appdomain">App Domain</a> </li> | ||
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.unhandledexception">AppDomain.UnhandledException Event</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals">Fundamentals of garbage | ||
collection</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/unmanaged">Cleaning up unmanaged resources</a> | ||
</li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose">Implement a Dispose | ||
method</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.safehandle"><code>SafeHandle</code> | ||
Class</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose"><code>IDisposable.Dispose</code> Method</a> | ||
</li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.object.finalize">Object.Finalize method</a> </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
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
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
Oops, something went wrong.