Skip to content

[Feature] Enum InvalidCastException analysis #215

Open
@jzabroski

Description

@jzabroski

Placeholder to fully describe positive and negative test cases.

public enum ABEnum { A = 0, B = 1}
public class Test
{
  public void TestAB()
  {
    foreach (var item in Enum.GetValues(typeof(ABEnum)))
    {
      Console.WriteLine(((Enum)item).GetDisplayName()); // Feature logged in: https://github.com/DotNetAnalyzers/ReflectionAnalyzers/issues/214
                     // ^^^^^^^^^^^^ bonus: don't raise Possible InvalidCastException if typeof(ABEnum) is an enum defined as an int64 sub-class (default scenario).
    }
  }
}

Ideally, the code will recommend the correct cast to the appropriate integer size. For example, if the lvalue being assigned to is an int, and the enum is a long, it will recommend a down-cast. If the lvalue being assigned to is a long, and the enum is a short, there will be no loss of precision but it may make sense to warn low severity about data type mismatch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions