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

Warn about negating unsigned (uint/ulong etc) #891

Open
HuskyNator opened this issue Feb 11, 2023 · 0 comments
Open

Warn about negating unsigned (uint/ulong etc) #891

HuskyNator opened this issue Feb 11, 2023 · 0 comments

Comments

@HuskyNator
Copy link

Warn when an unsigned value is negated.
Example from a bug I had:

uint size = 5u;
uint offset = -size // 4294967291
int offset = -size; // -5
float offset = -size; // 4.29497e+09
float offset = -5u; // Error: cannot implicitly convert expression `4294967291u` of type `uint` to `float`

Unsigned arithmetic is full of pitfalls. Even checked casts seem inconsistent.

uint(-5u); // 4294967291;
int(-5u); // -5;
int(4294967291) // Error: cannot implicitly convert expression `4294967291L` of type `long` to `int`
int(4294967291u) // -5;

I'm not even sure why this happens when using this way of casting:

uint a = -5u; // 4294967291
float(-a); // 4.29497e+09
float(-5u); // Error: cannot implicitly convert expression `4294967291u` of type `uint` to `float`
cast(float) -5u // 4.29497e+09
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

1 participant