Skip to content

Code analyzer that makes problematic equality checks compiler errors #1527

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

Open
lkomanetz opened this issue Mar 17, 2025 · 3 comments
Open

Comments

@lkomanetz
Copy link

Is your feature request related to a problem? Please describe.
I really like the idea of having equality checks where a developer goes left == right or left.Equals(right) being more than just a warning to enforce proper equality checks with decimal values.

Describe the solution you'd like
A code analyzer that looks for equality checks where they aren't using any tolerance and treat them as compiler errors.

Describe alternatives you've considered
I've thought about including FxCop and adding a rule for this purpose but with the Roslyn SDK I feel like a custom analyzer would be the way to go.

Additional context
N/A

@lipchev
Copy link
Collaborator

lipchev commented Mar 17, 2025

@lkomanetz If you're planning a PR for this, please note that I've been working (it's going to be exactly one year soon) on a version of UnitsNet (hopefully to be release as v6) that, among other things, replaces the floating point representation with a rational one, which allows as to use all the natural comparison/equality operators without worries.

Until that time, I'd advice you to keep in mind that, while there aren't any warnings emitted for the comparison operators, but they are also not safe : it's possible to come up with a situation where (a >= b) != (b<=a) is true. In practical terms, there isn't any exact way of stating that, "the length should be in the range 1m ± 0.1m" - depending on the units you're using, this may or may not work with something like 1m + 10%.

@lkomanetz
Copy link
Author

@lipchev
Do you feel like this enhancement ticket should be closed out? I did think about the <= and >= comparisons and felt that maybe a warning would've sufficed because of what you mentioned. An analyzer that sees those and goes, "yeah...not the MOST safe thing to do but we're not sure how to do this with every possible unit type so we'll warn you." With that another analyzer that goes, "you're checking equality? It's got to be in this way otherwise that's FOR SURE unsafe." That's where my mind was going.

@angularsen
Copy link
Owner

I think it can be wise to hold off until the dust settles, in particular regarding how the new rational number representation may change things.

Lots of changes coming to v6, and it's now merged into master branch, but it's not all done yet.

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

No branches or pull requests

3 participants