rspec/rules/S3532/csharp/rule.adoc

103 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
The ``++default++`` clause should take appropriate action. Having an empty ``++default++`` is a waste of keystrokes.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
enum Fruit
{
Apple,
Orange,
Banana
}
void PrintName(Fruit fruit)
{
switch(fruit)
{
case Fruit.Apple:
Console.WriteLine("apple");
break;
default: //Noncompliant
break;
}
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
enum Fruit
{
Apple,
Orange,
Banana
}
void PrintName(Fruit fruit)
{
switch(fruit)
{
case Fruit.Apple:
Console.WriteLine("apple");
break;
default:
throw new NotSupportedException();
}
}
----
or
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
void PrintName(Fruit fruit)
{
switch(fruit)
{
case Fruit.Apple:
Console.WriteLine("apple");
break;
}
}
----
=== Exceptions
2021-04-28 16:49:39 +02:00
``++default++`` clauses containing only a comment are ignored with the assumption that they are empty on purpose and the comment documents why.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this empty "default" clause.
=== Highlighting
the whole default clause
'''
== Comments And Links
(visible only on this page)
=== relates to: S108
=== relates to: S131
=== on 5 Feb 2016, 16:42:33 Ann Campbell wrote:
Normally RSPEC-131 would be expected to cover this case, but in C# that rule is thought to make too much noise, while at the same time, there is still a desire to flag and eliminate empty ``++default++``s
endif::env-github,rspecator-view[]