Open alexangas opened 6 years ago
I was thinking of this as well. There are according to me, 3 scenario's that are none-compliant, if you use Enum.HasFlag(Enum)
:
FlagsAttribute
Like this code:
public class HasFlag
{
[Flags]
public enum Flagged
{
None = 0,
UnionJack = 1,
StarSpangledBanner = 2,
}
public void Test(Flagged flags, LoaderOptimization optimization)
{
var noFlag = optimization.HasFlag(LoaderOptimization.MultiDomain); // none-compliant. LoaderOptimization is not a flags enum.
var hasNone = flags.HasFlag(Flagged.None); //none-compliant, None is not a flag value.
var hasOther = flags.HasFlag(LoaderOptimization.SingleDomain); // not-compliant, enum types don't match
var hasUS = flags.HasFlag(Flagged.StarSpangledBanner); // compliant
}
}
Description
Where an attempt is made to check if an enum value has a flag set, then SonarC# should verify that this can be done correctly.
This issue suggests a new rule to cover this, based off these remarks in MS API documentation.
Repro steps
Given one of the following code patterns is used to check if a flag is set:
Then this rule would be triggered if the target enum declaration does not have the FlagsAttribute, or if one of the values in the enum declaration is zero or not a power of two.
Related example problematic enum declaration:
Related information