Skip to content

Commit e6948c4

Browse files
committed
Last PR adjustments
1 parent fcdd08b commit e6948c4

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

clippy_lints/src/casts/cast_possible_truncation.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
33
use clippy_utils::expr_or_init;
44
use clippy_utils::source::snippet;
55
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
6-
use rustc_ast::ast;
7-
use rustc_attr::IntType;
86
use rustc_errors::{Applicability, SuggestionStyle};
97
use rustc_hir::def::{DefKind, Res};
108
use rustc_hir::{BinOpKind, Expr, ExprKind};
@@ -157,8 +155,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
157155
_ => return,
158156
};
159157

160-
let snippet = snippet(cx, expr.span, "x");
161-
let name_of_cast_from = snippet.split(" as").next().unwrap_or("x");
158+
let snippet = snippet(cx, expr.span, "..");
159+
let name_of_cast_from = snippet.split(" as").next().unwrap_or("..");
162160
let suggestion = format!("{cast_to}::try_from({name_of_cast_from})");
163161

164162
span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| {

clippy_lints/src/casts/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ declare_clippy_lint! {
9494
/// x as u8
9595
/// }
9696
/// ```
97+
/// Use instead:
98+
/// ```
99+
/// fn as_u8(x: u64) -> u8 {
100+
/// if let Ok(x) = u8::try_from(x) {
101+
/// x
102+
/// } else {
103+
/// todo!();
104+
/// }
105+
/// }
106+
/// // Or
107+
/// #[allow(clippy::cast_possible_truncation)]
108+
/// fn as_u16(x: u64) -> u16 {
109+
/// x as u16
110+
/// }
111+
/// ```
97112
#[clippy::version = "pre 1.29.0"]
98113
pub CAST_POSSIBLE_TRUNCATION,
99114
pedantic,

0 commit comments

Comments
 (0)