@@ -10,21 +10,20 @@ use super::SLICED_STRING_AS_BYTES;
1010pub ( super ) fn check ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , recv : & Expr < ' _ > ) {
1111 if let ExprKind :: Index ( indexed, index, _) = recv. kind
1212 && is_range_literal ( index)
13+ && let ty = cx. typeck_results ( ) . expr_ty ( indexed) . peel_refs ( )
14+ && ( ty. is_str ( ) || is_type_lang_item ( cx, ty, LangItem :: String ) )
1315 {
14- let ty = cx. typeck_results ( ) . expr_ty ( indexed) . peel_refs ( ) ;
15- if ty. is_str ( ) || is_type_lang_item ( cx, ty, LangItem :: String ) {
16- let mut applicability = Applicability :: MaybeIncorrect ;
17- let stringish = snippet_with_applicability ( cx, indexed. span , ".." , & mut applicability) ;
18- let range = snippet_with_applicability ( cx, index. span , ".." , & mut applicability) ;
19- span_lint_and_sugg (
20- cx,
21- SLICED_STRING_AS_BYTES ,
22- expr. span ,
23- "calling `as_bytes` after slicing a string" ,
24- "try" ,
25- format ! ( "&{stringish}.as_bytes()[{range}]" ) ,
26- applicability,
27- ) ;
28- }
16+ let mut applicability = Applicability :: MaybeIncorrect ;
17+ let stringish = snippet_with_applicability ( cx, indexed. span , ".." , & mut applicability) ;
18+ let range = snippet_with_applicability ( cx, index. span , ".." , & mut applicability) ;
19+ span_lint_and_sugg (
20+ cx,
21+ SLICED_STRING_AS_BYTES ,
22+ expr. span ,
23+ "calling `as_bytes` after slicing a string" ,
24+ "try" ,
25+ format ! ( "&{stringish}.as_bytes()[{range}]" ) ,
26+ applicability,
27+ ) ;
2928 }
3029}
0 commit comments