-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
invalid_upcast_comparisons gives false positive on u8 to i8 wrapping cast #886
Comments
I think this may be a flaw in the design of this lint. I see in the tests for this lint:
So it seems this was intended behavior. However, this isn't a correct warning. A |
I think avoiding implicit wrapping is the point of the lint. |
I think the warning message in this case should be different, then. The error message, "this expression is always false", is misleading in this case. (I also think that should be an |
On the other hand, I guess I can't think of a case where this would be desirable/necessary to use in an if statement/comparison... but I still think the error message may need to be modified for the unsigned-to-signed wraps. |
I went back and found the original source that caused this warning:
This is equivalent to:
So while the latter makes the intention more clear, the unsigned-to-signed wrap appears to be a legitimate optimization that prevents an additional comparison. With this in mind, my opinion is that unsigned-to-signed casting should be moved to a separate, allow-by-default lint. |
@shssoichiro you can also use a match and see if rustc produces good code: match s.as_bytes()[index] {
0...0x7F | 0xC0...0xFF => /* true action */,
_ => /* false action */
} |
we should probably Allow-ify the lint or fix it |
We do have the |
This seems to have been fixed: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6d192a511daa0151d7afa0eb2f68e536 |
Given this minimal test case:
Clippy prints the warning:
This is incorrect, as
new
, if greater than 127, will wrap around to -128 as specified in RFC 560.The text was updated successfully, but these errors were encountered: