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

Deny overflowing (and lossy) integer type cast operations #1895

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

davidsemakula
Copy link

@davidsemakula davidsemakula commented Jan 9, 2025

Summary

Closes #_ (N/A)

  • [n] y/n | Does it introduce breaking changes?
  • [n] y/n | Is it dependent on the specific version of ink or pallet-contracts?

Enables clippy lints for overflowing (and lossy) integer type cast operations (i.e. as conversions)

Description

#1190 "Disallow[ed] unchecked arithmetic", but from this comment, it looks like the actual intent was to "guarantee that implicit overflows never happen", if that's the case then like the -C overflow-checks for rustc, -Dclippy::arithmetic_side_effects is actually NOT sufficient.
This is because type cast operations (i.e. as conversions) can also cause implicit overflows/underflows (as well as other lossy conversions).

This PR enables additional clippy lints (see list below) to "guarantee that overflowing/underflowing and lossy integer operations never happen (in the local crate)".

NOTE: We intentionally ignore safe/lossless lints (e.g. cast_lossless), and lints where either the source or target type is not an integer type (e.g. cast_precision_loss, char_lit_as_u8, as_conversions e.t.c).

References:
https://rust-lang.github.io/rfcs/0560-integer-overflow.html#updates-since-being-accepted
rust-lang/rfcs#1019
https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics
https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow

Checklist before requesting a review

  • My code follows the style guidelines of this project
  • I have added an entry to CHANGELOG.md
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • Any dependent changes have been merged and published in downstream modules

@davidsemakula davidsemakula marked this pull request as ready for review January 9, 2025 05:18
@davidsemakula
Copy link
Author

Seeing as #1190 didn't include any tests, I'm not sure if it makes sense to add tests for both (this and #1190), or if they just simply don't add much value - as we're just enabling clippy lints.

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

Successfully merging this pull request may close these issues.

1 participant