rspec/rules/S2222/vbnet/rule.adoc

87 lines
2.6 KiB
Plaintext
Raw Normal View History

2023-06-06 16:02:42 +02:00
include::../why-dotnet.adoc[]
2023-06-06 16:02:42 +02:00
== How to fix it
2021-12-02 13:42:27 +00:00
2023-06-06 16:02:42 +02:00
To make sure that a lock is always released correctly, you can follow one of these two methods:
2021-12-02 13:42:27 +00:00
2023-06-06 16:02:42 +02:00
* Use a https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/synclock-statement[`SyncLock`] statement with your lock object.
* Use a https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement[`Try-Finally`] statement and put the release of your lock object within a https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement#finally-block[`Finally`] block.
=== Code examples
==== Noncompliant code example
[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Class Example
Private obj As Object = New Object()
Public Sub DoSomethingWithMonitor()
2023-06-06 16:02:42 +02:00
Monitor.Enter(obj) ' Noncompliant: not all paths release the lock
If IsInitialized() Then
2023-06-06 16:02:42 +02:00
' ...
Monitor.Exit(obj)
End If
End Sub
2023-06-06 16:02:42 +02:00
End Class
----
2023-06-06 16:02:42 +02:00
[source,vbnet,diff-id=2,diff-type=noncompliant]
----
Class Example
Private lockObj As ReaderWriterLockSlim = New ReaderWriterLockSlim()
Public Sub DoSomethingWithReaderWriteLockSlim()
2023-06-06 16:02:42 +02:00
lockObj.EnterReadLock() ' Noncompliant: not all paths release the lock
If IsInitialized() Then
2023-06-06 16:02:42 +02:00
' ...
lockObj.ExitReadLock()
End If
End Sub
End Class
----
2023-06-06 16:02:42 +02:00
==== Compliant solution
2023-06-06 16:02:42 +02:00
[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Class Example
Private obj As Object = New Object()
Public Sub DoSomethingWithMonitor()
2023-06-07 15:41:34 +02:00
SyncLock obj ' Compliant: the lock will be released at the end of the SyncLock block
If IsInitialized() Then
2023-06-06 16:02:42 +02:00
' ...
End If
End SyncLock
End Sub
2023-06-06 16:02:42 +02:00
End Class
----
2023-06-06 16:02:42 +02:00
[source,vbnet,diff-id=2,diff-type=noncompliant]
----
Class Example
Private lockObj As ReaderWriterLockSlim = New ReaderWriterLockSlim()
Public Sub DoSomethingWithReaderWriteLockSlim()
2023-06-07 15:41:34 +02:00
lockObj.EnterReadLock() ' Compliant: the lock will be released in the finally block
Try
If IsInitialized() Then
2023-06-06 16:02:42 +02:00
' ...
End If
Finally
lockObj.ExitReadLock()
End Try
End Sub
End Class
----
2023-06-06 16:02:42 +02:00
include::../resources-dotnet.adoc[]
2023-06-06 16:02:42 +02:00
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/synclock-statement[`SyncLock` statement]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement#finally-block[`Finally` block]
2021-12-02 13:42:27 +00:00
2023-06-06 16:02:42 +02:00
include::../rspecator.adoc[]