rspec/rules/S3928/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

70 lines
2.3 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

== Why is this an issue?
Some constructors of the ``++ArgumentException++``, ``++ArgumentNullException++``, ``++ArgumentOutOfRangeException++`` and ``++DuplicateWaitObjectException++`` classes must be fed with a valid parameter name. This rule raises an issue in two cases:
* When this parameter name doesn't match any existing ones.
* When a call is made to the default (parameterless) constructor
=== Noncompliant code example
[source,csharp]
----
public void Foo(Bar a, int[] b)
{
throw new ArgumentException(); // Noncompliant
throw new ArgumentException("My error message", "c"); // Noncompliant
throw new ArgumentException("My error message", "c", innerException); // Noncompliant
throw new ArgumentNullException("c"); // Noncompliant
throw new ArgumentNullException("My error message","c"); // Noncompliant
throw new ArgumentOutOfRangeException("c");
throw new ArgumentOutOfRangeException("c", "My error message"); // Noncompliant
throw new ArgumentOutOfRangeException("c",b,"My error message"); // Noncompliant
throw new DuplicateWaitObjectException("c", "My error message"); // Noncompliant
}
----
=== Compliant solution
[source,csharp]
----
public void Foo(Bar a, Bar b)
{
throw new ArgumentException("My error message", "a");
throw new ArgumentException("My error message", "b", innerException);
throw new ArgumentNullException("a");
throw new ArgumentNullException(nameof(a));
throw new ArgumentNullException("a", "My error message");
throw new ArgumentOutOfRangeException("b");
throw new ArgumentOutOfRangeException("b", "My error message");
throw new ArgumentOutOfRangeException("b",b,"My error message");
throw new DuplicateWaitObjectException("b", "My error message");
}
----
=== Exceptions
The rule won't raise an issue if the parameter name is not a constant value (inline declaration, nameof() or const variable).
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Use a constructor overload that allows a more meaningful exception message to be provided.
* The parameter name '{0}' is not declared in the argument list.
* ArgumentException constructor arguments have been inverted.
=== Highlighting
The string literal supposed to be the parameter name
endif::env-github,rspecator-view[]