Skip to content

Commit 20f501a

Browse files
committed
Improve code style further
1 parent 9b2fc8e commit 20f501a

File tree

2 files changed

+73
-74
lines changed

2 files changed

+73
-74
lines changed

clippy_lints/src/fn_null_check.rs

+39-39
Original file line numberDiff line numberDiff line change
@@ -55,50 +55,50 @@ fn is_fn_ptr_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
5555

5656
impl<'tcx> LateLintPass<'tcx> for FnNullCheck {
5757
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
58-
// Catching:
59-
// (fn_ptr as *<const/mut> <ty>).is_null()
60-
if let ExprKind::MethodCall(method_name, receiver, _, _) = expr.kind
61-
&& method_name.ident.as_str() == "is_null"
62-
&& is_fn_ptr_cast(cx, receiver)
63-
{
58+
match expr.kind {
59+
ExprKind::MethodCall(method_name, receiver, _, _)
60+
if method_name.ident.as_str() == "is_null" && is_fn_ptr_cast(cx, receiver) =>
61+
{
6462
lint_expr(cx, expr);
65-
return;
66-
}
63+
},
6764

68-
if let ExprKind::Binary(op, left, right) = expr.kind
69-
&& let BinOpKind::Eq = op.node
70-
{
71-
let to_check: &Expr<'_>;
72-
if is_fn_ptr_cast(cx, left) {
73-
to_check = right;
74-
} else if is_fn_ptr_cast(cx, right) {
75-
to_check = left;
76-
} else {
77-
return;
78-
}
65+
ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq) => {
66+
let to_check: &Expr<'_>;
67+
if is_fn_ptr_cast(cx, left) {
68+
to_check = right;
69+
} else if is_fn_ptr_cast(cx, right) {
70+
to_check = left;
71+
} else {
72+
return;
73+
}
7974

80-
// Catching:
81-
// (fn_ptr as *<const/mut> <ty>) == <const that evaluates to null_ptr>
82-
let c = constant(cx, cx.typeck_results(), to_check);
83-
if let Some((Constant::RawPtr(0), _)) = c {
84-
lint_expr(cx, expr);
85-
return;
86-
}
75+
match to_check.kind {
76+
// Catching:
77+
// (fn_ptr as *<const/mut> <ty>) == (0 as <ty>)
78+
ExprKind::Cast(cast_expr, _) if is_integer_literal(cast_expr, 0) => {
79+
lint_expr(cx, expr);
80+
},
8781

88-
// Catching:
89-
// (fn_ptr as *<const/mut> <ty>) == (0 as <ty>)
90-
if let ExprKind::Cast(cast_expr, _) = to_check.kind && is_integer_literal(cast_expr, 0) {
91-
lint_expr(cx, expr);
92-
return;
93-
}
82+
// Catching:
83+
// (fn_ptr as *<const/mut> <ty>) == std::ptr::null()
84+
ExprKind::Call(func, []) if is_path_diagnostic_item(cx, func, sym::ptr_null) => {
85+
lint_expr(cx, expr);
86+
},
9487

95-
// Catching:
96-
// (fn_ptr as *<const/mut> <ty>) == std::ptr::null()
97-
if let ExprKind::Call(func, []) = to_check.kind &&
98-
is_path_diagnostic_item(cx, func, sym::ptr_null)
99-
{
100-
lint_expr(cx, expr);
101-
}
88+
// Catching:
89+
// (fn_ptr as *<const/mut> <ty>) == <const that evaluates to null_ptr>
90+
_ if matches!(
91+
constant(cx, cx.typeck_results(), to_check),
92+
Some((Constant::RawPtr(0), _))
93+
) =>
94+
{
95+
lint_expr(cx, expr);
96+
},
97+
98+
_ => {},
99+
}
100+
},
101+
_ => {},
102102
}
103103
}
104104
}

clippy_lints/src/transmute/transmute_null_to_fn.rs

