@@ -102,22 +102,48 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
102102 None ,
103103 ) ;
104104 } else if let Guard :: If ( if_expr) = guard
105- && let ExprKind :: MethodCall ( path, recv, ..) = if_expr. kind
105+ && let ExprKind :: MethodCall ( path, recv, args , ..) = if_expr. kind
106106 && let Some ( binding) = get_pat_binding ( cx, recv, outer_arm)
107107 {
108108 let ty = cx. typeck_results ( ) . expr_ty ( recv) . peel_refs ( ) ;
109109 let slice_like = ty. is_slice ( ) || ty. is_array ( ) ;
110110
111- if path. ident . name == sym ! ( is_empty) {
111+ let sugg = if path. ident . name == sym ! ( is_empty) {
112112 // `s if s.is_empty()` becomes ""
113113 // `arr if arr.is_empty()` becomes []
114114
115115 if ty. is_str ( ) {
116- emit_redundant_guards ( cx , outer_arm , if_expr . span , r#""""# . into ( ) , & binding , None )
116+ r#""""# . into ( )
117117 } else if slice_like {
118- emit_redundant_guards ( cx, outer_arm, if_expr. span , "[]" . into ( ) , & binding, None )
118+ "[]" . into ( )
119+ } else {
120+ continue ;
119121 }
120- }
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+
131+ let mut sugg = snippet ( cx, needle. span , "<needle>" ) . into_owned ( ) ;
132+
133+ if path. ident . name == sym ! ( starts_with) {
134+ sugg. insert_str ( sugg. len ( ) - 1 , ", .." ) ;
135+ } else if path. ident . name == sym ! ( ends_with) {
136+ sugg. insert_str ( 1 , ".., " ) ;
137+ } else {
138+ continue ;
139+ }
140+
141+ sugg. into ( )
142+ } else {
143+ continue ;
144+ } ;
145+
146+ emit_redundant_guards ( cx, outer_arm, if_expr. span , sugg, & binding, None ) ;
121147 }
122148 }
123149}
0 commit comments