Skip to content

Commit 7cd0ec5

Browse files
committed
add more info link
1 parent 6681914 commit 7cd0ec5

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

clippy_lints/src/casts/cast_possible_wrap.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use clippy_utils::diagnostics::span_lint;
21
use rustc_hir::Expr;
3-
use rustc_lint::LateContext;
2+
use rustc_lint::{LateContext, LintContext};
43
use rustc_middle::ty::Ty;
54

65
use super::{utils, CAST_POSSIBLE_WRAP};
@@ -9,6 +8,7 @@ use super::{utils, CAST_POSSIBLE_WRAP};
98
const ALLOWED_POINTER_SIZES: [u64; 3] = [16, 32, 64];
109

1110
// whether the lint should be emitted, and the required pointer size, if it matters
11+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1212
enum EmitState {
1313
NoLint,
1414
LintAlways,
@@ -77,5 +77,13 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, ca
7777
),
7878
};
7979

80-
span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, message.as_str());
80+
cx.struct_span_lint(CAST_POSSIBLE_WRAP, expr.span, message, |diag| {
81+
if let EmitState::LintOnPtrSize(16) = should_lint {
82+
diag
83+
.note("`usize` and `isize` may be as small as 16 bits on some platforms")
84+
.note("for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types")
85+
} else {
86+
diag
87+
}
88+
});
8189
}

tests/ui/cast.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ error: casting `usize` to `i16` may wrap around the value on targets with 16-bit
244244
|
245245
LL | 1usize as i16; // wraps on 16 bit ptr size
246246
| ^^^^^^^^^^^^^
247+
|
248+
= note: `usize` and `isize` may be as small as 16 bits on some platforms
249+
= note: for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types
247250

248251
error: casting `usize` to `i32` may truncate the value on targets with 64-bit wide pointers
249252
--> $DIR/cast.rs:46:5
@@ -274,6 +277,9 @@ error: casting `u16` to `isize` may wrap around the value on targets with 16-bit
274277
|
275278
LL | 1u16 as isize; // wraps on 16 bit ptr size
276279
| ^^^^^^^^^^^^^
280+
|
281+
= note: `usize` and `isize` may be as small as 16 bits on some platforms
282+
= note: for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types
277283

278284
error: casting `u32` to `isize` may wrap around the value on targets with 32-bit wide pointers
279285
--> $DIR/cast.rs:50:5

0 commit comments

Comments
 (0)