Skip to content

Commit 69a6e94

Browse files
committed
Do not remove cast if it would make the expression type uncertain
1 parent 5f2f02c commit 69a6e94

File tree

4 files changed

+105
-47
lines changed

4 files changed

+105
-47
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::numeric_literal::NumericLiteral;
33
use clippy_utils::source::{SpanRangeExt, snippet_opt};
4+
use clippy_utils::ty::expr_type_is_certain;
45
use clippy_utils::visitors::{Visitable, for_each_expr_without_closures};
56
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local};
67
use rustc_ast::{LitFloatType, LitIntType, LitKind};
@@ -143,7 +144,10 @@ pub(super) fn check<'tcx>(
143144
}
144145
}
145146

146-
if cast_from.kind() == cast_to.kind() && !expr.span.in_external_macro(cx.sess().source_map()) {
147+
if cast_from.kind() == cast_to.kind()
148+
&& !expr.span.in_external_macro(cx.sess().source_map())
149+
&& expr_type_is_certain(cx, cast_expr)
150+
{
147151
enum MaybeParenOrBlock {
148152
Paren,
149153
Block,
@@ -167,7 +171,7 @@ pub(super) fn check<'tcx>(
167171
sym::assert_ne_macro,
168172
sym::debug_assert_ne_macro,
169173
];
170-
matches!(expr.span.ctxt().outer_expn_data().macro_def_id, Some(def_id) if
174+
matches!(expr.span.ctxt().outer_expn_data().macro_def_id, Some(def_id) if
171175
cx.tcx.get_diagnostic_name(def_id).is_some_and(|sym| ALLOWED_MACROS.contains(&sym)))
172176
}
173177

tests/ui/unnecessary_cast.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
clippy::no_effect,
77
clippy::nonstandard_macro_braces,
88
clippy::unnecessary_operation,
9+
clippy::double_parens,
910
nonstandard_style,
1011
unused
1112
)]
@@ -283,4 +284,24 @@ mod fixable {
283284
let _ = 5i32 as i64;
284285
//~^ unnecessary_cast
285286
}
287+
288+
fn issue_14366(i: u32) {
289+
// Do not remove the cast if it helps determining the type
290+
let _ = ((1.0 / 8.0) as f64).powf(i as f64);
291+
292+
// But remove useless casts anyway
293+
let _ = (((1.0 / 8.0) as f64)).powf(i as f64);
294+
//~^ unnecessary_cast
295+
}
296+
297+
fn ambiguity() {
298+
pub trait T {}
299+
impl T for u32 {}
300+
impl T for String {}
301+
fn f(_: impl T) {}
302+
303+
f((1 + 2) as u32);
304+
f(((1 + 2u32)));
305+
//~^ unnecessary_cast
306+
}
286307
}

tests/ui/unnecessary_cast.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
clippy::no_effect,
77
clippy::nonstandard_macro_braces,
88
clippy::unnecessary_operation,
9+
clippy::double_parens,
910
nonstandard_style,
1011
unused
1112
)]
@@ -283,4 +284,24 @@ mod fixable {
283284
let _ = 5i32 as i64 as i64;
284285
//~^ unnecessary_cast
285286
}
287+
288+
fn issue_14366(i: u32) {
289+
// Do not remove the cast if it helps determining the type
290+
let _ = ((1.0 / 8.0) as f64).powf(i as f64);
291+
292+
// But remove useless casts anyway
293+
let _ = (((1.0 / 8.0) as f64) as f64).powf(i as f64);
294+
//~^ unnecessary_cast
295+
}
296+
297+
fn ambiguity() {
298+
pub trait T {}
299+
impl T for u32 {}
300+
impl T for String {}
301+
fn f(_: impl T) {}
302+
303+
f((1 + 2) as u32);
304+
f((1 + 2u32) as u32);
305+
//~^ unnecessary_cast
306+
}
286307
}

0 commit comments

Comments
 (0)