rspec/rules/S2302/csharp/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

65 lines
1.6 KiB
Plaintext

== Why is this an issue?
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.
This rule raises an issue when a string in the ``++throw++`` statement contains the name of one of the method parameters.
=== Noncompliant code example
[source,csharp]
----
void DoSomething(int someParameter, string anotherParam)
{
if (someParameter < 0)
{
throw new ArgumentException("Bad argument", "someParameter"); // Noncompliant
}
if (anotherParam == null)
{
throw new Exception("anotherParam should not be null"); // Noncompliant
}
}
----
=== Compliant solution
[source,csharp]
----
void DoSomething(int someParameter)
{
if (someParameter < 0)
{
throw new ArgumentException("Bad argument", nameof(someParameter));
}
if (anotherParam == null)
{
throw new Exception($"{nameof(anotherParam)} should not be null");
}
}
----
=== Exceptions
* The rule doesn't raise any issue when using C# < 6.0.
* 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[]