1
- use rustc_ast:: { ExprPrecedence , LitKind } ;
1
+ use rustc_ast:: LitKind ;
2
2
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, source :: snippet_block_with_applicability } ;
6
+ use clippy_utils:: { diagnostics:: span_lint_and_then, is_else_clause, sugg :: Sugg } ;
7
7
use rustc_errors:: Applicability ;
8
8
9
9
declare_clippy_lint ! {
@@ -69,28 +69,27 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
69
69
return ;
70
70
} ;
71
71
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
80
78
} ;
81
79
82
80
let ty = ctx. typeck_results ( ) . expr_ty ( then_lit) ; // then and else must be of same type
83
81
84
82
let suggestion = {
85
83
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
91
89
} ; // when used in else clause if statement should be wrapped in curly braces
92
90
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) ;
94
93
95
94
span_lint_and_then ( ctx,
96
95
BOOL_TO_INT_WITH_IF ,
@@ -103,7 +102,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
103
102
suggestion,
104
103
applicability,
105
104
) ;
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" ) ) ;
107
106
} ) ;
108
107
} ;
109
108
}
@@ -135,7 +134,3 @@ fn check_int_literal_equals_val<'tcx>(expr: &'tcx rustc_hir::Expr<'tcx>, expecte
135
134
false
136
135
}
137
136
}
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