Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jzabroski opened this issue Apr 26, 2019 · 4 comments
Open

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

jzabroski opened this issue Apr 26, 2019 · 4 comments

Comments

@jzabroski
Copy link

jzabroski commented Apr 26, 2019

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).
    }
  }
}
@JohanLarsson
Copy link
Collaborator

For the bonus it is a separate analyzer/diagnostic id, create a separate issue for it if you have time.

@jzabroski
Copy link
Author

@JohanLarsson Fair enough. Should there be separate analyze/diagnostic id for Enum.GetNames and other static methods introduced in .NET 4?

@JohanLarsson
Copy link
Collaborator

I usually err on making too many diagnostics as it enables configuration via ruleset. For issues my preference is as small and many as possible. Have found it fastest to work with. If you are interested in trying your hand at writing analyzers for some of them just leave a note. Ask questions in chat if you happen to get stuck.

@jzabroski
Copy link
Author

@JohanLarsson Thanks, I would like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants