
org.sonar.l10n.csharp.rules.csharpsquid.SwitchWithoutDefault.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-csharp-plugin Show documentation
Show all versions of sonar-csharp-plugin Show documentation
Enable analysis and reporting on C# projects.
Always having a default:
case in switch
statements is defensive programming.
It should either take appropriate action or contain a suitable comment as to why no action is taken.
The following code snippet:
int foo = 42;
switch (foo) // Non-Compliant
{
case 0:
Console.WriteLine("foo = 0");
break;
case 42:
Console.WriteLine("foo = 42");
break;
}
should be refactored into:
int foo = 42;
switch (foo) // Compliant
{
case 0:
Console.WriteLine("foo = 0");
break;
case 42:
Console.WriteLine("foo = 42");
break;
default:
throw new InvalidOperationException("Unexpected value foo = " + foo);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy