Modify rule S2222: Update code examples diff type (#2142)
This commit is contained in:
parent
869c168284
commit
31a1f3c8f8
@ -13,11 +13,11 @@ To make sure that a lock is always released correctly, you can follow one of the
|
||||
|
||||
[source,csharp,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
class MyClass
|
||||
class MyClass
|
||||
{
|
||||
private object obj = new object();
|
||||
|
||||
public void DoSomethingWithMonitor()
|
||||
public void DoSomethingWithMonitor()
|
||||
{
|
||||
Monitor.Enter(obj); // Noncompliant: not all paths release the lock
|
||||
if (IsInitialized())
|
||||
@ -31,11 +31,11 @@ class MyClass
|
||||
|
||||
[source,csharp,diff-id=2,diff-type=noncompliant]
|
||||
----
|
||||
class MyClass
|
||||
class MyClass
|
||||
{
|
||||
private ReaderWriterLockSlim lockObj = new ReaderWriterLockSlim();
|
||||
|
||||
public void DoSomethingWithReaderWriteLockSlim()
|
||||
|
||||
public void DoSomethingWithReaderWriteLockSlim()
|
||||
{
|
||||
lockObj.EnterReadLock(); // Noncompliant: not all paths release the lock
|
||||
if (IsInitialized())
|
||||
@ -49,13 +49,13 @@ class MyClass
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,csharp,diff-id=1,diff-type=noncompliant]
|
||||
[source,csharp,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
class MyClass
|
||||
class MyClass
|
||||
{
|
||||
private object obj = new object();
|
||||
|
||||
public void DoSomethingWithMonitor()
|
||||
public void DoSomethingWithMonitor()
|
||||
{
|
||||
lock(obj) // Compliant: the lock will be released at the end of the lock block
|
||||
{
|
||||
@ -68,13 +68,13 @@ class MyClass
|
||||
}
|
||||
----
|
||||
|
||||
[source,csharp,diff-id=2,diff-type=noncompliant]
|
||||
[source,csharp,diff-id=2,diff-type=compliant]
|
||||
----
|
||||
class MyClass
|
||||
class MyClass
|
||||
{
|
||||
private ReaderWriterLockSlim lockObj = new ReaderWriterLockSlim();
|
||||
|
||||
public void DoSomethingWithReaderWriteLockSlim()
|
||||
public void DoSomethingWithReaderWriteLockSlim()
|
||||
{
|
||||
lockObj.EnterReadLock(); // Compliant: the lock will be released in the finally block
|
||||
try
|
||||
|
@ -44,14 +44,14 @@ End Class
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,vbnet,diff-id=1,diff-type=noncompliant]
|
||||
[source,vbnet,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
Class Example
|
||||
Private obj As Object = New Object()
|
||||
|
||||
Public Sub DoSomethingWithMonitor()
|
||||
SyncLock obj ' Compliant: the lock will be released at the end of the SyncLock block
|
||||
If IsInitialized() Then
|
||||
If IsInitialized() Then
|
||||
' ...
|
||||
End If
|
||||
End SyncLock
|
||||
@ -59,7 +59,7 @@ Class Example
|
||||
End Class
|
||||
----
|
||||
|
||||
[source,vbnet,diff-id=2,diff-type=noncompliant]
|
||||
[source,vbnet,diff-id=2,diff-type=compliant]
|
||||
----
|
||||
Class Example
|
||||
Private lockObj As ReaderWriterLockSlim = New ReaderWriterLockSlim()
|
||||
|
Loading…
x
Reference in New Issue
Block a user