You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: clippy_lints/src/casts/mod.rs
+58-55
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,7 @@ use rustc_session::impl_lint_pass;
32
32
33
33
declare_clippy_lint!{
34
34
/// ### What it does
35
-
/// Checks for casts from any numerical to a float type where
35
+
/// Checks for casts from any numeric type to a float type where
36
36
/// the receiving type cannot store all values from the original type without
37
37
/// rounding errors. This possible rounding is to be expected, so this lint is
38
38
/// `Allow` by default.
@@ -58,14 +58,14 @@ declare_clippy_lint! {
58
58
59
59
declare_clippy_lint!{
60
60
/// ### What it does
61
-
/// Checks for casts from a signed to an unsigned numerical
61
+
/// Checks for casts from a signed to an unsigned numeric
62
62
/// type. In this case, negative values wrap around to large positive values,
63
-
/// which can be quite surprising in practice. However, as the cast works as
63
+
/// which can be quite surprising in practice. However, since the cast works as
64
64
/// defined, this lint is `Allow` by default.
65
65
///
66
66
/// ### Why is this bad?
67
67
/// Possibly surprising results. You can activate this lint
68
-
/// as a one-time check to see where numerical wrapping can arise.
68
+
/// as a one-time check to see where numeric wrapping can arise.
69
69
///
70
70
/// ### Example
71
71
/// ```no_run
@@ -80,7 +80,7 @@ declare_clippy_lint! {
80
80
81
81
declare_clippy_lint!{
82
82
/// ### What it does
83
-
/// Checks for casts between numerical types that may
83
+
/// Checks for casts between numeric types that may
84
84
/// truncate large values. This is expected behavior, so the cast is `Allow` by
85
85
/// default. It suggests user either explicitly ignore the lint,
86
86
/// or use `try_from()` and handle the truncation, default, or panic explicitly.
@@ -120,17 +120,16 @@ declare_clippy_lint! {
120
120
declare_clippy_lint!{
121
121
/// ### What it does
122
122
/// Checks for casts from an unsigned type to a signed type of
123
-
/// the same size, or possibly smaller due to targetdependent integers.
124
-
/// Performing such a cast is a 'no-op' for the compiler, i.e., nothing is
125
-
/// changed at the bit level, and the binary representation of the value is
123
+
/// the same size, or possibly smaller due to target-dependent integers.
124
+
/// Performing such a cast is a no-op for the compiler (that is, nothing is
125
+
/// changed at the bit level), and the binary representation of the value is
126
126
/// reinterpreted. This can cause wrapping if the value is too big
127
127
/// for the target signed type. However, the cast works as defined, so this lint
128
128
/// is `Allow` by default.
129
129
///
130
130
/// ### Why is this bad?
131
131
/// While such a cast is not bad in itself, the results can
132
-
/// be surprising when this is not the intended behavior, as demonstrated by the
133
-
/// example below.
132
+
/// be surprising when this is not the intended behavior:
134
133
///
135
134
/// ### Example
136
135
/// ```no_run
@@ -144,16 +143,16 @@ declare_clippy_lint! {
144
143
145
144
declare_clippy_lint!{
146
145
/// ### What it does
147
-
/// Checks for casts between numerical types that may
148
-
/// be replaced by safe conversion functions.
146
+
/// Checks for casts between numeric types that can be replaced by safe
147
+
/// conversion functions.
149
148
///
150
149
/// ### Why is this bad?
151
-
/// Rust's `as` keyword will perform many kinds of
152
-
/// conversions, including silently lossy conversions. Conversion functions such
153
-
/// as `i32::from` will only perform lossless conversions. Using the conversion
154
-
/// functions prevents conversions from turning into silent lossy conversions if
155
-
/// the types of the input expressions ever change, and make it easier for
156
-
/// people reading the code to know that the conversion is lossless.
150
+
/// Rust's `as` keyword will perform many kinds of conversions, including
151
+
/// silently lossy conversions. Conversion functions such as `i32::from`
152
+
/// will only perform lossless conversions. Using the conversion functions
153
+
/// prevents conversions from becoming silently lossy if the input types
154
+
/// ever change, and makes it clear for people reading the code that the
155
+
/// conversion is lossless.
157
156
///
158
157
/// ### Example
159
158
/// ```no_run
@@ -177,19 +176,21 @@ declare_clippy_lint! {
177
176
178
177
declare_clippy_lint!{
179
178
/// ### What it does
180
-
/// Checks for casts to the same type, casts of int literals to integer types, casts of float
181
-
/// literals to float types and casts between raw pointers without changing type or constness.
179
+
/// Checks for casts to the same type, casts of int literals to integer
180
+
/// types, casts of float literals to float types, and casts between raw
181
+
/// pointers that don't change type or constness.
182
182
///
183
183
/// ### Why is this bad?
184
184
/// It's just unnecessary.
185
185
///
186
186
/// ### Known problems
187
-
/// When the expression on the left is a function call, the lint considers the return type to be
188
-
/// a type alias if it's aliased through a `use` statement
189
-
/// (like `use std::io::Result as IoResult`). It will not lint such cases.
187
+
/// When the expression on the left is a function call, the lint considers
188
+
/// the return type to be a type alias if it's aliased through a `use`
189
+
/// statement (like `use std::io::Result as IoResult`). It will not lint
190
+
/// such cases.
190
191
///
191
-
/// This check is also rather primitive. It will only work on primitive types without any
192
-
/// intermediate references, raw pointers and trait objects may or may not work.
192
+
/// This check will only work on primitive types without any intermediate
193
+
/// references: raw pointers and trait objects may or may not work.
193
194
///
194
195
/// ### Example
195
196
/// ```no_run
@@ -211,17 +212,17 @@ declare_clippy_lint! {
211
212
212
213
declare_clippy_lint!{
213
214
/// ### What it does
214
-
/// Checks for casts, using `as` or `pointer::cast`,
215
-
/// from a less-strictly-aligned pointer to a more-strictly-aligned pointer
215
+
/// Checks for casts, using `as` or `pointer::cast`, from a
216
+
/// lessstrictlyaligned pointer to a morestrictlyaligned pointer.
216
217
///
217
218
/// ### Why is this bad?
218
-
/// Dereferencing the resulting pointer may be undefined
219
-
/// behavior.
219
+
/// Dereferencing the resulting pointer may be undefined behavior.
220
220
///
221
221
/// ### Known problems
222
-
/// Using `std::ptr::read_unaligned` and `std::ptr::write_unaligned` or similar
223
-
/// on the resulting pointer is fine. Is over-zealous: Casts with manual alignment checks or casts like
224
-
/// u64-> u8 -> u16 can be fine. Miri is able to do a more in-depth analysis.
222
+
/// Using [`std::ptr::read_unaligned`](https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html) and [`std::ptr::write_unaligned`](https://doc.rust-lang.org/std/ptr/fn.write_unaligned.html) or
223
+
/// similar on the resulting pointer is fine. Is over-zealous: casts with
224
+
/// manual alignment checks or casts like `u64` -> `u8` -> `u16` can be
225
+
/// fine. Miri is able to do a more in-depth analysis.
225
226
///
226
227
/// ### Example
227
228
/// ```no_run
@@ -234,20 +235,21 @@ declare_clippy_lint! {
234
235
#[clippy::version = "pre 1.29.0"]
235
236
pubCAST_PTR_ALIGNMENT,
236
237
pedantic,
237
-
"cast from a pointer to a more-strictly-aligned pointer"
238
+
"cast from a pointer to a morestrictlyaligned pointer"
238
239
}
239
240
240
241
declare_clippy_lint!{
241
242
/// ### What it does
242
-
/// Checks for casts of function pointers to something other than usize
243
+
/// Checks for casts of function pointers to something other than `usize`.
243
244
///
244
245
/// ### Why is this bad?
245
-
/// Casting a function pointer to anything other than usize/isize is not portable across
246
-
/// architectures, because you end up losing bits if the target type is too small or end up with a
247
-
/// bunch of extra bits that waste space and add more instructions to the final binary than
248
-
/// strictly necessary for the problem
246
+
/// Casting a function pointer to anything other than `usize`/`isize` is
247
+
/// not portable across architectures. If the target type is too small the
248
+
/// address would be truncated, and target types larger than `usize` are
249
+
/// unnecessary.
249
250
///
250
-
/// Casting to isize also doesn't make sense since there are no signed addresses.
251
+
/// Casting to `isize` also doesn't make sense, since addresses are never
252
+
/// signed.
251
253
///
252
254
/// ### Example
253
255
/// ```no_run
@@ -263,17 +265,17 @@ declare_clippy_lint! {
263
265
#[clippy::version = "pre 1.29.0"]
264
266
pubFN_TO_NUMERIC_CAST,
265
267
style,
266
-
"casting a function pointer to a numeric type other than usize"
268
+
"casting a function pointer to a numeric type other than `usize`"
267
269
}
268
270
269
271
declare_clippy_lint!{
270
272
/// ### What it does
271
273
/// Checks for casts of a function pointer to a numeric type not wide enough to
272
-
/// store address.
274
+
/// store an address.
273
275
///
274
276
/// ### Why is this bad?
275
277
/// Such a cast discards some bits of the function's address. If this is intended, it would be more
276
-
/// clearly expressed by casting to usize first, then casting the usize to the intended type (with
278
+
/// clearly expressed by casting to `usize` first, then casting the `usize` to the intended type (with
277
279
/// a comment) to perform the truncation.
278
280
///
279
281
/// ### Example
@@ -306,7 +308,7 @@ declare_clippy_lint! {
306
308
/// ### Why restrict this?
307
309
/// Casting a function pointer to an integer can have surprising results and can occur
308
310
/// accidentally if parentheses are omitted from a function call. If you aren't doing anything
309
-
/// low-level with function pointers then you can opt-out of casting functions to integers in
311
+
/// low-level with function pointers then you can optout of casting functions to integers in
310
312
/// order to avoid mistakes. Alternatively, you can use this lint to audit all uses of function
311
313
/// pointer casts in your code.
312
314
///
@@ -349,8 +351,8 @@ declare_clippy_lint! {
349
351
/// ### Why is this bad?
350
352
/// In general, casting values to smaller types is
351
353
/// error-prone and should be avoided where possible. In the particular case of
352
-
/// converting a character literal to u8, it is easy to avoid by just using a
353
-
/// byte literal instead. As an added bonus, `b'a'` is even slightly shorter
354
+
/// converting a character literal to `u8`, it is easy to avoid by just using a
355
+
/// byte literal instead. As an added bonus, `b'a'` is also slightly shorter
354
356
/// than `'a' as u8`.
355
357
///
356
358
/// ### Example
@@ -371,12 +373,13 @@ declare_clippy_lint! {
371
373
372
374
declare_clippy_lint!{
373
375
/// ### What it does
374
-
/// Checks for `as` casts between raw pointers without changing its mutability,
375
-
/// namely `*const T` to `*const U` and `*mut T` to `*mut U`.
376
+
/// Checks for `as` casts between raw pointers that don't change their
377
+
/// constness, namely `*const T` to `*const U` and `*mut T` to `*mut U`.
376
378
///
377
379
/// ### Why is this bad?
378
-
/// Though `as` casts between raw pointers are not terrible, `pointer::cast` is safer because
379
-
/// it cannot accidentally change the pointer's mutability nor cast the pointer to other types like `usize`.
380
+
/// Though `as` casts between raw pointers are not terrible,
381
+
/// `pointer::cast` is safer because it cannot accidentally change the
382
+
/// pointer's mutability, nor cast the pointer to other types like `usize`.
380
383
///
381
384
/// ### Example
382
385
/// ```no_run
@@ -395,12 +398,12 @@ declare_clippy_lint! {
395
398
#[clippy::version = "1.51.0"]
396
399
pubPTR_AS_PTR,
397
400
pedantic,
398
-
"casting using `as` from and to raw pointers that doesn't change its mutability, where `pointer::cast` could take the place of `as`"
401
+
"casting using `as` between raw pointers that doesn't change their constness, where `pointer::cast` could take the place of `as`"
399
402
}
400
403
401
404
declare_clippy_lint!{
402
405
/// ### What it does
403
-
/// Checks for `as` casts between raw pointers which change its constness, namely `*const T` to
406
+
/// Checks for `as` casts between raw pointers that change their constness, namely `*const T` to
404
407
/// `*mut T` and `*mut T` to `*const T`.
405
408
///
406
409
/// ### Why is this bad?
@@ -423,12 +426,12 @@ declare_clippy_lint! {
423
426
#[clippy::version = "1.72.0"]
424
427
pubPTR_CAST_CONSTNESS,
425
428
pedantic,
426
-
"casting using `as` from and to raw pointers to change constness when specialized methods apply"
429
+
"casting using `as` on raw pointers to change constness when specialized methods apply"
427
430
}
428
431
429
432
declare_clippy_lint!{
430
433
/// ### What it does
431
-
/// Checks for casts from an enum type to an integral type which will definitely truncate the
434
+
/// Checks for casts from an enum type to an integral type that will definitely truncate the
432
435
/// value.
433
436
///
434
437
/// ### Why is this bad?
@@ -442,7 +445,7 @@ declare_clippy_lint! {
442
445
#[clippy::version = "1.61.0"]
443
446
pubCAST_ENUM_TRUNCATION,
444
447
suspicious,
445
-
"casts from an enum type to an integral type which will truncate the value"
448
+
"casts from an enum type to an integral type that will truncate the value"
446
449
}
447
450
448
451
declare_clippy_lint!{
@@ -621,7 +624,7 @@ declare_clippy_lint! {
621
624
622
625
declare_clippy_lint!{
623
626
/// ### What it does
624
-
/// Checks for the result of a `&self`-taking `as_ptr` being cast to a mutable pointer
627
+
/// Checks for the result of a `&self`-taking `as_ptr` being cast to a mutable pointer.
625
628
///
626
629
/// ### Why is this bad?
627
630
/// Since `as_ptr` takes a `&self`, the pointer won't have write permissions unless interior
0 commit comments