Skip to content

RCS1096 incorrectly identifies/converts HasFlag logic #720

@qbert09

Description

@qbert09

Product and Version Used:
When running the conversion for RCS1096 it incorrectly converts the HasFlag logic. The current conversion treats composite flags as though HasFlag is identifying that any of the bits is set, where as in reality HasFlag will only return true if all bits are set.

Here is a sample program to demonstrate:
https://rextester.com/RENET59796

Steps to Reproduce:
given an enumeration and a variable x:

public enum MyFlags
{
    A = 1,
    B = 2,
    C = 4,
    D = 8,
    DA = D | A
}

var x = MyFlags.A | MyFlags.C;

if I apply the fix for RCS1096 and code:

if(x.HasFlag(MyFlags.A|MyFlags.C))
{
    //...
}

I get:

if((x & (MyFlags.A|MyFlags.C)) != 0)
{
    //...
}

When I would expect:

if((x & (MyFlags.A|MyFlags.C)) == (MyFlags.A|MyFlags.C))
{
    //...
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions