diff options
| author | bors <bors@rust-lang.org> | 2023-11-27 20:26:31 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-11-27 20:26:31 +0000 |
| commit | 003e910760ef75196ca26e29c4fc83c2f418f693 (patch) | |
| tree | 775488d63f5c72bd1f7dc414b67d09c5400fa1e6 /clippy_lints | |
| parent | caa73941f853a324ee0526387b1bd09526decf50 (diff) | |
| parent | 3e83a521e45fb6221099673f6d295f0e59b0ecf2 (diff) | |
Auto merge of #11817 - y21:ptr_arg_mut_ref, r=Alexendoo
[`ptr_arg`]: recognize methods that also exist on slices Fixes #11816 Not a new lint, just a very small improvement to the existing `ptr_arg` lint which would have caught the linked issue. The problem was that the lint checks if a `Vec`-specific method was called, that is, if the receiver is `Vec<_>`. This is the case for `len` and `is_empty`, however these methods also exist on slices so we can still lint there. This logic exists in a different lint, so we can just reuse that here. Interestingly, there was even a comment up top that explained what it should have been doing, but the logic for it just wasn't there? changelog: [`ptr_arg`]: recognize methods that also exist on slices <sub>Also, this is my 100th PR to clippy 🎉 </sub>
Diffstat (limited to 'clippy_lints')
| -rw-r--r-- | clippy_lints/src/ptr.rs | 4 | ||||
| -rw-r--r-- | clippy_lints/src/vec.rs | 2 |
2 files changed, 4 insertions, 2 deletions
diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 16581a934f2..90e38fcdc72 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -28,6 +28,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`, @@ -660,7 +662,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 d49d3cc4cc9..ba958c5b392 100644 --- a/clippy_lints/src/vec.rs +++ b/clippy_lints/src/vec.rs @@ -57,7 +57,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 { |
