@@ -3,7 +3,7 @@ use rustc_hir::{Block, ExprKind};
33use rustc_lint:: { LateContext , LateLintPass } ;
44use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
55
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 } ;
77use rustc_errors:: Applicability ;
88
99declare_clippy_lint ! {
@@ -44,14 +44,14 @@ declare_clippy_lint! {
4444declare_lint_pass ! ( BoolToIntWithIf => [ BOOL_TO_INT_WITH_IF ] ) ;
4545
4646impl < ' 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) ;
5050 }
5151 }
5252}
5353
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 > ) {
5555 if let ExprKind :: If ( check, then, Some ( else_) ) = expr. kind
5656 && let Some ( then_lit) = int_literal ( then)
5757 && 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
6666 } ;
6767 let mut applicability = Applicability :: MachineApplicable ;
6868 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) ;
7070 if inverted {
7171 sugg = !sugg;
7272 }
7373 sugg
7474 } ;
7575
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
7777
7878 let suggestion = {
79- let wrap_in_curly = is_else_clause ( ctx . tcx , expr) ;
79+ let wrap_in_curly = is_else_clause ( cx . tcx , expr) ;
8080 let mut s = Sugg :: NonParen ( format ! ( "{ty}::from({snippet})" ) . into ( ) ) ;
8181 if wrap_in_curly {
8282 s = s. blockify ( ) ;
@@ -87,7 +87,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
8787 let into_snippet = snippet. clone ( ) . maybe_par ( ) ;
8888 let as_snippet = snippet. as_ty ( ty) ;
8989
90- span_lint_and_then ( ctx ,
90+ span_lint_and_then ( cx ,
9191 BOOL_TO_INT_WITH_IF ,
9292 expr. span ,
9393 "boolean to int conversion using if" ,
0 commit comments