about summary refs log tree commit diff
path: root/clippy_lints
diff options
context:
space:
mode:
authory21 <30553356+y21@users.noreply.github.com>2023-11-15 14:59:11 +0100
committery21 <30553356+y21@users.noreply.github.com>2023-11-15 14:59:11 +0100
commitbb694615b83545a90477f0ad3030fc0cd84a1960 (patch)
treeb66dbfc3faed24439561370a36f0fe43245376cd /clippy_lints
parentabf01e469b9f9a49fdf96eddb3537f9ab6ae2e4a (diff)
[`ptr_arg`]: recognize methods that also exist on slices
Diffstat (limited to 'clippy_lints')
-rw-r--r--clippy_lints/src/ptr.rs4
-rw-r--r--clippy_lints/src/vec.rs2
2 files changed, 4 insertions, 2 deletions
diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs
index 945b78185d9..6440a34dad6 100644
--- a/clippy_lints/src/ptr.rs
+++ b/clippy_lints/src/ptr.rs
@@ -29,6 +29,8 @@ use rustc_trait_selection::infer::InferCtxtExt as _;
 use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
 use std::{fmt, iter};
 
+use crate::vec::is_allowed_vec_method;
+
 declare_clippy_lint! {
     /// ### What it does
     /// This lint checks for function arguments of type `&String`, `&Vec`,
@@ -661,7 +663,7 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &'tcx Body<'_>, args:
                             },
                             // If the types match check for methods which exist on both types. e.g. `Vec::len` and
                             // `slice::len`
-                            ty::Adt(def, _) if def.did() == args.ty_did => {
+                            ty::Adt(def, _) if def.did() == args.ty_did && !is_allowed_vec_method(self.cx, e) => {
                                 set_skip_flag();
                             },
                             _ => (),
diff --git a/clippy_lints/src/vec.rs b/clippy_lints/src/vec.rs
index 9ad2ad2d195..78e7129a88e 100644
--- a/clippy_lints/src/vec.rs
+++ b/clippy_lints/src/vec.rs
@@ -58,7 +58,7 @@ fn adjusts_to_slice(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
 /// Checks if the given expression is a method call to a `Vec` method
 /// that also exists on slices. If this returns true, it means that
 /// this expression does not actually require a `Vec` and could just work with an array.
-fn is_allowed_vec_method(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
+pub fn is_allowed_vec_method(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
     const ALLOWED_METHOD_NAMES: &[&str] = &["len", "as_ptr", "is_empty"];
 
     if let ExprKind::MethodCall(path, ..) = e.kind {