Skip to content

Commit

Permalink
Last PR adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Dec 6, 2022
1 parent 92f2277 commit dc887c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 2 additions & 4 deletions clippy_lints/src/casts/cast_possible_truncation.rs
Expand Up @@ -3,8 +3,6 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::expr_or_init;
use clippy_utils::source::snippet;
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_hir::def::{DefKind, Res};
use rustc_hir::{BinOpKind, Expr, ExprKind};
Expand Down Expand Up @@ -157,8 +155,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
_ => return,
};

let snippet = snippet(cx, expr.span, "x");
let name_of_cast_from = snippet.split(" as").next().unwrap_or("x");
let snippet = snippet(cx, expr.span, "..");
let name_of_cast_from = snippet.split(" as").next().unwrap_or("..");
let suggestion = format!("{cast_to}::try_from({name_of_cast_from})");

span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg, |diag| {
Expand Down
15 changes: 15 additions & 0 deletions clippy_lints/src/casts/mod.rs
Expand Up @@ -94,6 +94,21 @@ declare_clippy_lint! {
/// x as u8
/// }
/// ```
/// Use instead:
/// ```
/// fn as_u8(x: u64) -> u8 {
/// if let Ok(x) = u8::try_from(x) {
/// x
/// } else {
/// todo!();
/// }
/// }
/// // Or
/// fn as_u16(x: u64) -> u16 {
/// #[allow(clippy::cast_possible_truncation)]
/// x as u16
/// }
/// ```
#[clippy::version = "pre 1.29.0"]
pub CAST_POSSIBLE_TRUNCATION,
pedantic,
Expand Down

0 comments on commit dc887c5

Please sign in to comment.