Skip to content

Commit 1f4d944

Browse files
committed
inline clippy_utils::ptr into needless_pass_by_value
1 parent 44052c4 commit 1f4d944

File tree

3 files changed

+52
-58
lines changed

3 files changed

+52
-58
lines changed

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::ptr::get_spans;
32
use clippy_utils::source::{SpanRangeExt, snippet};
43
use clippy_utils::ty::{
54
implements_trait, implements_trait_with_env_from_iter, is_copy, is_type_diagnostic_item, is_type_lang_item,
65
};
7-
use clippy_utils::{is_self, peel_hir_ty_options};
6+
use clippy_utils::visitors::{Descend, for_each_expr_without_closures};
7+
use clippy_utils::{is_self, path_to_local_id, peel_hir_ty_options, strip_pat_refs, sym};
88
use rustc_abi::ExternAbi;
99
use rustc_errors::{Applicability, Diag};
1010
use rustc_hir::intravisit::FnKind;
1111
use rustc_hir::{
12-
Attribute, BindingMode, Body, FnDecl, GenericArg, HirId, HirIdSet, Impl, ItemKind, LangItem, Mutability, Node,
13-
PatKind, QPath, TyKind,
12+
Attribute, BindingMode, Body, BodyId, ExprKind, FnDecl, GenericArg, HirId, HirIdSet, Impl, ItemKind, LangItem,
13+
Mutability, Node, PatKind, QPath, TyKind,
1414
};
1515
use rustc_hir_typeck::expr_use_visitor as euv;
1616
use rustc_lint::{LateContext, LateLintPass};
@@ -19,10 +19,13 @@ use rustc_middle::ty::{self, Ty, TypeVisitableExt};
1919
use rustc_session::declare_lint_pass;
2020
use rustc_span::def_id::LocalDefId;
2121
use rustc_span::symbol::kw;
22-
use rustc_span::{Span, sym};
22+
use rustc_span::{Span, Symbol};
2323
use rustc_trait_selection::traits;
2424
use rustc_trait_selection::traits::misc::type_allowed_to_implement_copy;
2525

26+
use std::borrow::Cow;
27+
use std::ops::ControlFlow;
28+
2629
declare_clippy_lint! {
2730
/// ### What it does
2831
/// Checks for functions taking arguments by value, but not
@@ -340,3 +343,47 @@ impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt {
340343

341344
fn fake_read(&mut self, _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {}
342345
}
346+
347+
pub fn get_spans(
348+
cx: &LateContext<'_>,
349+
opt_body_id: Option<BodyId>,
350+
idx: usize,
351+
replacements: &[(Symbol, &'static str)],
352+
) -> Option<Vec<(Span, Cow<'static, str>)>> {
353+
if let Some(body) = opt_body_id.map(|id| cx.tcx.hir_body(id)) {
354+
if let PatKind::Binding(_, binding_id, _, _) = strip_pat_refs(body.params[idx].pat).kind {
355+
extract_clone_suggestions(cx, binding_id, replacements, body)
356+
} else {
357+
Some(vec![])
358+
}
359+
} else {
360+
Some(vec![])
361+
}
362+
}
363+
364+
fn extract_clone_suggestions<'tcx>(
365+
cx: &LateContext<'tcx>,
366+
id: HirId,
367+
replace: &[(Symbol, &'static str)],
368+
body: &'tcx Body<'_>,
369+
) -> Option<Vec<(Span, Cow<'static, str>)>> {
370+
let mut spans = Vec::new();
371+
for_each_expr_without_closures(body, |e| {
372+
if let ExprKind::MethodCall(seg, recv, [], _) = e.kind
373+
&& path_to_local_id(recv, id)
374+
{
375+
if seg.ident.name == sym::capacity {
376+
return ControlFlow::Break(());
377+
}
378+
for &(fn_name, suffix) in replace {
379+
if seg.ident.name == fn_name {
380+
spans.push((e.span, snippet(cx, recv.span, "_") + suffix));
381+
return ControlFlow::Continue(Descend::No);
382+
}
383+
}
384+
}
385+
ControlFlow::Continue(Descend::Yes)
386+
})
387+
.is_none()
388+
.then_some(spans)
389+
}

clippy_utils/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ pub mod mir;
6363
pub mod msrvs;
6464
pub mod numeric_literal;
6565
pub mod paths;
66-
pub mod ptr;
6766
pub mod qualify_min_const_fn;
6867
pub mod source;
6968
pub mod str_utils;

clippy_utils/src/ptr.rs

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)