rspec/rules/S3343/csharp/rule.adoc

80 lines
3.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2023-06-28 13:30:29 +02:00
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/caller-information[Caller information attributes] provide a way to get information about the caller of a method through https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments[optional] parameters. But they only work right if their values aren't provided explicitly. So if you define a method with caller info attributes in the middle of the parameter list, the caller is forced to use named arguments if they want to use the method properly.
2021-04-28 16:49:39 +02:00
2023-06-28 13:30:29 +02:00
This rule raises an issue when the following attributes are used on parameters before the end of the parameter list:
2023-06-28 13:30:29 +02:00
* https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerfilepathattribute[CallerFilePathAttribute]
* https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerlinenumberattribute[CallerLineNumberAttribute]
* https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callermembernameattribute[CallerMemberNameAttribute]
* https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerargumentexpressionattribute[CallerArgumentExpressionAttribute]
2021-04-28 16:49:39 +02:00
2023-06-28 13:30:29 +02:00
== How to fix it
Move the decorated parameters to the end of the parameter list.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
void TraceMessage([CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0,
2023-06-28 13:30:29 +02:00
string message = null) // Noncompliant: decorated parameters appear before "message" parameter
2021-04-28 16:49:39 +02:00
{
/* ... */
}
----
2023-06-28 13:30:29 +02:00
==== Compliant solution
2023-06-28 13:30:29 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
void TraceMessage(string message = null,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
2023-06-28 13:30:29 +02:00
[CallerLineNumber] int lineNumber = 0)
2021-04-28 16:49:39 +02:00
{
/* ... */
}
----
2023-06-28 13:30:29 +02:00
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/caller-information[Determine caller information using attributes interpreted by the C# compiler]
* https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerfilepathattribute[CallerFilePathAttribute Class]
* https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerlinenumberattribute[CallerLineNumberAttribute Class]
* https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callermembernameattribute[CallerMemberNameAttribute Class]
* https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerargumentexpressionattribute[CallerArgumentExpressionAttribute Class]
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments[Named and Optional Arguments]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Move "xxx" to the end of the parameter list.
'''
== Comments And Links
(visible only on this page)
=== on 18 Nov 2015, 10:01:25 Tamas Vajk wrote:
\[~ann.campbell.2] I modified the sample code: there can't be any parameter without a default value after parameters with default values. (So I added the ``++null++`` default value to the ``++message++``)
And also modified the last sentence. It is possible to not specify all parameters in a call, but then you force the callers of this method to use named arguments.
=== on 18 Nov 2015, 19:55:28 Ann Campbell wrote:
Okay, thanks [~tamas.vajk]
endif::env-github,rspecator-view[]