rspec/rules/S2551/vbnet/rule.adoc
2023-06-08 15:36:00 +02:00

37 lines
1.1 KiB
Plaintext

include::../why-dotnet.adoc[]
The following objects are considered as shared resources:
* a reference to https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me[Me]: if the instance is publicly accessibly, the lock might be shared
* a https://learn.microsoft.com/en-us/dotnet/api/system.type[Type] object: if the type class is publicly accessibly, the lock might be shared
* a https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/string-data-type[string] literal or instance: if any other part of the program uses the same string, the lock is shared because of interning
== How to fix it
=== Code examples
==== Noncompliant code example
[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Public Sub MyLockingMethod()
SyncLock Me 'Noncompliant
' ...
End SyncLock
End Sub
----
==== Compliant solution
[source,vbnet,diff-id=1,diff-type=compliant]
----
Private lockObj As New Object()
Public Sub MyLockingMethod()
SyncLock lockObj
' ...
End SyncLock
End Sub
----
include::../resources-dotnet.adoc[]
include::../rspecator.adoc[]