
When an include is not surrounded by empty lines, its content is inlined on the same line as the adjacent content. That can lead to broken tags and other display issues. This PR fixes all such includes and introduces a validation step that forbids introducing the same problem again.
84 lines
2.3 KiB
Plaintext
84 lines
2.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
When https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements#the-switch-statement[switch] statements have large sets of case clauses, it is usually an attempt to map two sets of data. A https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2[Dictionary] should be used instead to make the code more readable and maintainable.
|
|
|
|
=== Exceptions
|
|
|
|
This rule ignores `switch` statements over `Enum` arguments and empty, fall-through cases.
|
|
|
|
== How to fix it
|
|
|
|
Store all the cases apart from the `default` one in a dictionary and try to get the matching value by calling the https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.trygetvalue[TryGetValue] method.
|
|
|
|
=== Code examples
|
|
|
|
The example below are using the "Maximum number of case" property set to `4`.
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
public class TooManyCase
|
|
{
|
|
public int mapValues(char ch)
|
|
{
|
|
switch(ch) { // Noncompliant: 5 cases, "default" excluded, more than maximum = 4
|
|
case 'a':
|
|
return 1;
|
|
case 'b':
|
|
case 'c':
|
|
return 2;
|
|
case 'd':
|
|
return 3;
|
|
case 'e':
|
|
return 4;
|
|
case 'f':
|
|
case 'g':
|
|
case 'h':
|
|
return 5;
|
|
default:
|
|
return 6;
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
using System.Collections.Generic;
|
|
|
|
public class TooManyCase
|
|
{
|
|
Dictionary<char, int> matching = new Dictionary<char, int>()
|
|
{
|
|
{ 'a', 1 },
|
|
{ 'b', 2 },
|
|
{ 'c', 2 },
|
|
{ 'd', 3 },
|
|
{ 'e', 4 },
|
|
{ 'f', 5 },
|
|
{ 'g', 5 },
|
|
{ 'h', 5 }
|
|
};
|
|
|
|
public int mapValues(char ch)
|
|
{
|
|
int value;
|
|
if (this.matching.TryGetValue(ch, out value)) {
|
|
return value;
|
|
} else {
|
|
return 6;
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
include::../resources-dotnet.adoc[]
|
|
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements#the-switch-statement[The `switch` statement]
|
|
|
|
include::../rspecator-dotnet.adoc[]
|
|
|