Skip to content

Commit

Permalink
Do not lint when cast is coming from signum method call
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibsG committed Oct 21, 2021
1 parent 76150a4 commit af34c7b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
12 changes: 10 additions & 2 deletions clippy_lints/src/casts/cast_possible_truncation.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::ty::is_isize_or_usize;
use rustc_hir::Expr;
use clippy_utils::expr_or_init;
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);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/casts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit af34c7b

Please sign in to comment.