diff --git a/clippy_lints/src/casts/cast_possible_wrap.rs b/clippy_lints/src/casts/cast_possible_wrap.rs index e872d190c82..f77d7821539 100644 --- a/clippy_lints/src/casts/cast_possible_wrap.rs +++ b/clippy_lints/src/casts/cast_possible_wrap.rs @@ -1,6 +1,5 @@ -use clippy_utils::diagnostics::span_lint; use rustc_hir::Expr; -use rustc_lint::LateContext; +use rustc_lint::{LateContext, LintContext}; use rustc_middle::ty::Ty; use super::{utils, CAST_POSSIBLE_WRAP}; @@ -9,6 +8,7 @@ const ALLOWED_POINTER_SIZES: [u64; 3] = [16, 32, 64]; // whether the lint should be emitted, and the required pointer size, if it matters +#[derive(Copy, Clone, Debug, PartialEq, Eq)] enum EmitState { NoLint, LintAlways, @@ -77,5 +77,13 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, ca ), }; - span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, message.as_str()); + cx.struct_span_lint(CAST_POSSIBLE_WRAP, expr.span, message, |diag| { + if let EmitState::LintOnPtrSize(16) = should_lint { + diag + .note("`usize` and `isize` may be as small as 16 bits on some platforms") + .note("for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types") + } else { + diag + } + }); } diff --git a/tests/ui/cast.stderr b/tests/ui/cast.stderr index a7d78a95d59..de29af78ef3 100644 --- a/tests/ui/cast.stderr +++ b/tests/ui/cast.stderr @@ -244,6 +244,9 @@ error: casting `usize` to `i16` may wrap around the value on targets with 16-bit | LL | 1usize as i16; // wraps on 16 bit ptr size | ^^^^^^^^^^^^^ + | + = note: `usize` and `isize` may be as small as 16 bits on some platforms + = note: for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types error: casting `usize` to `i32` may truncate the value on targets with 64-bit wide pointers --> $DIR/cast.rs:46:5 @@ -274,6 +277,9 @@ error: casting `u16` to `isize` may wrap around the value on targets with 16-bit | LL | 1u16 as isize; // wraps on 16 bit ptr size | ^^^^^^^^^^^^^ + | + = note: `usize` and `isize` may be as small as 16 bits on some platforms + = note: for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types error: casting `u32` to `isize` may wrap around the value on targets with 32-bit wide pointers --> $DIR/cast.rs:50:5