Skip to content

[Guideline] Add do not divide by 0 #132

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

vapdrs
Copy link
Contributor

@vapdrs vapdrs commented Jun 10, 2025

Closes #131

Copy link

netlify bot commented Jun 10, 2025

Deploy Preview for scrc-coding-guidelines ready!

Name Link
🔨 Latest commit 1076b36
🔍 Latest deploy log https://app.netlify.com/projects/scrc-coding-guidelines/deploys/689f3bf0b185860008a6b560
😎 Deploy Preview https://deploy-preview-132--scrc-coding-guidelines.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@felix91gr
Copy link
Collaborator

felix91gr commented Jun 11, 2025

Hi @vapdrs! I have already sent you a message in Zulip, but here seems like a better place to do so.

I come to add a couple of things to this guideline :)

  1. There is a Clippy lint (actually a couple of lints, but this one is the main one) that one can enable to catch these kinds of operations: arithmetic_side_effects. This is a fairly general lint that, if enabled using warn or deny, lints any arithmetic expression that either overflows or panics (division and modulo by 0 will do the latter).

  2. The aforementioned lint should indicate to the user that there are arithmetic operations in the stdlib that can be used to guarantee well-defined behavior. In the case of division by zero, it will suggest using operations such as checked_div, which outputs an Option<T> that the user must then handle properly. The None result indicates overflow, underflow or division by zero.

There are other such operations for division, such as checked_div_rem_euclid for when a remainder is desired.

And for other arithmetic operations, there are quite a few functions one can use to avoid Undefined Behavior: https://doc.rust-lang.org/std/?search=checked

  1. A complement to the two resources above is the NonZero type. One uses NonZero to enclose a value that is known, by construction, to not equal zero.

This combines rather well with Option, as in Option<NonZero>, since the compiler can do some memory layout optimization due to the fact that the value being enclosed by NonZero has one bit-pattern that is known to not be possible (the 000...000 pattern)

I will review the PR shortly :3

Copy link
Collaborator

@PLeVasseur PLeVasseur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this up @vapdrs! Could you check the comments I left?

vapdrs added 2 commits June 24, 2025 17:23
As stated there is no compliant way to do this, so no example should be
present.
While the guideline does not strictly apply to this example, it is a
good suggestion for what to do instead.
@vapdrs vapdrs requested review from felix91gr and PLeVasseur June 27, 2025 18:09
Copy link
Collaborator

@PLeVasseur PLeVasseur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pulling this together @vapdrs! I left a few suggestions based on how conf.py works together with Sphinx Needs as well as a way to use our coding guidelines extension. Could you take a look?

@PLeVasseur
Copy link
Collaborator

I'm realizing now that perhaps the issue template having uppercase options may have been to blame for them being made uppercase in this PR. Was that the case for you @vapdrs?

I've opened #143 to fix this 👍

vapdrs and others added 3 commits July 30, 2025 08:41
It is not recommended for normative language

Co-authored-by: Alex Eris Celeste née Gilding <[email protected]>
Also moved to the Rationale.
Mandatory is a bit strong for panics as determined in today's meeting.
@felix91gr
Copy link
Collaborator

I haven't been able to give the examples a proper re-read just yet. Tis' been a low-battery week. Will do my best to have final feedback on those (also regarding #136)

@felix91gr
Copy link
Collaborator

@vapdrs the build is failing, but to no fault of your own. It's just something that happens every now and then when the FLS and our FLS lockfile diverge.

There should be a way to automate this for most PRs, but for right now... let's see. I'd usually ask you to run the command that fixes this, but for traceability purposes we actually want those changes to be their own Pull Requests.

Lemme fix this in main, and that should enable your changes to build again :)

@felix91gr felix91gr mentioned this pull request Aug 11, 2025
@felix91gr felix91gr added chapter: expressions decidability: undecidable A coding guideline which cannot be checked automatically labels Aug 11, 2025
@felix91gr
Copy link
Collaborator

@vapdrs thanks! I was gonna ping you here as well but you just did the thing ❤️

@felix91gr felix91gr self-assigned this Aug 11, 2025
Copy link
Collaborator

@felix91gr felix91gr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are the last few touches I'd add.

vapdrs and others added 2 commits August 15, 2025 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chapter: expressions decidability: undecidable A coding guideline which cannot be checked automatically
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Coding Guideline]: Do not divide by 0
7 participants