rspec/rules/S2302/vbnet/rule.adoc

53 lines
1.5 KiB
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
Because parameter names could be changed during refactoring, they should not be spelled out literally in strings. Instead, use ``++NameOf()++``, and the string that's output will always be correct.
2020-06-30 12:48:07 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule raises an issue when any string in the ``++Throw++`` statement is an exact match for the name of one of the method parameters.
2020-06-30 12:48:07 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,vbnet]
2020-06-30 12:48:07 +02:00
----
Public Sub DoSomething(param As Integer, secondParam As String)
If (param < 0)
Throw New Exception("param") ' Noncompliant
End If
If secondParam is Nothing
Throw New Exception("secondParam should be valid") ' Noncompliant
End If
End Sub
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,vbnet]
2020-06-30 12:48:07 +02:00
----
Public Sub DoSomething(param As Integer, secondParam As String)
If (param < 0)
Throw New Exception(NameOf(param))
End If
If secondParam is Nothing
Throw New Exception($"{NameOf(secondParam)} should be valid")
End If
End Sub
----
== Exceptions
* The rule doesn't raise any issue when using VB.NET < 14.0.
2021-01-27 13:42:22 +01:00
* When the parameter name is contained in a sentence inside the ``++Throw++`` statement string, the rule will raise an issue only if the parameter name is at least 5 characters long. This is to avoid false positives.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]