Skip to content

Commit 25c632d

Browse files
committed
Fix FP in fn_to_numeric_cast_with_truncation
We only want this lint to check casts to numeric, as per the lint title. Rust already has a built-in check for all other casts [here][rust_check]. [rust_check]: https://github.com/rust-lang/rust/blob/5472b0718f286266ab89acdf234c3552de7e973c/src/librustc_typeck/check/cast.rs#L430-L433
1 parent 1e0729d commit 25c632d

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

clippy_lints/src/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
10881088
}
10891089

10901090
fn lint_fn_to_numeric_cast(cx: &LateContext<'_, '_>, expr: &Expr, cast_expr: &Expr, cast_from: Ty<'_>, cast_to: Ty<'_>) {
1091+
// We only want to check casts to `ty::Uint` or `ty::Int`
1092+
match cast_to.sty {
1093+
ty::Uint(_) | ty::Int(..) => { // continue on },
1094+
_ => return
1095+
}
10911096
match cast_from.sty {
10921097
ty::FnDef(..) | ty::FnPtr(_) => {
10931098
let from_snippet = snippet(cx, cast_expr.span, "x");

tests/ui/fn_to_numeric_cast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ fn test_function_to_numeric_cast() {
3131

3232
// Casting to usize is OK and should not warn
3333
let _ = foo as usize;
34+
35+
// Cast `f` (a `FnDef`) to `fn()` should not warn
36+
fn f() {}
37+
let _ = f as fn();
3438
}
3539

3640
fn test_function_var_to_numeric_cast() {

0 commit comments

Comments
 (0)