rspec/rules/S3265/csharp/rule.adoc

61 lines
2.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2023-06-13 11:36:14 +02:00
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum[Enumerations] are commonly used to identify distinct elements from a set of values.
2021-04-28 16:49:39 +02:00
2023-06-13 11:36:14 +02:00
However, they can also serve as https://en.wikipedia.org/wiki/Bit_field[bit flags], enabling bitwise operations to combine multiple elements within a single value.
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
----
2023-06-13 11:36:14 +02:00
// Saturday = 0b00100000, Sunday = 0b01000000, weekend = 0b01100000
var weekend = Days.Saturday | Days.Sunday; // Combining elements
2021-04-28 16:49:39 +02:00
----
2023-06-13 11:36:14 +02:00
When enumerations are used as bit flags, it is considered https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum#enumeration-types-as-bit-flags[good practice] to annotate the enum type with the https://learn.microsoft.com/en-us/dotnet/api/system.flagsattribute[FlagsAttribute]:
2023-06-13 11:36:14 +02:00
[source,csharp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
enum Permissions
2023-06-13 11:36:14 +02:00
{
2021-04-28 16:49:39 +02:00
None = 0,
2023-06-13 11:36:14 +02:00
Read = 1,
Write = 2,
2021-04-28 16:49:39 +02:00
Execute = 4
}
2023-06-13 11:36:14 +02:00
2021-04-28 16:49:39 +02:00
// ...
2023-06-13 11:36:14 +02:00
var x = Permissions.Read | Permissions.Write; // Noncompliant: enum is not annotated with [Flags]
2021-04-28 16:49:39 +02:00
----
2023-06-13 11:36:14 +02:00
The `FlagsAttribute` explicitly marks an enumeration as bit flags, making it clear that it uses bit fields and is intended to be used as flags.
2023-06-13 11:36:14 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
----
2023-06-13 11:36:14 +02:00
[Flags]
enum Permissions
{
None = 0,
2023-06-13 11:36:14 +02:00
Read = 1,
Write = 2,
Execute = 4
}
2023-06-13 11:36:14 +02:00
// ...
2023-06-13 11:36:14 +02:00
var x = Permissions.Read | Permissions.Write; // Compliant: enum is annotated with [Flags]
----
2023-06-13 11:36:14 +02:00
Additionally, adding the `FlagsAttribute` to the enumeration enable a https://learn.microsoft.com/en-us/dotnet/api/system.flagsattribute#examples[better string representation] when using the https://learn.microsoft.com/en-us/dotnet/api/system.enum.tostring[Enum.ToString] method.
2023-06-13 11:36:14 +02:00
== Resources
2023-06-13 11:36:14 +02:00
=== Documentation
2023-06-13 11:36:14 +02:00
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum[Enumeration in C#]
** https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum#enumeration-types-as-bit-flags[Enumeration types as bit flags]
* https://learn.microsoft.com/en-us/dotnet/api/system.flagsattribute[FlagsAttribute class]
* https://learn.microsoft.com/en-us/dotnet/api/system.enum.tostring[Enum.ToString method]
* https://en.wikipedia.org/wiki/Bit_field[Bit field - Wikipedia]
2023-06-13 11:36:14 +02:00
include::../rspecator.adoc[]