Skip to content

Commit 9681e60

Browse files
committed
Add a suggestion for a lint.
1 parent 6dbbc70 commit 9681e60

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

text/0000-integer-overflow.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ newtype for which the operator overloads are implemented using the
217217
Note that this is only for potential convenience. The type-based approach has the
218218
drawback that e.g. `Vec<int>` and `Vec<Wrapping<int>>` are incompatible types.
219219

220+
## Lint
221+
222+
In general it seems inadvisable to use operations with error
223+
conditions (like a naked `+` or `-`) in unsafe code. It would be
224+
better to use explicit `checked` or `wrapped` operations as
225+
appropriate. The same holds for destructors, since unwinding in
226+
destructors is inadvisable. Therefore, the RFC recommends a lint be
227+
added against such operations, defaulting to warn, though the details
228+
(such as the name of this lint) are not spelled out.
229+
220230
# Drawbacks
221231

222232
**Making choices is hard.** Having to think about whether wraparound
@@ -252,10 +262,10 @@ violated. However, this danger already exists, as there are numerous
252262
ways to trigger a panic, and hence unsafe code must be written with
253263
this in mind. It seems like the best advice is for unsafe code to
254264
eschew the plain `+` and `-` operators, and instead prefer explicit
255-
checked or wrapping operations as appropriate (this would be a good
256-
opportunity for a lint). Furthermore, the danger of an unexpected
257-
panic occurring in unsafe code must be weighed against the danger of a
258-
(silent) overflow, which can also lead to unsafety.
265+
checked or wrapping operations as appropriate (hence the proposed
266+
lint). Furthermore, the danger of an unexpected panic occurring in
267+
unsafe code must be weighed against the danger of a (silent) overflow,
268+
which can also lead to unsafety.
259269

260270
**Divergence of debug and optimized code.** The proposal here causes
261271
additional divergence of debug and optimized code, since optimized

0 commit comments

Comments
 (0)