-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Re-enable cast_lossless lint by default #4864
Comments
That's what the "lossless" part is. Truncation can't happen with the current types. It would lint if the types changes in the future such that truncation is possible. As such, I am personally opposed to this. |
I don't think that enabling this lint will be very popular. At best, it improves style (but not much, and it makes the code longer, too), and at worst it is seen as a false positive. |
I agree with @llogiq. IMO |
A common misconception about
|
How would that play out? Changing the signature or type of a |
I suppose violation of correctness could happen like this:
The code still compiles, so client code developers may assume the semver-incompatible changes did not affect them, but actually the code now unexpectedly truncates the value of |
I'm actually not that concerned about future changes to the code breaking it - as discussed in #4528, that may not be a strong enough signal for a correctness lint. In my view Consider the following real-world code: for j in 0..min(blur_radius, width) {
let bb = backbuf[ti + j]; // VERTICAL: ti + j * width
val_r += bb[0] as isize;
val_g += bb[1] as isize;
val_b += bb[2] as isize;
}
if blur_radius > width {
val_r += (blur_radius - height) as isize * lv[0] as isize;
val_g += (blur_radius - height) as isize * lv[1] as isize;
val_b += (blur_radius - height) as isize * lv[2] as isize;
}
// Process the left side where we need pixels from beyond the left edge
for _ in 0..min(width, blur_radius + 1) {
let bb = get_right(ri); ri += 1;
val_r += bb[0] as isize - fv[0] as isize;
val_g += bb[1] as isize - fv[1] as isize;
val_b += bb[2] as isize - fv[2] as isize;
frontbuf[ti] = [round(val_r as f32 * iarr) as u8,
round(val_g as f32 * iarr) as u8,
round(val_b as f32 * iarr) as u8];
ti += 1; // VERTICAL : ti += width, same with the other areas
} It was producing incorrect numerical results for some inputs, and I was trying to figure out why. With Here is the same code with all for j in 0..min(blur_radius, width) {
let bb = backbuf[ti + j]; // VERTICAL: ti + j * width
val_r += isize::from(bb[0]);
val_g += isize::from(bb[1]);
val_b += isize::from(bb[2]);
}
if blur_radius > width {
val_r += (blur_radius - height) as isize * isize::from(lv[0]);
val_g += (blur_radius - height) as isize * isize::from(lv[1]);
val_b += (blur_radius - height) as isize * isize::from(lv[2]);
}
// Process the left side where we need pixels from beyond the left edge
for _ in 0..min(width, blur_radius + 1) {
let bb = get_right(ri); ri += 1;
val_r += isize::from(bb[0]) - isize::from(fv[0]);
val_g += isize::from(bb[1]) - isize::from(fv[1]);
val_b += isize::from(bb[2]) - isize::from(fv[2]);
frontbuf[ti] = [round(val_r as f32 * iarr) as u8,
round(val_g as f32 * iarr) as u8,
round(val_b as f32 * iarr) as u8];
ti += 1; // VERTICAL : ti += width, same with the other areas
} Now I don't have to worry about losing data in any of the |
Although I am more concerned about the fragility of external changes, I agree that it helps readability and that |
+1 for promoting to |
I'd be in favor of |
Note that casting bools to integers is also a form of |
Closing as this has become stale (1066 days since last comment) and the PR was closed. |
The cast_lossless lint has been made pedantic following #4528, with the reasoning being "It will only become a problem if the code changes in the future".
I am a happy user of this lint, and I find that converting always-lossless casts to
from()
calls as the lint suggests clarifies the code a great deal: it makes it explicit where truncation may happen, which makes the code much easier to understand.As such, I believe this lint is a good candidate for
clippy::style
category. Perhaps its description could be revised to make the readability aspect more explicit.The text was updated successfully, but these errors were encountered: