False postive on S4070: Enum is not regonized as set of flags

If we look to the following code:

[Flags] // FP: this is a flags enum
public enum SvoFeatures
{
    Field = /*...............*/ 0x00001,
    GetHashCode = /*.........*/ 0x00002,
    IsEmpty = /*.............*/ 0x00004,
    IsUnknown = /*...........*/ 0x00008,
    IEquatable = /*..........*/ 0x00010,
    EqualsSvo = /*...........*/ 0x00020,
    ISerializable = /*.......*/ 0x00080,
    IXmlSerializable = /*....*/ 0x00100,
    IJsonSerializable = /*...*/ 0x00200,
    IFormattable = /*........*/ 0x00400,
    IComparable = /*.........*/ 0x00800,
    ComparisonOperators = /*.*/ 0x01000,
    Parsing = /*.............*/ 0x02000,
    Validation = /*..........*/ 0x04000,
    All = /*.................*/ 0x07FFF,

    Structure = Field | IsEmpty | IsUnknown,
    EqualsSvoAndGetHashCode = EqualsSvo | GetHashCode,
    IsEmptyOrUnknown = IsEmpty | IsUnknown,
    Default = All ^ ComparisonOperators ^ Validation,
    Continuous = All ^ IsEmpty ^ IsUnknown ^ Validation,

    Utf8 = /*................*/ 0x08000,
}

Reported by SonarAnalyzer.CSharp v10.7.0.110445

Than S4070 states that this is a none-flags enum. It does so because All (which in this case does contain all except the last one, this is on purpose) is not a base2 value. If Allis removed, or defined as an or operation of all the previous 14 values, the rule would be satisfied. I would argue that in this case having an or on 14 values is not making things easier to read, and that this should be a false positive.

Note that if I change the value of All to 0x0FFFF, containing (actually all) values, the rule is also not satisified.

Hello @Corniel,

I don’t think this is a false positive.
If you change the value of All to 0x07FBF the issue disappears.
Or you are maybe missing an enum member for 0x0040.

For enum member that are not power of 2, we check if it is a combination of the other existing values. In this case, with the missing 0x0040, All is incorrect in a Flag enum.

Let me know if it solves your issue.

Well, that is clever!

I think that it would be nice if the message could tell that on of its flags was not specified.

1 Like

That’s a good suggestion!
I will take note so we can discuss it in the future.

Have a nice day!

1 Like