-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Fix overflows in the implementation of overflowing_literals
lint's help
#135249
Fix overflows in the implementation of overflowing_literals
lint's help
#135249
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @chenyukang (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
Apply eta-reduction on map to simplify code and make the style more consistent
you may add:
in the PR description so that the two issue will be closed after PR merged. |
seems we does not find the proper type for different range of number literals , for example:
the value is |
3 | let _ = 18446744073709551614;
| ^^^^^^^^^^^^^^^^^^^^
|
= note: the literal `18446744073709551614` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
= help: consider using the type `i128` instead maybe better choice is suggesting |
@chenyukang I preserved the original behavior which is to suggest types with the same signedness unless the value fits into an unsigned number of the same size. I was focusing on fixing the overflows and outright wrong suggestions in this PR, but if you feel that it's ok to change the behavior here, I'll change it so that it always suggests the smallest fitting type. |
Changes the behavior of the `overflowing_literals` suggestion so that it always suggest the smallest type regardless of the original type size.
Done implementing that too. Hope that there was no special reason for the old, less straightforward behavior. |
@bors r+ rollup |
…als-help, r=chenyukang Fix overflows in the implementation of `overflowing_literals` lint's help This PR fixes two overflow problems that cause the `overflowing_literals` lint to behave incorrectly in some edge cases. 1. When an integer literal is between `i128::MAX` and `u128::MAX`, an overflowing `as` cast can cause the suggested type to be overly small. It's fixed by using checked type conversion and returning `u128` when it's the only choice. (Fixes rust-lang#135248) 2. When an integer literal is `i128::MIN` but is of a smaller type, an overflowing negation cause the compiler to panic in debug build. Fixed by checking the number size beforehand and `wrapping_neg`. (Fixes rust-lang#131849) Edit: extracted the type conversion part into a standalone function to separate the concern of overflowing.
Rollup of 5 pull requests Successful merges: - rust-lang#134286 (Enable `unreachable_pub` lint in core) - rust-lang#135249 (Fix overflows in the implementation of `overflowing_literals` lint's help) - rust-lang#135534 (use indirect return for `i128` and `f128` on wasm32) - rust-lang#135556 (Clarify note in `std::sync::LazyLock` example) - rust-lang#135560 (Update `compiler-builtins` to 0.1.144) r? `@ghost` `@rustbot` modify labels: rollup
…als-help, r=chenyukang Fix overflows in the implementation of `overflowing_literals` lint's help This PR fixes two overflow problems that cause the `overflowing_literals` lint to behave incorrectly in some edge cases. 1. When an integer literal is between `i128::MAX` and `u128::MAX`, an overflowing `as` cast can cause the suggested type to be overly small. It's fixed by using checked type conversion and returning `u128` when it's the only choice. (Fixes rust-lang#135248) 2. When an integer literal is `i128::MIN` but is of a smaller type, an overflowing negation cause the compiler to panic in debug build. Fixed by checking the number size beforehand and `wrapping_neg`. (Fixes rust-lang#131849) Edit: extracted the type conversion part into a standalone function to separate the concern of overflowing.
…iaskrgr Rollup of 4 pull requests Successful merges: - rust-lang#135249 (Fix overflows in the implementation of `overflowing_literals` lint's help) - rust-lang#135534 (use indirect return for `i128` and `f128` on wasm32) - rust-lang#135556 (Clarify note in `std::sync::LazyLock` example) - rust-lang#135560 (Update `compiler-builtins` to 0.1.144) r? `@ghost` `@rustbot` modify labels: rollup
…als-help, r=chenyukang Fix overflows in the implementation of `overflowing_literals` lint's help This PR fixes two overflow problems that cause the `overflowing_literals` lint to behave incorrectly in some edge cases. 1. When an integer literal is between `i128::MAX` and `u128::MAX`, an overflowing `as` cast can cause the suggested type to be overly small. It's fixed by using checked type conversion and returning `u128` when it's the only choice. (Fixes rust-lang#135248) 2. When an integer literal is `i128::MIN` but is of a smaller type, an overflowing negation cause the compiler to panic in debug build. Fixed by checking the number size beforehand and `wrapping_neg`. (Fixes rust-lang#131849) Edit: extracted the type conversion part into a standalone function to separate the concern of overflowing.
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#133720 ([cfg_match] Adjust syntax) - rust-lang#134496 (Update documentation for Arc::from_raw, Arc::increment_strong_count, and Arc::decrement_strong_count to clarify allocator requirement) - rust-lang#134754 (Implement `use` associated items of traits) - rust-lang#135249 (Fix overflows in the implementation of `overflowing_literals` lint's help) - rust-lang#135251 (Only treat plain literal patterns as short) - rust-lang#135556 (Clarify note in `std::sync::LazyLock` example) - rust-lang#135560 (Update `compiler-builtins` to 0.1.144) r? `@ghost` `@rustbot` modify labels: rollup
…als-help, r=chenyukang Fix overflows in the implementation of `overflowing_literals` lint's help This PR fixes two overflow problems that cause the `overflowing_literals` lint to behave incorrectly in some edge cases. 1. When an integer literal is between `i128::MAX` and `u128::MAX`, an overflowing `as` cast can cause the suggested type to be overly small. It's fixed by using checked type conversion and returning `u128` when it's the only choice. (Fixes rust-lang#135248) 2. When an integer literal is `i128::MIN` but is of a smaller type, an overflowing negation cause the compiler to panic in debug build. Fixed by checking the number size beforehand and `wrapping_neg`. (Fixes rust-lang#131849) Edit: extracted the type conversion part into a standalone function to separate the concern of overflowing.
…iaskrgr Rollup of 6 pull requests Successful merges: - rust-lang#133720 ([cfg_match] Adjust syntax) - rust-lang#134496 (Update documentation for Arc::from_raw, Arc::increment_strong_count, and Arc::decrement_strong_count to clarify allocator requirement) - rust-lang#135249 (Fix overflows in the implementation of `overflowing_literals` lint's help) - rust-lang#135251 (Only treat plain literal patterns as short) - rust-lang#135556 (Clarify note in `std::sync::LazyLock` example) - rust-lang#135560 (Update `compiler-builtins` to 0.1.144) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 5 pull requests Successful merges: - rust-lang#133720 ([cfg_match] Adjust syntax) - rust-lang#134496 (Update documentation for Arc::from_raw, Arc::increment_strong_count, and Arc::decrement_strong_count to clarify allocator requirement) - rust-lang#135249 (Fix overflows in the implementation of `overflowing_literals` lint's help) - rust-lang#135251 (Only treat plain literal patterns as short) - rust-lang#135556 (Clarify note in `std::sync::LazyLock` example) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#135249 - s-cerevisiae:fix-overflowing-literals-help, r=chenyukang Fix overflows in the implementation of `overflowing_literals` lint's help This PR fixes two overflow problems that cause the `overflowing_literals` lint to behave incorrectly in some edge cases. 1. When an integer literal is between `i128::MAX` and `u128::MAX`, an overflowing `as` cast can cause the suggested type to be overly small. It's fixed by using checked type conversion and returning `u128` when it's the only choice. (Fixes rust-lang#135248) 2. When an integer literal is `i128::MIN` but is of a smaller type, an overflowing negation cause the compiler to panic in debug build. Fixed by checking the number size beforehand and `wrapping_neg`. (Fixes rust-lang#131849) Edit: extracted the type conversion part into a standalone function to separate the concern of overflowing.
This PR fixes two overflow problems that cause the
overflowing_literals
lint to behave incorrectly in some edge cases.i128::MAX
andu128::MAX
, an overflowingas
cast can cause the suggested type to be overly small. It's fixed by using checked type conversion and returningu128
when it's the only choice. (Fixes Wrong type suggested foroverflowing_literals
when the number is big #135248)i128::MIN
but is of a smaller type, an overflowing negation cause the compiler to panic in debug build. Fixed by checking the number size beforehand andwrapping_neg
. (Fixes ICE:attempt to negate with overflow
#131849)Edit: extracted the type conversion part into a standalone function to separate the concern of overflowing.