-
Notifications
You must be signed in to change notification settings - Fork 28
[Guideline] Add safe division guideline to avoid dividing by zero #136
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
Changes from 23 commits
06933f9
b2a3143
8d2392d
752ba7a
9d1e817
1ad9181
ee853e0
03d7bbc
9dc042f
357c012
36f60e2
2fb316f
9291002
c9254d0
b28f8ce
296df27
bc8ed67
fbb4429
c2669a7
e6c1aaf
ef44809
557bb5c
4a2e017
34dc8a9
11662cf
52834f8
cd22b39
e9cdb3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| [default] | ||
| extend-ignore-identifiers-re = [ | ||
| # Ignore things that look like gui_xztNdXA2oFNB | ||
| # Ignore Sphinx directives for typos | ||
| "gui_.*", | ||
| "rat_.*", | ||
| "compl_ex_.*", | ||
| "non_compl_ex_.*", | ||
| ] | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,59 @@ Expressions | |
|
|
||
| fn with_base(_: &Base) { ... } | ||
|
|
||
| .. guideline:: Do not use integer type as divisor | ||
| :id: gui_7y0GAMmtMhch | ||
| :category: advisory | ||
| :status: draft | ||
| :release: latest | ||
| :fls: fls_Q9dhNiICGIfr | ||
| :decidability: decidable | ||
| :scope: module | ||
| :tags: numerics, subset | ||
|
|
||
| This guideline applies when a `Division Expression | ||
| <https://rust-lang.github.io/fls/expressions.html#syntax_divisionexpression>`_ or `RemainderExpression | ||
| <https://rust-lang.github.io/fls/expressions.html#syntax_remainderexpression>`_ is used with a RightOperand of | ||
| `integer type <https://rust-lang.github.io/fls/types-and-traits.html#integer-types>`_. | ||
|
|
||
| .. rationale:: | ||
| :id: rat_vLFlPWSCHRje | ||
| :status: draft | ||
|
|
||
| The built-in semantics for these expressions can result in panics when division by zero occurs. It is | ||
| recommended to either use checked arithmetic functions to explicitly specify the behavior in such | ||
vapdrs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| situations or to use :std:`std::num::NonZero` as a divisor to avoid division by zero. | ||
|
|
||
| .. non_compliant_example:: | ||
| :id: non_compl_ex_0XeioBrgfh5z | ||
| :status: draft | ||
|
|
||
| When the division is performed, the right operand is evaluated to zero and the program panics. | ||
vapdrs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| .. code-block:: rust | ||
|
|
||
| let x = 0; | ||
| let y = 5 / x; | ||
vapdrs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let x = 5 % x; | ||
vapdrs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| .. compliant_example:: | ||
| :id: compl_ex_k1CD6xoZxhXb | ||
| :status: draft | ||
|
|
||
| The developer must explicitly indicate the intended behavior when a division by zero occurs, or use a | ||
| type for which it is invalid to have a value of zero. | ||
|
||
|
|
||
| .. code-block:: rust | ||
|
|
||
| let x = 0; | ||
| if let Some(divisor) = match NonZero::<u32>::new(x) { | ||
| let result = 5 / divisor; | ||
| } | ||
PLeVasseur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let result = match 5u32.checked_rem(x) { | ||
| None => 0, | ||
| Some(r) => r, | ||
| } | ||
|
|
||
|
|
||
| .. guideline:: The 'as' operator should not be used with numeric operands | ||
| :id: gui_ADHABsmK9FXz | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.