Bluehill.Analyzers

BH0014, BH0015: Use ToStringFast() or HasFlagFast()

This analyzer includes the Bluehill.EnumExtensionsAttribute attribute and a corresponding source generator for generating ToStringFast and HasFlagFast extension methods for enumerations. For performance and AOT compatibility, should use ToStringFast() or HasFlagFast() extension methods instead. See the EnumExtensionsGenerator page for more details.

BH0014: Use ToStringFast() or HasFlagFast()

This rule is reported if there is already a ToStringFast() or HasFlagFast() extension method for that enumeration.

BH0015: Apply the Bluehill.EnumExtensionsAttribute to the enumeration and use ToStringFast() or HasFlagFast()

This rule is reported when the enumeration does not have the Bluehill.EnumExtensionsAttribute and the ToString() or HasFlag() method is called. Note: This is not reported if the project being called is different from the project of the enumeration (if the enumeration is in an external assembly). It is also not reported if the enumeration is a nested type and is not public or internal accessible.

Code with violation

public enum TestEnum {
    A,
    B,
    C
}

public class TestClass {
    public void TestMethod() {
        var e = TestEnum.A;
        var str = e.ToString();
        var hasFlag = e.HasFlag(TestEnum.B);
    }
}

Fixed Code

[Bluehill.EnumExtensions]
public enum TestEnum {
    A,
    B,
    C
}

public class TestClass {
    public void TestMethod() {
        var e = TestEnum.A;
        var str = e.ToStringFast();
        var hasFlag = e.HasFlagFast(TestEnum.B);
    }
}