@@ -3,7 +3,7 @@ use rustc_hir::{Block, ExprKind};
3
3
use rustc_lint:: { LateContext , LateLintPass } ;
4
4
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
5
5
6
- use clippy_utils:: { diagnostics:: span_lint_and_then, is_else_clause, is_integer_literal, sugg:: Sugg } ;
6
+ use clippy_utils:: { diagnostics:: span_lint_and_then, in_constant , is_else_clause, is_integer_literal, sugg:: Sugg } ;
7
7
use rustc_errors:: Applicability ;
8
8
9
9
declare_clippy_lint ! {
@@ -44,14 +44,14 @@ declare_clippy_lint! {
44
44
declare_lint_pass ! ( BoolToIntWithIf => [ BOOL_TO_INT_WITH_IF ] ) ;
45
45
46
46
impl < ' tcx > LateLintPass < ' tcx > for BoolToIntWithIf {
47
- fn check_expr ( & mut self , ctx : & LateContext < ' tcx > , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) {
48
- if !expr. span . from_expansion ( ) {
49
- check_if_else ( ctx , expr) ;
47
+ fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) {
48
+ if !expr. span . from_expansion ( ) && ! in_constant ( cx , expr . hir_id ) {
49
+ check_if_else ( cx , expr) ;
50
50
}
51
51
}
52
52
}
53
53
54
- fn check_if_else < ' tcx > ( ctx : & LateContext < ' tcx > , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) {
54
+ fn check_if_else < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) {
55
55
if let ExprKind :: If ( check, then, Some ( else_) ) = expr. kind
56
56
&& let Some ( then_lit) = int_literal ( then)
57
57
&& let Some ( else_lit) = int_literal ( else_)
@@ -66,17 +66,17 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
66
66
} ;
67
67
let mut applicability = Applicability :: MachineApplicable ;
68
68
let snippet = {
69
- let mut sugg = Sugg :: hir_with_applicability ( ctx , check, ".." , & mut applicability) ;
69
+ let mut sugg = Sugg :: hir_with_applicability ( cx , check, ".." , & mut applicability) ;
70
70
if inverted {
71
71
sugg = !sugg;
72
72
}
73
73
sugg
74
74
} ;
75
75
76
- let ty = ctx . typeck_results ( ) . expr_ty ( then_lit) ; // then and else must be of same type
76
+ let ty = cx . typeck_results ( ) . expr_ty ( then_lit) ; // then and else must be of same type
77
77
78
78
let suggestion = {
79
- let wrap_in_curly = is_else_clause ( ctx . tcx , expr) ;
79
+ let wrap_in_curly = is_else_clause ( cx . tcx , expr) ;
80
80
let mut s = Sugg :: NonParen ( format ! ( "{ty}::from({snippet})" ) . into ( ) ) ;
81
81
if wrap_in_curly {
82
82
s = s. blockify ( ) ;
@@ -87,7 +87,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
87
87
let into_snippet = snippet. clone ( ) . maybe_par ( ) ;
88
88
let as_snippet = snippet. as_ty ( ty) ;
89
89
90
- span_lint_and_then ( ctx ,
90
+ span_lint_and_then ( cx ,
91
91
BOOL_TO_INT_WITH_IF ,
92
92
expr. span ,
93
93
"boolean to int conversion using if" ,
0 commit comments