-
Notifications
You must be signed in to change notification settings - Fork 8
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 [Flags] should warn if values not powers of 2 #213
Comments
This is starting to no longer be reflection territory but keep the issue. |
I mean, I guess that a bunch of these could go into a new EnumAnalyzers project, but unless you develop a super-package of analyzers, it loses the utility and purpose. |
We wrote a couple of analyzers for enums in Gu.Analyzers, maybe there is one checking this already, I don't remember. |
I thought there was something like this in the legacy FxCop, might have already been ported to FxCopAnalyzers. If you do create such a rule, it would be nice to have an exemption for mask values :) |
Agreed it should only check literals. |
I checked and do not see it. Here is what I see when I control+F search my web page in Chrome:
|
I'm not even sure I agree with this analyzer. There's no rule that says flags may not be mutually exclusive. For example, |
@jnm2 Rules can certainly have exceptions, but the point is to flag potentially bad code. Frankly, I agree with Don Syme (F# creator): Discriminated union types are much better, and preferred, to Enumerations. The narrow use case is for representing physical bit positions. For
For |
Except that a core use case in real-world applications is I'm not convinced it's a sign of potentially bad code. Why should everyone operate by the more narrow expectation? |
In practice, you would write something like: new KeyGesture(Key.C, (ModifierKeys.Control | ModifierKeys.Shift)) I guess someone (ahem) should open up a ticket to improve that documentation and explain best practices. |
@jzabroski I'm used to Windows Forms. KeyEventArgs has ORed keys and modifiers together since .NET Framework 1.0, I think. |
The following examples do not follow the Remarks section in System.FlagsAttribute:
As a result of this invalid configuration, ToString() returns incorrect results because the Flags values use an unsupported configuration, so the behavior is undefined/non-supported.
The text was updated successfully, but these errors were encountered: