Skip to content

Commit fc44198

Browse files
authored
Use Ty::is_fn instead of manually matching on TyKind (#15089)
just makes the code a bit more concise changelog: none
2 parents e1be062 + 4154387 commit fc44198

File tree

7 files changed

+43
-57
lines changed

7 files changed

+43
-57
lines changed

clippy_lints/src/casts/confusing_method_to_numeric_cast.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ fn get_const_name_and_ty_name(
5959

6060
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
6161
// We allow casts from any function type to any function type.
62-
match cast_to.kind() {
63-
ty::FnDef(..) | ty::FnPtr(..) => return,
64-
_ => { /* continue to checks */ },
62+
if cast_to.is_fn() {
63+
return;
6564
}
6665

6766
if let ty::FnDef(def_id, generics) = cast_from.kind()

clippy_lints/src/casts/fn_to_numeric_cast.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::source::snippet_with_applicability;
33
use rustc_errors::Applicability;
44
use rustc_hir::Expr;
55
use rustc_lint::LateContext;
6-
use rustc_middle::ty::{self, Ty};
6+
use rustc_middle::ty::Ty;
77

88
use super::{FN_TO_NUMERIC_CAST, utils};
99

@@ -13,23 +13,20 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
1313
return;
1414
};
1515

16-
match cast_from.kind() {
17-
ty::FnDef(..) | ty::FnPtr(..) => {
18-
let mut applicability = Applicability::MaybeIncorrect;
16+
if cast_from.is_fn() {
17+
let mut applicability = Applicability::MaybeIncorrect;
1918

20-
if to_nbits >= cx.tcx.data_layout.pointer_size().bits() && !cast_to.is_usize() {
21-
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
22-
span_lint_and_sugg(
23-
cx,
24-
FN_TO_NUMERIC_CAST,
25-
expr.span,
26-
format!("casting function pointer `{from_snippet}` to `{cast_to}`"),
27-
"try",
28-
format!("{from_snippet} as usize"),
29-
applicability,
30-
);
31-
}
32-
},
33-
_ => {},
19+
if to_nbits >= cx.tcx.data_layout.pointer_size().bits() && !cast_to.is_usize() {
20+
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
21+
span_lint_and_sugg(
22+
cx,
23+
FN_TO_NUMERIC_CAST,
24+
expr.span,
25+
format!("casting function pointer `{from_snippet}` to `{cast_to}`"),
26+
"try",
27+
format!("{from_snippet} as usize"),
28+
applicability,
29+
);
30+
}
3431
}
3532
}

clippy_lints/src/casts/fn_to_numeric_cast_any.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ use clippy_utils::source::snippet_with_applicability;
33
use rustc_errors::Applicability;
44
use rustc_hir::Expr;
55
use rustc_lint::LateContext;
6-
use rustc_middle::ty::{self, Ty};
6+
use rustc_middle::ty::Ty;
77

88
use super::FN_TO_NUMERIC_CAST_ANY;
99

1010
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
1111
// We allow casts from any function type to any function type.
12-
match cast_to.kind() {
13-
ty::FnDef(..) | ty::FnPtr(..) => return,
14-
_ => { /* continue to checks */ },
12+
if cast_to.is_fn() {
13+
return;
1514
}
1615

17-
if let ty::FnDef(..) | ty::FnPtr(..) = cast_from.kind() {
16+
if cast_from.is_fn() {
1817
let mut applicability = Applicability::MaybeIncorrect;
1918
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "..", &mut applicability);
2019

clippy_lints/src/casts/fn_to_numeric_cast_with_truncation.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::source::snippet_with_applicability;
33
use rustc_errors::Applicability;
44
use rustc_hir::Expr;
55
use rustc_lint::LateContext;
6-
use rustc_middle::ty::{self, Ty};
6+
use rustc_middle::ty::Ty;
77

88
use super::{FN_TO_NUMERIC_CAST_WITH_TRUNCATION, utils};
99

@@ -12,23 +12,20 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
1212
let Some(to_nbits) = utils::int_ty_to_nbits(cx.tcx, cast_to) else {
1313
return;
1414
};
15-
match cast_from.kind() {
16-
ty::FnDef(..) | ty::FnPtr(..) => {
17-
let mut applicability = Applicability::MaybeIncorrect;
18-
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
15+
if cast_from.is_fn() {
16+
let mut applicability = Applicability::MaybeIncorrect;
17+
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
1918

20-
if to_nbits < cx.tcx.data_layout.pointer_size().bits() {
21-
span_lint_and_sugg(
22-
cx,
23-
FN_TO_NUMERIC_CAST_WITH_TRUNCATION,
24-
expr.span,
25-
format!("casting function pointer `{from_snippet}` to `{cast_to}`, which truncates the value"),
26-
"try",
27-
format!("{from_snippet} as usize"),
28-
applicability,
29-
);
30-
}
31-
},
32-
_ => {},
19+
if to_nbits < cx.tcx.data_layout.pointer_size().bits() {
20+
span_lint_and_sugg(
21+
cx,
22+
FN_TO_NUMERIC_CAST_WITH_TRUNCATION,
23+
expr.span,
24+
format!("casting function pointer `{from_snippet}` to `{cast_to}`, which truncates the value"),
25+
"try",
26+
format!("{from_snippet} as usize"),
27+
applicability,
28+
);
29+
}
3330
}
3431
}

clippy_lints/src/mixed_read_write_in_expression.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,11 @@ impl<'tcx> Visitor<'tcx> for DivergenceVisitor<'_, 'tcx> {
171171
ExprKind::Continue(_) | ExprKind::Break(_, _) | ExprKind::Ret(_) => self.report_diverging_sub_expr(e),
172172
ExprKind::Call(func, _) => {
173173
let typ = self.cx.typeck_results().expr_ty(func);
174-
match typ.kind() {
175-
ty::FnDef(..) | ty::FnPtr(..) => {
176-
let sig = typ.fn_sig(self.cx.tcx);
177-
if self.cx.tcx.instantiate_bound_regions_with_erased(sig).output().kind() == &ty::Never {
178-
self.report_diverging_sub_expr(e);
179-
}
180-
},
181-
_ => {},
174+
if typ.is_fn() {
175+
let sig = typ.fn_sig(self.cx.tcx);
176+
if self.cx.tcx.instantiate_bound_regions_with_erased(sig).output().kind() == &ty::Never {
177+
self.report_diverging_sub_expr(e);
178+
}
182179
}
183180
},
184181
ExprKind::MethodCall(..) => {

clippy_lints/src/mut_reference.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn check_arguments<'tcx>(
7979
name: &str,
8080
fn_kind: &str,
8181
) {
82-
if let ty::FnDef(..) | ty::FnPtr(..) = type_definition.kind() {
82+
if type_definition.is_fn() {
8383
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
8484
for (argument, parameter) in iter::zip(arguments, parameters) {
8585
if let ty::Ref(_, _, Mutability::Not) | ty::RawPtr(_, Mutability::Not) = parameter.kind()

clippy_utils/src/ty/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,7 @@ pub fn peel_mid_ty_refs_is_mutable(ty: Ty<'_>) -> (Ty<'_>, usize, Mutability) {
492492

493493
/// Returns `true` if the given type is an `unsafe` function.
494494
pub fn type_is_unsafe_function<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
495-
match ty.kind() {
496-
ty::FnDef(..) | ty::FnPtr(..) => ty.fn_sig(cx.tcx).safety().is_unsafe(),
497-
_ => false,
498-
}
495+
ty.is_fn() && ty.fn_sig(cx.tcx).safety().is_unsafe()
499496
}
500497

501498
/// Returns the base type for HIR references and pointers.

0 commit comments

Comments
 (0)