Last PR adjustments

This commit is contained in:
xFrednet 2022-12-06 19:07:18 +01:00
parent fcdd08badf
commit e6948c4117
No known key found for this signature in database
GPG Key ID: F5C59D0E669E5302
2 changed files with 17 additions and 4 deletions

View File

@ -3,8 +3,6 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::expr_or_init; use clippy_utils::expr_or_init;
use clippy_utils::source::snippet; use clippy_utils::source::snippet;
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize}; use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
use rustc_ast::ast;
use rustc_attr::IntType;
use rustc_errors::{Applicability, SuggestionStyle}; use rustc_errors::{Applicability, SuggestionStyle};
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_hir::{BinOpKind, Expr, ExprKind};
@ -157,8 +155,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
_ => return, _ => return,
}; };
let snippet = snippet(cx, expr.span, "x"); let snippet = snippet(cx, expr.span, "..");
let name_of_cast_from = snippet.split(" as").next().unwrap_or("x"); let name_of_cast_from = snippet.split(" as").next().unwrap_or("..");
let suggestion = format!("{cast_to}::try_from({name_of_cast_from})"); let suggestion = format!("{cast_to}::try_from({name_of_cast_from})");
span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| { span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| {

View File

@ -94,6 +94,21 @@ declare_clippy_lint! {
/// x as u8 /// x as u8
/// } /// }
/// ``` /// ```
/// Use instead:
/// ```
/// fn as_u8(x: u64) -> u8 {
/// if let Ok(x) = u8::try_from(x) {
/// x
/// } else {
/// todo!();
/// }
/// }
/// // Or
/// #[allow(clippy::cast_possible_truncation)]
/// fn as_u16(x: u64) -> u16 {
/// x as u16
/// }
/// ```
#[clippy::version = "pre 1.29.0"] #[clippy::version = "pre 1.29.0"]
pub CAST_POSSIBLE_TRUNCATION, pub CAST_POSSIBLE_TRUNCATION,
pedantic, pedantic,