[ptr_arg]: recognize methods that also exist on slices
This commit is contained in:
parent
abf01e469b
commit
bb694615b8
6 changed files with 65 additions and 46 deletions
|
|
@ -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();
|
||||
},
|
||||
_ => (),
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue