about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-09-07 19:55:34 +0000
committerbors <bors@rust-lang.org>2022-09-07 19:55:34 +0000
commit617417e9ad355e61a466640165d32680ee017198 (patch)
tree01534a1d289bb15e0b5d1dd121c155a8bdc05601 /src
parentda29f8928d929309cec7ae06828659a634567d9f (diff)
parent0d078c9fd6881a011713d810773aec03e17d793f (diff)
downloadrust-617417e9ad355e61a466640165d32680ee017198.tar.gz
rust-617417e9ad355e61a466640165d32680ee017198.zip
Auto merge of #9365 - c410-f3r:arith, r=Alexendoo
[Arithmetic] Consider literals

Fixes https://github.com/rust-lang/rust-clippy/issues/9307 and makes the `arithmetic` lint behave like `integer_arithmetic`.

It is worth noting that literal integers of a binary operation (`1 + 1`, `i32::MAX + 1`), **regardless if they are in a constant environment**, won't trigger the lint. Assign operations also have similar reasoning.

changelog: Consider literals in the arithmetic lint
Diffstat (limited to 'src')
-rw-r--r--src/docs/arithmetic.txt21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/docs/arithmetic.txt b/src/docs/arithmetic.txt
index 0b3f07d9505..f04125060fb 100644
--- a/src/docs/arithmetic.txt
+++ b/src/docs/arithmetic.txt
@@ -1,22 +1,27 @@
 ### What it does
-Checks for any kind of arithmetic operation of any type.
+Checks any kind of arithmetic operation of any type.
 
 Operators like `+`, `-`, `*` or `<<` are usually capable of overflowing according to the [Rust
 Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
-or can panic (`/`, `%`). Known safe built-in types like `Wrapping` or `Saturing` are filtered
-away.
+or can panic (`/`, `%`).
+
+Known safe built-in types like `Wrapping` or `Saturing`, floats, operations in constant
+environments, allowed types and non-constant operations that won't overflow are ignored.
 
 ### Why is this bad?
-Integer overflow will trigger a panic in debug builds or will wrap in
-release mode. Division by zero will cause a panic in either mode. In some applications one
-wants explicitly checked, wrapping or saturating arithmetic.
+For integers, overflow will trigger a panic in debug builds or wrap the result in
+release mode; division by zero will cause a panic in either mode. As a result, it is
+desirable to explicitly call checked, wrapping or saturating arithmetic methods.
 
 #### Example
 ```
-a + 1;
+// `n` can be any number, including `i32::MAX`.
+fn foo(n: i32) -> i32 {
+  n + 1
+}
 ```
 
-Third-party types also tend to overflow.
+Third-party types can also overflow or present unwanted side-effects.
 
 #### Example
 ```