Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix bug where enums with underlying types would cause a crash #351

Merged
merged 5 commits into from
Sep 13, 2024
Merged

Fix bug where enums with underlying types would cause a crash #351

merged 5 commits into from
Sep 13, 2024

Conversation

Genbox
Copy link
Contributor

@Genbox Genbox commented Sep 9, 2024

I've added a unit test to demonstrate the issue. The pattern-based check in EnumWithoutDefaultValueAnalyzer.cs:49 only works when the enum is using its default underlying type (int). When it is any other numerical type (byte, short, ushort, uint, long or ulong) it crashes due to an invalid cast.

Convert.ToULong() works because it uses the IConvertible interface on the numerical types to convert between values, and using ToUlong() instead of f.ex. ToInt() means we don't have to check for all possible types, since we are only interested in if the value is 0.

@Genbox
Copy link
Contributor Author

Genbox commented Sep 9, 2024

I just tested with negative values and it does not work.

Add test for negative value + different underlying type
Copy link
Owner

@Vannevelj Vannevelj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find! I've left a few comments to address in the unit tests and solution looks good to me otherwise.

@Vannevelj Vannevelj merged commit da08e53 into Vannevelj:master Sep 13, 2024
5 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants