@@ -8,7 +8,7 @@ use rustc_hir::def::{DefKind, Res};
88use rustc_hir:: { Arm , BinOpKind , Expr , ExprKind , Guard , MatchSource , Node , Pat , PatKind } ;
99use rustc_lint:: LateContext ;
1010use rustc_span:: symbol:: Ident ;
11- use rustc_span:: Span ;
11+ use rustc_span:: { Span , Symbol } ;
1212use std:: borrow:: Cow ;
1313use std:: ops:: ControlFlow ;
1414
@@ -105,50 +105,62 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
105105 && let ExprKind :: MethodCall ( path, recv, args, ..) = if_expr. kind
106106 && let Some ( binding) = get_pat_binding ( cx, recv, outer_arm)
107107 {
108- let ty = cx. typeck_results ( ) . expr_ty ( recv) . peel_refs ( ) ;
109- let slice_like = ty. is_slice ( ) || ty. is_array ( ) ;
110-
111- let sugg = if path. ident . name == sym ! ( is_empty) {
112- // `s if s.is_empty()` becomes ""
113- // `arr if arr.is_empty()` becomes []
108+ check_method_calls ( cx, outer_arm, path. ident . name , recv, args, if_expr, & binding) ;
109+ }
110+ }
111+ }
114112
115- if ty. is_str ( ) {
116- r#""""# . into ( )
117- } else if slice_like {
118- "[]" . into ( )
119- } else {
120- continue ;
121- }
122- } else if slice_like
123- && let Some ( needle) = args. first ( )
124- && let ExprKind :: AddrOf ( .., needle) = needle. kind
125- && let ExprKind :: Array ( needles) = needle. kind
126- && needles. iter ( ) . all ( |needle| expr_can_be_pat ( cx, needle) )
127- {
128- // `arr if arr.starts_with(&[123])` becomes [123, ..]
129- // `arr if arr.ends_with(&[123])` becomes [.., 123]
130- // `arr if arr.starts_with(&[])` becomes [..] (why would anyone write this?)
113+ fn check_method_calls < ' tcx > (
114+ cx : & LateContext < ' tcx > ,
115+ arm : & Arm < ' tcx > ,
116+ method : Symbol ,
117+ recv : & Expr < ' _ > ,
118+ args : & [ Expr < ' _ > ] ,
119+ if_expr : & Expr < ' _ > ,
120+ binding : & PatBindingInfo ,
121+ ) {
122+ let ty = cx. typeck_results ( ) . expr_ty ( recv) . peel_refs ( ) ;
123+ let slice_like = ty. is_slice ( ) || ty. is_array ( ) ;
131124
132- let mut sugg = snippet ( cx, needle. span , "<needle>" ) . into_owned ( ) ;
125+ let sugg = if method == sym ! ( is_empty) {
126+ // `s if s.is_empty()` becomes ""
127+ // `arr if arr.is_empty()` becomes []
133128
134- if needles. is_empty ( ) {
135- sugg. insert_str ( 1 , ".." ) ;
136- } else if path. ident . name == sym ! ( starts_with) {
137- sugg. insert_str ( sugg. len ( ) - 1 , ", .." ) ;
138- } else if path. ident . name == sym ! ( ends_with) {
139- sugg. insert_str ( 1 , ".., " ) ;
140- } else {
141- continue ;
142- }
129+ if ty. is_str ( ) {
130+ r#""""# . into ( )
131+ } else if slice_like {
132+ "[]" . into ( )
133+ } else {
134+ return ;
135+ }
136+ } else if slice_like
137+ && let Some ( needle) = args. first ( )
138+ && let ExprKind :: AddrOf ( .., needle) = needle. kind
139+ && let ExprKind :: Array ( needles) = needle. kind
140+ && needles. iter ( ) . all ( |needle| expr_can_be_pat ( cx, needle) )
141+ {
142+ // `arr if arr.starts_with(&[123])` becomes [123, ..]
143+ // `arr if arr.ends_with(&[123])` becomes [.., 123]
144+ // `arr if arr.starts_with(&[])` becomes [..] (why would anyone write this?)
143145
144- sugg. into ( )
145- } else {
146- continue ;
147- } ;
146+ let mut sugg = snippet ( cx, needle. span , "<needle>" ) . into_owned ( ) ;
148147
149- emit_redundant_guards ( cx, outer_arm, if_expr. span , sugg, & binding, None ) ;
148+ if needles. is_empty ( ) {
149+ sugg. insert_str ( 1 , ".." ) ;
150+ } else if method == sym ! ( starts_with) {
151+ sugg. insert_str ( sugg. len ( ) - 1 , ", .." ) ;
152+ } else if method == sym ! ( ends_with) {
153+ sugg. insert_str ( 1 , ".., " ) ;
154+ } else {
155+ return ;
150156 }
151- }
157+
158+ sugg. into ( )
159+ } else {
160+ return ;
161+ } ;
162+
163+ emit_redundant_guards ( cx, arm, if_expr. span , sugg, binding, None ) ;
152164}
153165
154166struct PatBindingInfo {
0 commit comments