Auto merge of #12496 - Jacherr:issue-12492, r=blyxyas
Disable `cast_lossless` when casting to u128 from any (u)int type Fixes https://github.com/rust-lang/rust-clippy/issues/12492 Disables `cast_lossless` when casting to u128 from any int or uint type. The lint states that when casting to any int type, there can potentially be lossy behaviour if the source type ever exceeds the size of the destination type in the future, which is impossible with a destination of u128. It's possible this is a bit of a niche edge case which is better addressed by just disabling the lint in code, but I personally couldn't think of any good reason to still lint in this specific case - maybe except if the source is a bool, for readability reasons :). changelog: FP: `cast_lossless`: disable lint when casting to u128 from any (u)int type
This commit is contained in:
commit
34766a6792
4 changed files with 39 additions and 24 deletions
|
|
@ -6,7 +6,7 @@ use clippy_utils::ty::is_isize_or_usize;
|
|||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Expr, ExprKind, QPath, TyKind};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, FloatTy, Ty};
|
||||
use rustc_middle::ty::{self, FloatTy, Ty, UintTy};
|
||||
|
||||
use super::{utils, CAST_LOSSLESS};
|
||||
|
||||
|
|
@ -77,7 +77,10 @@ pub(super) fn check(
|
|||
|
||||
fn should_lint(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool {
|
||||
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
|
||||
if in_constant(cx, expr.hir_id) {
|
||||
//
|
||||
// If destination is u128, do not lint because source type cannot be larger
|
||||
// If source is bool, still lint due to the lint message differing (refers to style)
|
||||
if in_constant(cx, expr.hir_id) || (!cast_from.is_bool() && matches!(cast_to.kind(), ty::Uint(UintTy::U128))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue