Skip to content

[Feature] Enum.GetValues(typeof(non-Enum)) warning #212

Open
@jzabroski

Description

@jzabroski
public class AB { }
public enum ABEnum { A = 0, B = 1}
public class Test
{
  public void TestAB()
  {
    foreach (var item in Enum.GetValues(typeof(AB))) // should warn here - syntactically valid C# but will blow up at run-time.  Caused due to developer typing AB instead of 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).
    }
  }
}

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