@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
2
2
use clippy_utils:: higher:: { get_vec_init_kind, VecInitKind } ;
3
3
use clippy_utils:: ty:: { is_type_diagnostic_item, is_uninit_value_valid_for_ty} ;
4
4
use clippy_utils:: { is_lint_allowed, path_to_local_id, peel_hir_expr_while, SpanlessEq } ;
5
+ use rustc_ast:: ast:: LitKind ;
5
6
use rustc_hir:: { Block , Expr , ExprKind , HirId , PatKind , PathSegment , Stmt , StmtKind } ;
6
7
use rustc_lint:: { LateContext , LateLintPass } ;
7
8
use rustc_middle:: lint:: in_external_macro;
@@ -211,9 +212,12 @@ fn extract_set_len_self<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Opt
211
212
}
212
213
} ) ;
213
214
match expr. kind {
214
- ExprKind :: MethodCall ( path, self_expr, [ _ ] , _) => {
215
+ ExprKind :: MethodCall ( path, self_expr, [ arg ] , _) => {
215
216
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
+ {
217
221
Some ( ( self_expr, expr. span ) )
218
222
} else {
219
223
None
@@ -222,3 +226,13 @@ fn extract_set_len_self<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Opt
222
226
_ => None ,
223
227
}
224
228
}
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