Skip to content

Suggest Enum.HasFlag on modern frameworks. #55455

Closed

Description

@teo-tsirpanis

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

  1. ghost added
    untriaged
    on Jul 10, 2021
  2. MichalPetryka commented on Jul 10, 2021

    @MichalPetryka
    Contributor

    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.

  3. jkotas commented on Jul 10, 2021

    @jkotas
    Member

    intuitively

    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.

  4. added
    code-analyzer
    on Jul 10, 2021
  5. teo-tsirpanis commented on Jul 10, 2021

    @teo-tsirpanis
    ContributorAuthor

    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.

  6. jkotas commented on Jul 10, 2021

    @jkotas
    Member

    it used to do some expensive boxings and type checks, until it got optimized in .NET Core 2.1.

    Note that this optimization is not guaranteed to kick in. The optimization won't kick in Debug builds, in tier 0 JIT, or when the method is too big.

  7. teo-tsirpanis commented on Jul 10, 2021

    @teo-tsirpanis
    ContributorAuthor

    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.

  8. ghost locked as resolved and limited conversation to collaborators on Aug 9, 2021
  9. removed
    untriaged
    on Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtimecode-analyzer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions