1- use rustc_ast:: { ExprPrecedence , LitKind } ;
1+ use rustc_ast:: LitKind ;
22use 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, source :: snippet_block_with_applicability } ;
6+ use clippy_utils:: { diagnostics:: span_lint_and_then, is_else_clause, sugg :: Sugg } ;
77use rustc_errors:: Applicability ;
88
99declare_clippy_lint ! {
@@ -69,28 +69,27 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
6969 return ;
7070 } ;
7171 let mut applicability = Applicability :: MachineApplicable ;
72- let snippet = snippet_block_with_applicability ( ctx, check. span , ".." , None , & mut applicability) ;
73-
74- let invert = if inverted { "!" } else { "" } ;
75- let need_parens = should_have_parentheses ( check) ;
76-
77- let snippet_with_braces = {
78- let ( left_paren, right_paren) = if need_parens { ( "(" , ")" ) } else { ( "" , "" ) } ;
79- format ! ( "{invert}{left_paren}{snippet}{right_paren}" )
72+ let snippet = {
73+ let mut sugg = Sugg :: hir_with_applicability ( ctx, check, ".." , & mut applicability) ;
74+ if inverted {
75+ sugg = !sugg;
76+ }
77+ sugg
8078 } ;
8179
8280 let ty = ctx. typeck_results ( ) . expr_ty ( then_lit) ; // then and else must be of same type
8381
8482 let suggestion = {
8583 let wrap_in_curly = is_else_clause ( ctx. tcx , expr) ;
86- let ( left_curly , right_curly ) = if wrap_in_curly { ( "{" , "}" ) } else { ( "" , "" ) } ;
87- let ( left_paren , right_paren ) = if inverted && need_parens { ( "(" , ")" ) } else { ( "" , "" ) } ;
88- format ! (
89- "{left_curly}{ty}::from({invert}{left_paren}{snippet}{right_paren}){right_curly}"
90- )
84+ let mut s = Sugg :: NonParen ( format ! ( "{ty}::from({snippet})" ) . into ( ) ) ;
85+ if wrap_in_curly {
86+ s = s . blockify ( ) ;
87+ }
88+ s
9189 } ; // when used in else clause if statement should be wrapped in curly braces
9290
93- let ( inverted_left_paren, inverted_right_paren) = if inverted { ( "(" , ")" ) } else { ( "" , "" ) } ;
91+ let into_snippet = snippet. clone ( ) . maybe_par ( ) ;
92+ let as_snippet = snippet. as_ty ( ty) ;
9493
9594 span_lint_and_then ( ctx,
9695 BOOL_TO_INT_WITH_IF ,
@@ -103,7 +102,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
103102 suggestion,
104103 applicability,
105104 ) ;
106- diag. note ( format ! ( "`{snippet_with_braces} as {ty} ` or `{inverted_left_paren}{snippet_with_braces}{inverted_right_paren }.into()` can also be valid options" ) ) ;
105+ diag. note ( format ! ( "`{as_snippet} ` or `{into_snippet }.into()` can also be valid options" ) ) ;
107106 } ) ;
108107 } ;
109108}
@@ -135,7 +134,3 @@ fn check_int_literal_equals_val<'tcx>(expr: &'tcx rustc_hir::Expr<'tcx>, expecte
135134 false
136135 }
137136}
138-
139- fn should_have_parentheses < ' tcx > ( check : & ' tcx rustc_hir:: Expr < ' tcx > ) -> bool {
140- check. precedence ( ) . order ( ) < ExprPrecedence :: Cast . order ( )
141- }
0 commit comments