mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
Auto merge of #7850 - ThibsG:PossibleCastTruncation5395, r=camsteffen
Fix FP: no lint when cast is coming from `signum` method call for `cast_possible_truncation` lint Fixes a FP when cast is coming from `signum` method call fixes: #5395 changelog: [`cast_possible_truncation`] Fix FP when cast is coming from `signum` method call
This commit is contained in:
commit
c97a06d9af
3 changed files with 19 additions and 3 deletions
|
@ -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 @@ 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);
|
||||
|
|
|
@ -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 a new issue