rspec/rules/S4070/csharp/rule.adoc

76 lines
1.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
This rule raises an issue when an externally visible enumeration is marked with ``++FlagsAttribute++`` and one, or more, of its values is not a power of 2 or a combination of the other defined values.
=== 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
----
using System;
namespace MyLibrary
{
[Flags]
2021-04-28 16:49:39 +02:00
public enum Color // Noncompliant, Orange is neither a power of two, nor a combination of any of the defined values
{
None = 0,
Red = 1,
Orange = 3,
Yellow = 4
}
}
----
=== 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
----
using System;
namespace MyLibrary
{
public enum Color // Compliant - no FlagsAttribute
2021-04-28 16:49:39 +02:00
{
None = 0,
Red = 1,
Orange = 3,
Yellow = 4
}
[Flags]
2021-04-28 16:49:39 +02:00
public enum Days
{
None = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
All = Monday| Tuesday | Wednesday | Thursday | Friday // Compliant - combination of other values
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove the "FlagsAttribute" from this enum.
=== Highlighting
Primary: Enum declaration
Secondaries: Enum values that are raising the issue
endif::env-github,rspecator-view[]