@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
22use clippy_utils:: higher:: { get_vec_init_kind, VecInitKind } ;
33use clippy_utils:: ty:: { is_type_diagnostic_item, is_uninit_value_valid_for_ty} ;
44use clippy_utils:: { is_lint_allowed, path_to_local_id, peel_hir_expr_while, SpanlessEq } ;
5+ use rustc_ast:: ast:: LitKind ;
56use rustc_hir:: { Block , Expr , ExprKind , HirId , PatKind , PathSegment , Stmt , StmtKind } ;
67use rustc_lint:: { LateContext , LateLintPass } ;
78use rustc_middle:: lint:: in_external_macro;
@@ -211,9 +212,12 @@ fn extract_set_len_self<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Opt
211212 }
212213 } ) ;
213214 match expr. kind {
214- ExprKind :: MethodCall ( path, self_expr, [ _ ] , _) => {
215+ ExprKind :: MethodCall ( path, self_expr, [ arg ] , _) => {
215216 let self_type = cx. typeck_results ( ) . expr_ty ( self_expr) . peel_refs ( ) ;
216- if is_type_diagnostic_item ( cx, self_type, sym:: Vec ) && path. ident . name . as_str ( ) == "set_len" {
217+ if is_type_diagnostic_item ( cx, self_type, sym:: Vec )
218+ && path. ident . name . as_str ( ) == "set_len"
219+ && !is_literal_zero ( arg)
220+ {
217221 Some ( ( self_expr, expr. span ) )
218222 } else {
219223 None
@@ -222,3 +226,13 @@ fn extract_set_len_self<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Opt
222226 _ => None ,
223227 }
224228}
229+
230+ fn is_literal_zero ( arg : & Expr < ' _ > ) -> bool {
231+ if let ExprKind :: Lit ( lit) = & arg. kind
232+ && let LitKind :: Int ( 0 , _) = lit. node
233+ {
234+ true
235+ } else {
236+ false
237+ }
238+ }
0 commit comments