From 566244a8bcd4f94a0c8c2491c2abde552ee46558 Mon Sep 17 00:00:00 2001 From: ThibsG Date: Wed, 20 Oct 2021 21:44:05 +0200 Subject: [PATCH] Do not lint when cast is coming from `signum` method call --- clippy_lints/src/casts/cast_possible_truncation.rs | 12 ++++++++++-- clippy_lints/src/casts/mod.rs | 2 +- tests/ui/cast.rs | 8 ++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/casts/cast_possible_truncation.rs b/clippy_lints/src/casts/cast_possible_truncation.rs index 833ad122e..2ae7d16e0 100644 --- a/clippy_lints/src/casts/cast_possible_truncation.rs +++ b/clippy_lints/src/casts/cast_possible_truncation.rs @@ -1,12 +1,20 @@ use clippy_utils::diagnostics::span_lint; +use clippy_utils::expr_or_init; use clippy_utils::ty::is_isize_or_usize; -use rustc_hir::Expr; +use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, FloatTy, Ty}; use super::{utils, CAST_POSSIBLE_TRUNCATION}; -pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) { +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) { + // do not lint if cast comes from a `signum` function + if let ExprKind::MethodCall(path, ..) = expr_or_init(cx, cast_expr).kind { + if path.ident.name.as_str() == "signum" { + return; + } + } + let msg = match (cast_from.is_integral(), cast_to.is_integral()) { (true, true) => { let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx); diff --git a/clippy_lints/src/casts/mod.rs b/clippy_lints/src/casts/mod.rs index f0800c6a6..233abd178 100644 --- a/clippy_lints/src/casts/mod.rs +++ b/clippy_lints/src/casts/mod.rs @@ -427,7 +427,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts { fn_to_numeric_cast::check(cx, expr, cast_expr, cast_from, cast_to); fn_to_numeric_cast_with_truncation::check(cx, expr, cast_expr, cast_from, cast_to); if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) { - cast_possible_truncation::check(cx, expr, cast_from, cast_to); + cast_possible_truncation::check(cx, expr, cast_expr, cast_from, cast_to); cast_possible_wrap::check(cx, expr, cast_from, cast_to); cast_precision_loss::check(cx, expr, cast_from, cast_to); cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to); diff --git a/tests/ui/cast.rs b/tests/ui/cast.rs index 8ee0969b0..adc95e63a 100644 --- a/tests/ui/cast.rs +++ b/tests/ui/cast.rs @@ -92,4 +92,12 @@ fn main() { (1i64).checked_rem_euclid(-1i64).unwrap() as u64; (1i64).checked_rem_euclid(-1i64).unwrap() as u128; (1isize).checked_rem_euclid(-1isize).unwrap() as usize; + + // no lint for `cast_possible_truncation` + // with `signum` method call (see issue #5395) + let x: i64 = 5; + let _ = x.signum() as i32; + + let s = x.signum(); + let _ = s as i32; }