Modify rule S2997: LaYC format (#2353)
This commit is contained in:
parent
ee50358ea1
commit
071f93aafe
@ -24,5 +24,5 @@
|
||||
"defaultQualityProfiles": [
|
||||
"Sonar way"
|
||||
],
|
||||
"quickfix": "unknown"
|
||||
"quickfix": "infeasible"
|
||||
}
|
||||
|
@ -1,34 +1,45 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Typically you want to use ``++using++`` to create a local ``++IDisposable++`` variable; it will trigger disposal of the object when control passes out of the block's scope. The exception to this rule is when your method returns that ``++IDisposable++``. In that case ``++using++`` disposes of the object before the caller can make use of it, likely causing exceptions at runtime. So you should either remove ``++using++`` or avoid returning the ``++IDisposable++``.
|
||||
When you use a https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using[`using` statement], the goal is to ensure the correct disposal of an https://learn.microsoft.com/en-us/dotnet/api/system.idisposable[`IDisposable`] instance when the control leaves the `using` statement block.
|
||||
|
||||
If you return that `IDisposable` instance inside the block, `using` will dispose it before the caller can use it, likely causing exceptions at runtime. You should either remove `using` statement or avoid returning the `IDisposable` in the `using` statement block.
|
||||
|
||||
=== Noncompliant code example
|
||||
== How to fix it
|
||||
|
||||
[source,text]
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,csharp,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
public FileStream WriteToFile(string path, string text)
|
||||
{
|
||||
using (var fs = File.Create(path)) // Noncompliant
|
||||
using (var fs = File.Create(path)) // Noncompliant: 'fs' is disposed at the end of the using scope
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(text);
|
||||
fs.Write(bytes, 0, bytes.Length);
|
||||
return fs;
|
||||
return fs;
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
=== Compliant solution
|
||||
==== Compliant solution
|
||||
|
||||
[source,text]
|
||||
[source,csharp,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
public FileStream WriteToFile(string path, string text)
|
||||
{
|
||||
var fs = File.Create(path);
|
||||
var bytes = Encoding.UTF8.GetBytes(text);
|
||||
fs.Write(bytes, 0, bytes.Length);
|
||||
return fs;
|
||||
return fs; // Compliant: 'fs' is not disposed once the end of the scope is reached and the caller can use it
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
|
||||
=== Documentation
|
||||
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using[using statement - ensure the correct use of disposable objects]
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.idisposable[IDisposable]
|
||||
|
Loading…
x
Reference in New Issue
Block a user