rspec/rules/S3236/rule.adoc

46 lines
1.0 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
Caller information attributes: ``++CallerFilePathAttribute++``, ``++CallerLineNumberAttribute++``, and ``++CallerArgumentExpressionAttribute++`` provide a way to get information about the caller of a method through optional parameters. But the arguments for these optional parameters are only generated if they are not explicitly defined in the call. Thus, specifying the argument values defeats the purpose of the attributes.
2020-06-30 12:48:39 +02:00
=== Noncompliant code example
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
void TraceMessage(string message,
[CallerFilePath] string filePath = null,
[CallerLineNumber] int lineNumber = 0)
{
/* ... */
}
void MyMethod()
{
TraceMessage("my message", "A.B.C.Foo.cs", 42); // Noncompliant
}
----
=== Compliant solution
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
void TraceMessage(string message,
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0)
{
/* ... */
}
void MyMethod()
{
TraceMessage("my message");
}
----
=== Exceptions
2020-06-30 12:48:39 +02:00
2021-01-27 13:42:22 +01:00
``++CallerMemberName++`` is not checked to avoid False-Positives with WPF/UWP applications.
2020-06-30 12:48:39 +02:00