1
1
use clippy_utils:: consts:: { constant_context, Constant } ;
2
2
use clippy_utils:: diagnostics:: span_lint;
3
- use clippy_utils:: is_path_diagnostic_item;
4
- use if_chain:: if_chain;
5
- use rustc_ast:: LitKind ;
3
+ use clippy_utils:: { is_integer_literal, is_path_diagnostic_item} ;
6
4
use rustc_hir:: { Expr , ExprKind } ;
7
5
use rustc_lint:: LateContext ;
8
6
use rustc_middle:: ty:: Ty ;
@@ -19,37 +17,28 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'t
19
17
20
18
// Catching transmute over constants that resolve to `null`.
21
19
let mut const_eval_context = constant_context ( cx, cx. typeck_results ( ) ) ;
22
- if_chain ! {
23
- if let ExprKind :: Path ( ref _qpath) = arg. kind;
24
- if let Some ( Constant :: RawPtr ( x) ) = const_eval_context. expr( arg) ;
25
- if x == 0 ;
26
- then {
27
- span_lint( cx, TRANSMUTING_NULL , expr. span, LINT_MSG ) ;
28
- return true ;
29
- }
20
+ if let ExprKind :: Path ( ref _qpath) = arg. kind &&
21
+ let Some ( Constant :: RawPtr ( x) ) = const_eval_context. expr ( arg) &&
22
+ x == 0
23
+ {
24
+ span_lint ( cx, TRANSMUTING_NULL , expr. span , LINT_MSG ) ;
25
+ return true ;
30
26
}
31
27
32
28
// Catching:
33
29
// `std::mem::transmute(0 as *const i32)`
34
- if_chain ! {
35
- if let ExprKind :: Cast ( inner_expr, _cast_ty) = arg. kind;
36
- if let ExprKind :: Lit ( ref lit) = inner_expr. kind;
37
- if let LitKind :: Int ( 0 , _) = lit. node;
38
- then {
39
- span_lint( cx, TRANSMUTING_NULL , expr. span, LINT_MSG ) ;
40
- return true ;
41
- }
30
+ if let ExprKind :: Cast ( inner_expr, _cast_ty) = arg. kind && is_integer_literal ( inner_expr, 0 ) {
31
+ span_lint ( cx, TRANSMUTING_NULL , expr. span , LINT_MSG ) ;
32
+ return true ;
42
33
}
43
34
44
35
// Catching:
45
36
// `std::mem::transmute(std::ptr::null::<i32>())`
46
- if_chain ! {
47
- if let ExprKind :: Call ( func1, [ ] ) = arg. kind;
48
- if is_path_diagnostic_item( cx, func1, sym:: ptr_null) ;
49
- then {
50
- span_lint( cx, TRANSMUTING_NULL , expr. span, LINT_MSG ) ;
51
- return true ;
52
- }
37
+ if let ExprKind :: Call ( func1, [ ] ) = arg. kind &&
38
+ is_path_diagnostic_item ( cx, func1, sym:: ptr_null)
39
+ {
40
+ span_lint ( cx, TRANSMUTING_NULL , expr. span , LINT_MSG ) ;
41
+ return true ;
53
42
}
54
43
55
44
// FIXME:
0 commit comments