Do not lint when cast is coming from signum
method call
This commit is contained in:
parent
76150a4922
commit
566244a8bc
@ -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);
|
||||
|
@ -427,7 +427,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user