rspec/rules/S2551/vbnet/rule.adoc

39 lines
1.1 KiB
Plaintext
Raw Normal View History

2023-06-08 15:36:00 +02:00
include::../why-dotnet.adoc[]
2023-06-08 15:36:00 +02:00
The following objects are considered as shared resources:
2020-06-30 12:48:07 +02:00
* 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 accessible, the lock might be shared
* a https://learn.microsoft.com/en-us/dotnet/api/system.type[Type] object: if the type class is publicly accessible, 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
2021-02-02 15:02:10 +01:00
2023-06-08 15:36:00 +02:00
== How to fix it
2023-06-08 15:36:00 +02:00
=== Code examples
2020-06-30 12:48:07 +02:00
2023-06-08 15:36:00 +02:00
==== Noncompliant code example
2020-06-30 12:48:07 +02:00
2023-06-08 15:36:00 +02:00
[source,vbnet,diff-id=1,diff-type=noncompliant]
2020-06-30 12:48:07 +02:00
----
Public Sub MyLockingMethod()
SyncLock Me 'Noncompliant
' ...
End SyncLock
End Sub
----
2023-06-08 15:36:00 +02:00
==== Compliant solution
2020-06-30 12:48:07 +02:00
2023-06-08 15:36:00 +02:00
[source,vbnet,diff-id=1,diff-type=compliant]
2020-06-30 12:48:07 +02:00
----
2023-06-08 15:36:00 +02:00
Private lockObj As New Object()
Public Sub MyLockingMethod()
SyncLock lockObj
' ...
End SyncLock
End Sub
2020-06-30 12:48:07 +02:00
----
2023-06-08 15:36:00 +02:00
include::../resources-dotnet.adoc[]
include::../rspecator.adoc[]