Skip to content

Remove dead code in needless_pass_by_value #10944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions clippy_lints/src/needless_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ use clippy_utils::ty::{
use clippy_utils::{get_trait_def_id, is_self, paths};
use if_chain::if_chain;
use rustc_ast::ast::Attribute;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Applicability, Diagnostic};
use rustc_hir::intravisit::FnKind;
use rustc_hir::{
BindingAnnotation, Body, FnDecl, GenericArg, HirId, Impl, ItemKind, Mutability, Node, PatKind, QPath, TyKind,
};
use rustc_hir::{HirIdMap, HirIdSet, LangItem};
use rustc_hir::{HirIdSet, LangItem};
use rustc_hir_typeck::expr_use_visitor as euv;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -136,11 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {

// Collect moved variables and spans which will need dereferencings from the
// function body.
let MovedVariablesCtxt {
moved_vars,
spans_need_deref,
..
} = {
let MovedVariablesCtxt { moved_vars } = {
let mut ctx = MovedVariablesCtxt::default();
let infcx = cx.tcx.infer_ctxt().build();
euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id, cx.param_env, cx.typeck_results()).consume_body(body);
Expand Down Expand Up @@ -211,7 +206,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
}
}

let deref_span = spans_need_deref.get(&canonical_id);
if_chain! {
if is_type_diagnostic_item(cx, ty, sym::Vec);
if let Some(clone_spans) =
Expand Down Expand Up @@ -247,7 +241,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
}

// cannot be destructured, no need for `*` suggestion
assert!(deref_span.is_none());
return;
}
}
Expand Down Expand Up @@ -275,23 +268,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
);
}

assert!(deref_span.is_none());
return;
}
}

let mut spans = vec![(input.span, format!("&{}", snippet(cx, input.span, "_")))];

// Suggests adding `*` to dereference the added reference.
if let Some(deref_span) = deref_span {
spans.extend(
deref_span
.iter()
.copied()
.map(|span| (span, format!("*{}", snippet(cx, span, "<expr>")))),
);
spans.sort_by_key(|&(span, _)| span);
}
let spans = vec![(input.span, format!("&{}", snippet(cx, input.span, "_")))];

multispan_sugg(diag, "consider taking a reference instead", spans);
};

Expand Down Expand Up @@ -320,9 +302,6 @@ fn requires_exact_signature(attrs: &[Attribute]) -> bool {
#[derive(Default)]
struct MovedVariablesCtxt {
moved_vars: HirIdSet,
/// Spans which need to be prefixed with `*` for dereferencing the
/// suggested additional reference.
spans_need_deref: HirIdMap<FxHashSet<Span>>,
}

impl MovedVariablesCtxt {
Expand Down