-
Notifications
You must be signed in to change notification settings - Fork 231
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
Update S4070 to accept negative numbers #8211
Conversation
28c8b1d
to
667ca84
Compare
@antonioaversa, @sebastien-marichal what do you think? From the runtime point of view, the negative values are also acceptable if their absolute value is a power of 2. From the usage point of view, the math can get confusing but that looks to me as a separate dedicated rule. From the docs:
|
667ca84
to
c15f5a3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
I left one educational question.
: null; | ||
|
||
private static bool IsValidFlagValue(ulong? enumValue, List<ulong> allValues) => | ||
private static bool IsValidFlagValue(BigInteger? enumValue, IEnumerable<BigInteger> allValues) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Educational: Why did you change the type from List
to IEnumerable
?
Isn't there a risk of multiple enumeration if we drop the ToList
on allValues
line 58?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the method should not be aware of how it is used. If it does the iteration only once and doesn't use API specific to the collection it should use the most generic type. This in some scenarios will allow lazy evaluation, or no evaluation at all if the path is not reached.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, we should be able to use BigInteger.Abs
.
|| (value.Sign == -1 | ||
? BigInteger.Multiply(value, -1).IsPowerOfTwo | ||
: value.IsPowerOfTwo); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BigInteger.Abs(value).IsPowerOfTwo
|| (value.Sign == -1 | |
? BigInteger.Multiply(value, -1).IsPowerOfTwo | |
: value.IsPowerOfTwo); | |
|| BigInteger.Abs(value).IsPowerOfTwo |
@costin-zaharia-sonarsource The safeguard and the |
if their absolute value is a power of two.
c15f5a3
to
a0be94b
Compare
@antonioaversa I removed the safeguard. Regarding the tuple, I think that's a scenario that could not actually happen, to have an enum declaration with a member that doesn't have a location. It looks like defensive coding. |
a0be94b
to
8618a9b
Compare
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
if their absolute value is a power of two.
Fixes #7991