+34-35
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const NOTE_MSG: &str = "this transmute results in undefined behavior";
1313
const HELP_MSG: &str =
1414
"try wrapping your function pointer type in `Option<T>` instead, and using `None` as a null pointer value";
1515

16+
fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
17+
span_lint_and_then(cx, TRANSMUTE_NULL_TO_FN, expr.span, LINT_MSG, |diag| {
18+
diag.span_label(expr.span, NOTE_MSG);
19+
diag.help(HELP_MSG);
20+
});
21+
}
22+
1623
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'tcx Expr<'_>, to_ty: Ty<'tcx>) -> bool {
1724
if !to_ty.is_fn() {
1825
return false;
@@ -21,42 +28,34 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'t
2128
// Catching:
2229
// transmute over constants that resolve to `null`.
2330
let mut const_eval_context = constant_context(cx, cx.typeck_results());
24-
if let ExprKind::Path(ref _qpath) = arg.kind &&
25-
let Some(Constant::RawPtr(0)) = const_eval_context.expr(arg)
26-
{
27-
span_lint_and_then(cx, TRANSMUTE_NULL_TO_FN, expr.span, LINT_MSG, |diag| {
28-
diag.span_label(expr.span, NOTE_MSG);
29-
diag.help(HELP_MSG);
30-
});
31-
return true;
32-
}
3331

34-
// Catching:
35-
// `std::mem::transmute(0 as *const i32)`
36-
if let ExprKind::Cast(inner_expr, _cast_ty) = arg.kind && is_integer_literal(inner_expr, 0) {
37-
span_lint_and_then(cx, TRANSMUTE_NULL_TO_FN, expr.span, LINT_MSG, |diag| {
38-
diag.span_label(expr.span, NOTE_MSG);
39-
diag.help(HELP_MSG);
40-
});
41-
return true;
42-
}
32+
match arg.kind {
33+
ExprKind::Path(ref _qpath) if matches!(const_eval_context.expr(arg), Some(Constant::RawPtr(0))) => {
34+
lint_expr(cx, expr);
35+
true
36+
},
4337

44-
// Catching:
45-
// `std::mem::transmute(std::ptr::null::<i32>())`
46-
if let ExprKind::Call(func1, []) = arg.kind &&
47-
is_path_diagnostic_item(cx, func1, sym::ptr_null)
48-
{
49-
span_lint_and_then(cx, TRANSMUTE_NULL_TO_FN, expr.span, LINT_MSG, |diag| {
50-
diag.span_label(expr.span, NOTE_MSG);
51-
diag.help(HELP_MSG);
52-
});
53-
return true;
54-
}
38+
// Catching:
39+
// `std::mem::transmute(0 as *const i32)`
40+
ExprKind::Cast(inner_expr, _cast_ty) if is_integer_literal(inner_expr, 0) => {
41+
lint_expr(cx, expr);
42+
true
43+
},
5544

56-
// FIXME:
57-
// Also catch transmutations of variables which are known nulls.
58-
// To do this, MIR const propagation seems to be the better tool.
59-
// Whenever MIR const prop routines are more developed, this will
60-
// become available. As of this writing (25/03/19) it is not yet.
61-
false
45+
// Catching:
46+
// `std::mem::transmute(std::ptr::null::<i32>())`
47+
ExprKind::Call(func1, []) if is_path_diagnostic_item(cx, func1, sym::ptr_null) => {
48+
lint_expr(cx, expr);
49+
true
50+
},
51+
52+
_ => {
53+
// FIXME:
54+
// Also catch transmutations of variables which are known nulls.
55+
// To do this, MIR const propagation seems to be the better tool.
56+
// Whenever MIR const prop routines are more developed, this will
57+
// become available. As of this writing (25/03/19) it is not yet.
58+
false
59+
},
60+
}
6261
}

0 commit comments

Comments
 (0)