You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The System.Enum type has a HasFlag method that allows easily and intuitively checking if a bit is set on enums with the [Flags] attribute, but it has been historically avoided because on it used to do some expensive boxings and type checks, until it got optimized in .NET Core 2.1.
The problem is that few people know that it's now fine to use this method without fears of performance issues, which is why I propose an analyzer that would warn on bitwise tests on enum flags and offer to fix them to use Enum.HasFlag.
How it would work
The analyzer would convert this
to that
(x & MyEnum.Foo) == MyEnum.Foo
x.HasFlag(MyEnum.Foo)
(x & MyEnum.Foo) != MyEnum.Foo
!x.HasFlag(MyEnum.Foo)
(x & MyEnum.Foo) == 0
!x.HasFlag(MyEnum.Foo)
(x & MyEnum.Foo) != 0
x.HasFlag(MyEnum.Foo)
(x & (MyEnum) 8) == 0
the analyzer would not trigger
Supported frameworks
To avoid any performance regressions, the analyzer would be enabled when the project exclusively targets at least .NET Core 2.1 or .NET Standard 2.1 (I don't know whether HasFlag is optimized on Xamarin/Unity/UWP). It would not be enabled on projects targeting .NET Standard 2.0 because they might be used from a .NET Framework project.
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
Checks against 0 have a different behavior from HasFlag when MyEnum.Foo has more than 1 bit set, HasFlag checks if all bits are set then and != 0 checks if any bit is set.
It may be a personal style choice. I find (x & MyEnum.Foo) != 0 and (x & MyEnum.Foo) == 0 to be more intuitive and idiomatic than HasFlag. It is a few characters shorter too.
Checks against 0 have a different behavior from HasFlag when MyEnum.Foo has more than 1 bit set, HasFlag checks if all bits are set then and != 0 checks if any bit is set.
Okay @MichalPetryka the analyzer could work only when HasFlag is passed a flag with only one bit set.
I see, if that optimization is not guaranteed, there is no reason to encourage moving to HasFlag en masse, especially since there are many kinds of enum bitwise AND check depending on the exact intention, as Michal said.
Activity
MichalPetryka commentedon Jul 10, 2021
Checks against 0 have a different behavior from HasFlag when MyEnum.Foo has more than 1 bit set, HasFlag checks if all bits are set then and != 0 checks if any bit is set.
jkotas commentedon Jul 10, 2021
It may be a personal style choice. I find
(x & MyEnum.Foo) != 0and(x & MyEnum.Foo) == 0to be more intuitive and idiomatic thanHasFlag. It is a few characters shorter too.teo-tsirpanis commentedon Jul 10, 2021
Okay @MichalPetryka the analyzer could work only when
HasFlagis passed a flag with only one bit set.jkotas commentedon Jul 10, 2021
Note that this optimization is not guaranteed to kick in. The optimization won't kick in
Debugbuilds, in tier 0 JIT, or when the method is too big.teo-tsirpanis commentedon Jul 10, 2021
I see, if that optimization is not guaranteed, there is no reason to encourage moving to
HasFlagen masse, especially since there are many kinds of enum bitwise AND check depending on the exact intention, as Michal said.