Rollup merge of #4509 - sinkuu:redundant_clone_fix, r=llogiq
Fix false-positive of redundant_clone and move to clippy::perf This PR introduces dataflow analysis to `redundant_clone` lint to filter out borrowed variables, which had been incorrectly detected. Depends on https://github.com/rust-lang/rust/pull/64207. changelog: Moved `redundant_clone` lint to `perf` group # What this lint catches ## `clone`/`to_owned` ```rust let s = String::new(); let t = s.clone(); ``` ```rust // MIR _1 = String::new(); _2 = &_1; _3 = clone(_2); // (*) ``` We can turn this `clone` call into a move if 1. `_2` is the sole borrow of `_1` at the statement `(*)` 2. `_1` is not used hereafter ## `Deref` + type-specific `to_owned` method ```rust let s = std::path::PathBuf::new(); let t = s.to_path_buf(); ``` ```rust // MIR _1 = PathBuf::new(); _2 = &1; _3 = call deref(_2); _4 = _3; // Copies borrow StorageDead(_2); _5 = Path::to_path_buf(_4); // (*) ``` We can turn this `to_path_buf` call into a move if 1. `_3` `_4` are the sole borrow of `_1` at `(*)` 2. `_1` is not used hereafter # What this PR introduces 1. `MaybeStorageLive` that determines whether a local lives at a particular location 2. `PossibleBorrowerVisitor` that constructs [`TransitiveRelation`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/transitive_relation/struct.TransitiveRelation.html) of possible borrows, e.g. visiting `_2 = &1; _3 = &_2:` will result in `_3 -> _2 -> _1` relation. Then `_3` and `_2` will be counted as possible borrowers of `_1` in the sole-borrow analysis above.
This commit is contained in:
commit
8d2912ec00
21 changed files with 722 additions and 210 deletions
|
@ -100,10 +100,7 @@ pub fn run(check: bool, verbose: bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
|
fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
|
||||||
let arg_display: Vec<_> = args
|
let arg_display: Vec<_> = args.iter().map(|a| escape(a.as_ref().to_string_lossy())).collect();
|
||||||
.iter()
|
|
||||||
.map(|a| escape(a.as_ref().to_string_lossy()).to_owned())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
"cd {} && {} {}",
|
"cd {} && {} {}",
|
||||||
|
|
|
@ -343,7 +343,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
|
||||||
|
|
||||||
let stats = terminal_stats(&expr);
|
let stats = terminal_stats(&expr);
|
||||||
let mut simplified = expr.simplify();
|
let mut simplified = expr.simplify();
|
||||||
for simple in Bool::Not(Box::new(expr.clone())).simplify() {
|
for simple in Bool::Not(Box::new(expr)).simplify() {
|
||||||
match simple {
|
match simple {
|
||||||
Bool::Not(_) | Bool::True | Bool::False => {},
|
Bool::Not(_) | Bool::True | Bool::False => {},
|
||||||
_ => simplified.push(Bool::Not(Box::new(simple.clone()))),
|
_ => simplified.push(Bool::Not(Box::new(simple.clone()))),
|
||||||
|
|
|
@ -27,6 +27,8 @@ extern crate rustc_driver;
|
||||||
#[allow(unused_extern_crates)]
|
#[allow(unused_extern_crates)]
|
||||||
extern crate rustc_errors;
|
extern crate rustc_errors;
|
||||||
#[allow(unused_extern_crates)]
|
#[allow(unused_extern_crates)]
|
||||||
|
extern crate rustc_index;
|
||||||
|
#[allow(unused_extern_crates)]
|
||||||
extern crate rustc_mir;
|
extern crate rustc_mir;
|
||||||
#[allow(unused_extern_crates)]
|
#[allow(unused_extern_crates)]
|
||||||
extern crate rustc_target;
|
extern crate rustc_target;
|
||||||
|
@ -864,6 +866,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
|
||||||
ranges::RANGE_MINUS_ONE,
|
ranges::RANGE_MINUS_ONE,
|
||||||
ranges::RANGE_PLUS_ONE,
|
ranges::RANGE_PLUS_ONE,
|
||||||
ranges::RANGE_ZIP_WITH_LEN,
|
ranges::RANGE_ZIP_WITH_LEN,
|
||||||
|
redundant_clone::REDUNDANT_CLONE,
|
||||||
redundant_field_names::REDUNDANT_FIELD_NAMES,
|
redundant_field_names::REDUNDANT_FIELD_NAMES,
|
||||||
redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING,
|
redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING,
|
||||||
redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
|
redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
|
||||||
|
@ -1169,6 +1172,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
|
||||||
methods::SINGLE_CHAR_PATTERN,
|
methods::SINGLE_CHAR_PATTERN,
|
||||||
misc::CMP_OWNED,
|
misc::CMP_OWNED,
|
||||||
mutex_atomic::MUTEX_ATOMIC,
|
mutex_atomic::MUTEX_ATOMIC,
|
||||||
|
redundant_clone::REDUNDANT_CLONE,
|
||||||
slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
|
slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
|
||||||
trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF,
|
trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF,
|
||||||
types::BOX_VEC,
|
types::BOX_VEC,
|
||||||
|
@ -1188,7 +1192,6 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
|
||||||
mutex_atomic::MUTEX_INTEGER,
|
mutex_atomic::MUTEX_INTEGER,
|
||||||
needless_borrow::NEEDLESS_BORROW,
|
needless_borrow::NEEDLESS_BORROW,
|
||||||
path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE,
|
path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE,
|
||||||
redundant_clone::REDUNDANT_CLONE,
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,16 @@ use rustc::hir::{def_id, Body, FnDecl, HirId};
|
||||||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
||||||
use rustc::mir::{
|
use rustc::mir::{
|
||||||
self, traversal,
|
self, traversal,
|
||||||
visit::{MutatingUseContext, PlaceContext, Visitor},
|
visit::{MutatingUseContext, PlaceContext, Visitor as _},
|
||||||
TerminatorKind,
|
|
||||||
};
|
};
|
||||||
use rustc::ty::{self, Ty};
|
use rustc::ty::{self, fold::TypeVisitor, Ty};
|
||||||
use rustc::{declare_lint_pass, declare_tool_lint};
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
||||||
|
use rustc_data_structures::{fx::FxHashMap, transitive_relation::TransitiveRelation};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
|
use rustc_index::bit_set::{BitSet, HybridBitSet};
|
||||||
|
use rustc_mir::dataflow::{
|
||||||
|
do_dataflow, BitDenotation, BottomValue, DataflowResults, DataflowResultsCursor, DebugFormatted, GenKillSet,
|
||||||
|
};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use syntax::source_map::{BytePos, Span};
|
use syntax::source_map::{BytePos, Span};
|
||||||
|
|
||||||
|
@ -36,17 +40,7 @@ declare_clippy_lint! {
|
||||||
///
|
///
|
||||||
/// **Known problems:**
|
/// **Known problems:**
|
||||||
///
|
///
|
||||||
/// * Suggestions made by this lint could require NLL to be enabled.
|
/// False-negatives: analysis performed by this lint is conservative and limited.
|
||||||
/// * False-positive if there is a borrow preventing the value from moving out.
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// # fn foo(x: String) {}
|
|
||||||
/// let x = String::new();
|
|
||||||
///
|
|
||||||
/// let y = &x;
|
|
||||||
///
|
|
||||||
/// foo(x.clone()); // This lint suggests to remove this `clone()`
|
|
||||||
/// ```
|
|
||||||
///
|
///
|
||||||
/// **Example:**
|
/// **Example:**
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
@ -68,7 +62,7 @@ declare_clippy_lint! {
|
||||||
/// Path::new("/a/b").join("c").to_path_buf();
|
/// Path::new("/a/b").join("c").to_path_buf();
|
||||||
/// ```
|
/// ```
|
||||||
pub REDUNDANT_CLONE,
|
pub REDUNDANT_CLONE,
|
||||||
nursery,
|
perf,
|
||||||
"`clone()` of an owned value that is going to be dropped immediately"
|
"`clone()` of an owned value that is going to be dropped immediately"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,6 +82,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
|
||||||
let def_id = cx.tcx.hir().body_owner_def_id(body.id());
|
let def_id = cx.tcx.hir().body_owner_def_id(body.id());
|
||||||
let mir = cx.tcx.optimized_mir(def_id);
|
let mir = cx.tcx.optimized_mir(def_id);
|
||||||
|
|
||||||
|
let dead_unwinds = BitSet::new_empty(mir.basic_blocks().len());
|
||||||
|
let maybe_storage_live_result = do_dataflow(
|
||||||
|
cx.tcx,
|
||||||
|
mir,
|
||||||
|
def_id,
|
||||||
|
&[],
|
||||||
|
&dead_unwinds,
|
||||||
|
MaybeStorageLive::new(mir),
|
||||||
|
|bd, p| DebugFormatted::new(&bd.body.local_decls[p]),
|
||||||
|
);
|
||||||
|
let mut possible_borrower = {
|
||||||
|
let mut vis = PossibleBorrowerVisitor::new(cx, mir);
|
||||||
|
vis.visit_body(mir);
|
||||||
|
vis.into_map(cx, maybe_storage_live_result)
|
||||||
|
};
|
||||||
|
|
||||||
for (bb, bbdata) in mir.basic_blocks().iter_enumerated() {
|
for (bb, bbdata) in mir.basic_blocks().iter_enumerated() {
|
||||||
let terminator = bbdata.terminator();
|
let terminator = bbdata.terminator();
|
||||||
|
|
||||||
|
@ -114,29 +124,35 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// _1 in MIR `{ _2 = &_1; clone(move _2); }` or `{ _2 = _1; to_path_buf(_2); } (from_deref)
|
// `{ cloned = &arg; clone(move cloned); }` or `{ cloned = &arg; to_path_buf(cloned); }`
|
||||||
// In case of `from_deref`, `arg` is already a reference since it is `deref`ed in the previous
|
let (cloned, cannot_move_out) = unwrap_or_continue!(find_stmt_assigns_to(cx, mir, arg, from_borrow, bb));
|
||||||
// block.
|
|
||||||
let (cloned, cannot_move_out) = unwrap_or_continue!(find_stmt_assigns_to(
|
|
||||||
cx,
|
|
||||||
mir,
|
|
||||||
arg,
|
|
||||||
from_borrow,
|
|
||||||
bbdata.statements.iter()
|
|
||||||
));
|
|
||||||
|
|
||||||
if from_borrow && cannot_move_out {
|
let loc = mir::Location {
|
||||||
continue;
|
block: bb,
|
||||||
}
|
statement_index: bbdata.statements.len(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Cloned local
|
||||||
|
let local = if from_borrow {
|
||||||
|
// `res = clone(arg)` can be turned into `res = move arg;`
|
||||||
|
// if `arg` is the only borrow of `cloned` at this point.
|
||||||
|
|
||||||
|
if cannot_move_out || !possible_borrower.only_borrowers(&[arg], cloned, loc) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
cloned
|
||||||
|
} else {
|
||||||
|
// `arg` is a reference as it is `.deref()`ed in the previous block.
|
||||||
|
// Look into the predecessor block and find out the source of deref.
|
||||||
|
|
||||||
// _1 in MIR `{ _2 = &_1; _3 = deref(move _2); } -> { _4 = _3; to_path_buf(move _4); }`
|
|
||||||
let referent = if from_deref {
|
|
||||||
let ps = mir.predecessors_for(bb);
|
let ps = mir.predecessors_for(bb);
|
||||||
if ps.len() != 1 {
|
if ps.len() != 1 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let pred_terminator = mir[ps[0]].terminator();
|
let pred_terminator = mir[ps[0]].terminator();
|
||||||
|
|
||||||
|
// receiver of the `deref()` call
|
||||||
let pred_arg = if_chain! {
|
let pred_arg = if_chain! {
|
||||||
if let Some((pred_fn_def_id, pred_arg, pred_arg_ty, Some(res))) =
|
if let Some((pred_fn_def_id, pred_arg, pred_arg_ty, Some(res))) =
|
||||||
is_call_with_ref_arg(cx, mir, &pred_terminator.kind);
|
is_call_with_ref_arg(cx, mir, &pred_terminator.kind);
|
||||||
|
@ -151,21 +167,31 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let (local, cannot_move_out) = unwrap_or_continue!(find_stmt_assigns_to(
|
let (local, cannot_move_out) =
|
||||||
cx,
|
unwrap_or_continue!(find_stmt_assigns_to(cx, mir, pred_arg, true, ps[0]));
|
||||||
mir,
|
let loc = mir::Location {
|
||||||
pred_arg,
|
block: bb,
|
||||||
true,
|
statement_index: mir.basic_blocks()[bb].statements.len(),
|
||||||
mir[ps[0]].statements.iter()
|
};
|
||||||
));
|
|
||||||
if cannot_move_out {
|
// This can be turned into `res = move local` if `arg` and `cloned` are not borrowed
|
||||||
|
// at the last statement:
|
||||||
|
//
|
||||||
|
// ```
|
||||||
|
// pred_arg = &local;
|
||||||
|
// cloned = deref(pred_arg);
|
||||||
|
// arg = &cloned;
|
||||||
|
// StorageDead(pred_arg);
|
||||||
|
// res = to_path_buf(cloned);
|
||||||
|
// ```
|
||||||
|
if cannot_move_out || !possible_borrower.only_borrowers(&[arg, cloned], local, loc) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
local
|
local
|
||||||
} else {
|
|
||||||
cloned
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// `local` cannot be moved out if it is used later
|
||||||
let used_later = traversal::ReversePostorder::new(&mir, bb).skip(1).any(|(tbb, tdata)| {
|
let used_later = traversal::ReversePostorder::new(&mir, bb).skip(1).any(|(tbb, tdata)| {
|
||||||
// Give up on loops
|
// Give up on loops
|
||||||
if tdata.terminator().successors().any(|s| *s == bb) {
|
if tdata.terminator().successors().any(|s| *s == bb) {
|
||||||
|
@ -173,7 +199,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut vis = LocalUseVisitor {
|
let mut vis = LocalUseVisitor {
|
||||||
local: referent,
|
local,
|
||||||
used_other_than_drop: false,
|
used_other_than_drop: false,
|
||||||
};
|
};
|
||||||
vis.visit_basic_block_data(tbb, tdata);
|
vis.visit_basic_block_data(tbb, tdata);
|
||||||
|
@ -195,13 +221,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
|
||||||
let sugg_span = span.with_lo(
|
let sugg_span = span.with_lo(
|
||||||
span.lo() + BytePos(u32::try_from(dot).unwrap())
|
span.lo() + BytePos(u32::try_from(dot).unwrap())
|
||||||
);
|
);
|
||||||
|
let mut app = Applicability::MaybeIncorrect;
|
||||||
|
|
||||||
|
let mut call_snip = &snip[dot + 1..];
|
||||||
|
// Machine applicable when `call_snip` looks like `foobar()`
|
||||||
|
if call_snip.ends_with("()") {
|
||||||
|
call_snip = call_snip[..call_snip.len()-2].trim();
|
||||||
|
if call_snip.as_bytes().iter().all(|b| b.is_ascii_alphabetic() || *b == b'_') {
|
||||||
|
app = Applicability::MachineApplicable;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
span_lint_hir_and_then(cx, REDUNDANT_CLONE, node, sugg_span, "redundant clone", |db| {
|
span_lint_hir_and_then(cx, REDUNDANT_CLONE, node, sugg_span, "redundant clone", |db| {
|
||||||
db.span_suggestion(
|
db.span_suggestion(
|
||||||
sugg_span,
|
sugg_span,
|
||||||
"remove this",
|
"remove this",
|
||||||
String::new(),
|
String::new(),
|
||||||
Applicability::MaybeIncorrect,
|
app,
|
||||||
);
|
);
|
||||||
db.span_note(
|
db.span_note(
|
||||||
span.with_hi(span.lo() + BytePos(u32::try_from(dot).unwrap())),
|
span.with_hi(span.lo() + BytePos(u32::try_from(dot).unwrap())),
|
||||||
|
@ -224,7 +260,7 @@ fn is_call_with_ref_arg<'tcx>(
|
||||||
kind: &'tcx mir::TerminatorKind<'tcx>,
|
kind: &'tcx mir::TerminatorKind<'tcx>,
|
||||||
) -> Option<(def_id::DefId, mir::Local, Ty<'tcx>, Option<&'tcx mir::Place<'tcx>>)> {
|
) -> Option<(def_id::DefId, mir::Local, Ty<'tcx>, Option<&'tcx mir::Place<'tcx>>)> {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let TerminatorKind::Call { func, args, destination, .. } = kind;
|
if let mir::TerminatorKind::Call { func, args, destination, .. } = kind;
|
||||||
if args.len() == 1;
|
if args.len() == 1;
|
||||||
if let mir::Operand::Move(mir::Place { base: mir::PlaceBase::Local(local), .. }) = &args[0];
|
if let mir::Operand::Move(mir::Place { base: mir::PlaceBase::Local(local), .. }) = &args[0];
|
||||||
if let ty::FnDef(def_id, _) = func.ty(&*mir, cx.tcx).kind;
|
if let ty::FnDef(def_id, _) = func.ty(&*mir, cx.tcx).kind;
|
||||||
|
@ -241,42 +277,35 @@ fn is_call_with_ref_arg<'tcx>(
|
||||||
type CannotMoveOut = bool;
|
type CannotMoveOut = bool;
|
||||||
|
|
||||||
/// Finds the first `to = (&)from`, and returns
|
/// Finds the first `to = (&)from`, and returns
|
||||||
/// ``Some((from, [`true` if `from` cannot be moved out]))``.
|
/// ``Some((from, whether `from` cannot be moved out))``.
|
||||||
fn find_stmt_assigns_to<'a, 'tcx: 'a>(
|
fn find_stmt_assigns_to<'tcx>(
|
||||||
cx: &LateContext<'_, 'tcx>,
|
cx: &LateContext<'_, 'tcx>,
|
||||||
mir: &mir::Body<'tcx>,
|
mir: &mir::Body<'tcx>,
|
||||||
to: mir::Local,
|
to_local: mir::Local,
|
||||||
by_ref: bool,
|
by_ref: bool,
|
||||||
stmts: impl DoubleEndedIterator<Item = &'a mir::Statement<'tcx>>,
|
bb: mir::BasicBlock,
|
||||||
) -> Option<(mir::Local, CannotMoveOut)> {
|
) -> Option<(mir::Local, CannotMoveOut)> {
|
||||||
stmts
|
let rvalue = mir.basic_blocks()[bb].statements.iter().rev().find_map(|stmt| {
|
||||||
.rev()
|
if let mir::StatementKind::Assign(box (
|
||||||
.find_map(|stmt| {
|
mir::Place {
|
||||||
if let mir::StatementKind::Assign(box (
|
base: mir::PlaceBase::Local(local),
|
||||||
mir::Place {
|
..
|
||||||
base: mir::PlaceBase::Local(local),
|
},
|
||||||
..
|
v,
|
||||||
},
|
)) = &stmt.kind
|
||||||
v,
|
{
|
||||||
)) = &stmt.kind
|
return if *local == to_local { Some(v) } else { None };
|
||||||
{
|
}
|
||||||
if *local == to {
|
|
||||||
return Some(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
None
|
||||||
})
|
})?;
|
||||||
.and_then(|v| {
|
|
||||||
if by_ref {
|
match (by_ref, &*rvalue) {
|
||||||
if let mir::Rvalue::Ref(_, _, ref place) = v {
|
(true, mir::Rvalue::Ref(_, _, place)) | (false, mir::Rvalue::Use(mir::Operand::Copy(place))) => {
|
||||||
return base_local_and_movability(cx, mir, place);
|
base_local_and_movability(cx, mir, place)
|
||||||
}
|
},
|
||||||
} else if let mir::Rvalue::Use(mir::Operand::Copy(ref place)) = v {
|
_ => None,
|
||||||
return base_local_and_movability(cx, mir, place);
|
}
|
||||||
}
|
|
||||||
None
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts and returns the undermost base `Local` of given `place`. Returns `place` itself
|
/// Extracts and returns the undermost base `Local` of given `place`. Returns `place` itself
|
||||||
|
@ -288,8 +317,6 @@ fn base_local_and_movability<'tcx>(
|
||||||
mir: &mir::Body<'tcx>,
|
mir: &mir::Body<'tcx>,
|
||||||
place: &mir::Place<'tcx>,
|
place: &mir::Place<'tcx>,
|
||||||
) -> Option<(mir::Local, CannotMoveOut)> {
|
) -> Option<(mir::Local, CannotMoveOut)> {
|
||||||
use rustc::mir::Place;
|
|
||||||
use rustc::mir::PlaceBase;
|
|
||||||
use rustc::mir::PlaceRef;
|
use rustc::mir::PlaceRef;
|
||||||
|
|
||||||
// Dereference. You cannot move things out from a borrowed value.
|
// Dereference. You cannot move things out from a borrowed value.
|
||||||
|
@ -301,13 +328,15 @@ fn base_local_and_movability<'tcx>(
|
||||||
base: place_base,
|
base: place_base,
|
||||||
mut projection,
|
mut projection,
|
||||||
} = place.as_ref();
|
} = place.as_ref();
|
||||||
if let PlaceBase::Local(local) = place_base {
|
if let mir::PlaceBase::Local(local) = place_base {
|
||||||
while let [base @ .., elem] = projection {
|
while let [base @ .., elem] = projection {
|
||||||
projection = base;
|
projection = base;
|
||||||
deref = matches!(elem, mir::ProjectionElem::Deref);
|
deref |= matches!(elem, mir::ProjectionElem::Deref);
|
||||||
field = !field
|
field |= matches!(elem, mir::ProjectionElem::Field(..))
|
||||||
&& matches!(elem, mir::ProjectionElem::Field(..))
|
&& has_drop(
|
||||||
&& has_drop(cx, Place::ty_from(place_base, projection, &mir.local_decls, cx.tcx).ty);
|
cx,
|
||||||
|
mir::Place::ty_from(place_base, projection, &mir.local_decls, cx.tcx).ty,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Some((*local, deref || field))
|
Some((*local, deref || field))
|
||||||
|
@ -353,3 +382,233 @@ impl<'tcx> mir::visit::Visitor<'tcx> for LocalUseVisitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Determines liveness of each local purely based on `StorageLive`/`Dead`.
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
struct MaybeStorageLive<'a, 'tcx> {
|
||||||
|
body: &'a mir::Body<'tcx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'tcx> MaybeStorageLive<'a, 'tcx> {
|
||||||
|
fn new(body: &'a mir::Body<'tcx>) -> Self {
|
||||||
|
MaybeStorageLive { body }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'tcx> BitDenotation<'tcx> for MaybeStorageLive<'a, 'tcx> {
|
||||||
|
type Idx = mir::Local;
|
||||||
|
fn name() -> &'static str {
|
||||||
|
"maybe_storage_live"
|
||||||
|
}
|
||||||
|
fn bits_per_block(&self) -> usize {
|
||||||
|
self.body.local_decls.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_block_effect(&self, on_entry: &mut BitSet<mir::Local>) {
|
||||||
|
for arg in self.body.args_iter() {
|
||||||
|
on_entry.insert(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn statement_effect(&self, trans: &mut GenKillSet<mir::Local>, loc: mir::Location) {
|
||||||
|
let stmt = &self.body[loc.block].statements[loc.statement_index];
|
||||||
|
|
||||||
|
match stmt.kind {
|
||||||
|
mir::StatementKind::StorageLive(l) => trans.gen(l),
|
||||||
|
mir::StatementKind::StorageDead(l) => trans.kill(l),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn terminator_effect(&self, _trans: &mut GenKillSet<mir::Local>, _loc: mir::Location) {}
|
||||||
|
|
||||||
|
fn propagate_call_return(
|
||||||
|
&self,
|
||||||
|
_in_out: &mut BitSet<mir::Local>,
|
||||||
|
_call_bb: mir::BasicBlock,
|
||||||
|
_dest_bb: mir::BasicBlock,
|
||||||
|
_dest_place: &mir::Place<'tcx>,
|
||||||
|
) {
|
||||||
|
// Nothing to do when a call returns successfully
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'tcx> BottomValue for MaybeStorageLive<'a, 'tcx> {
|
||||||
|
/// bottom = dead
|
||||||
|
const BOTTOM_VALUE: bool = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Collects the possible borrowers of each local.
|
||||||
|
/// For example, `b = &a; c = &a;` will make `b` and (transitively) `c`
|
||||||
|
/// possible borrowers of `a`.
|
||||||
|
struct PossibleBorrowerVisitor<'a, 'tcx> {
|
||||||
|
possible_borrower: TransitiveRelation<mir::Local>,
|
||||||
|
body: &'a mir::Body<'tcx>,
|
||||||
|
cx: &'a LateContext<'a, 'tcx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'tcx> PossibleBorrowerVisitor<'a, 'tcx> {
|
||||||
|
fn new(cx: &'a LateContext<'a, 'tcx>, body: &'a mir::Body<'tcx>) -> Self {
|
||||||
|
Self {
|
||||||
|
possible_borrower: TransitiveRelation::default(),
|
||||||
|
cx,
|
||||||
|
body,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_map(
|
||||||
|
self,
|
||||||
|
cx: &LateContext<'a, 'tcx>,
|
||||||
|
maybe_live: DataflowResults<'tcx, MaybeStorageLive<'a, 'tcx>>,
|
||||||
|
) -> PossibleBorrower<'a, 'tcx> {
|
||||||
|
let mut map = FxHashMap::default();
|
||||||
|
for row in (1..self.body.local_decls.len()).map(mir::Local::from_usize) {
|
||||||
|
if is_copy(cx, self.body.local_decls[row].ty) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let borrowers = self.possible_borrower.reachable_from(&row);
|
||||||
|
if !borrowers.is_empty() {
|
||||||
|
let mut bs = HybridBitSet::new_empty(self.body.local_decls.len());
|
||||||
|
for &c in borrowers {
|
||||||
|
if c != mir::Local::from_usize(0) {
|
||||||
|
bs.insert(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bs.is_empty() {
|
||||||
|
map.insert(row, bs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let bs = BitSet::new_empty(self.body.local_decls.len());
|
||||||
|
PossibleBorrower {
|
||||||
|
map,
|
||||||
|
maybe_live: DataflowResultsCursor::new(maybe_live, self.body),
|
||||||
|
bitset: (bs.clone(), bs),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'tcx> mir::visit::Visitor<'tcx> for PossibleBorrowerVisitor<'a, 'tcx> {
|
||||||
|
fn visit_assign(&mut self, place: &mir::Place<'tcx>, rvalue: &mir::Rvalue<'_>, _location: mir::Location) {
|
||||||
|
if let mir::PlaceBase::Local(lhs) = place.base {
|
||||||
|
match rvalue {
|
||||||
|
mir::Rvalue::Ref(_, _, borrowed) => {
|
||||||
|
if let mir::PlaceBase::Local(borrowed_local) = borrowed.base {
|
||||||
|
self.possible_borrower.add(borrowed_local, lhs);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
other => {
|
||||||
|
if !ContainsRegion.visit_ty(place.ty(&self.body.local_decls, self.cx.tcx).ty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
rvalue_locals(other, |rhs| {
|
||||||
|
if lhs != rhs {
|
||||||
|
self.possible_borrower.add(rhs, lhs);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_terminator(&mut self, terminator: &mir::Terminator<'_>, _loc: mir::Location) {
|
||||||
|
if let mir::TerminatorKind::Call {
|
||||||
|
args,
|
||||||
|
destination:
|
||||||
|
Some((
|
||||||
|
mir::Place {
|
||||||
|
base: mir::PlaceBase::Local(dest),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
_,
|
||||||
|
)),
|
||||||
|
..
|
||||||
|
} = &terminator.kind
|
||||||
|
{
|
||||||
|
// If the call returns something with lifetimes,
|
||||||
|
// let's conservatively assume the returned value contains lifetime of all the arguments.
|
||||||
|
// For example, given `let y: Foo<'a> = foo(x)`, `y` is considered to be a possible borrower of `x`.
|
||||||
|
if !ContainsRegion.visit_ty(&self.body.local_decls[*dest].ty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for op in args {
|
||||||
|
match op {
|
||||||
|
mir::Operand::Copy(p) | mir::Operand::Move(p) => {
|
||||||
|
if let mir::PlaceBase::Local(arg) = p.base {
|
||||||
|
self.possible_borrower.add(arg, *dest);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ContainsRegion;
|
||||||
|
|
||||||
|
impl TypeVisitor<'_> for ContainsRegion {
|
||||||
|
fn visit_region(&mut self, _: ty::Region<'_>) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rvalue_locals(rvalue: &mir::Rvalue<'_>, mut visit: impl FnMut(mir::Local)) {
|
||||||
|
use rustc::mir::Rvalue::*;
|
||||||
|
|
||||||
|
let mut visit_op = |op: &mir::Operand<'_>| match op {
|
||||||
|
mir::Operand::Copy(p) | mir::Operand::Move(p) => {
|
||||||
|
if let mir::PlaceBase::Local(l) = p.base {
|
||||||
|
visit(l)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
|
||||||
|
match rvalue {
|
||||||
|
Use(op) | Repeat(op, _) | Cast(_, op, _) | UnaryOp(_, op) => visit_op(op),
|
||||||
|
Aggregate(_, ops) => ops.iter().for_each(visit_op),
|
||||||
|
BinaryOp(_, lhs, rhs) | CheckedBinaryOp(_, lhs, rhs) => {
|
||||||
|
visit_op(lhs);
|
||||||
|
visit_op(rhs);
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Result of `PossibleBorrowerVisitor`.
|
||||||
|
struct PossibleBorrower<'a, 'tcx> {
|
||||||
|
/// Mapping `Local -> its possible borrowers`
|
||||||
|
map: FxHashMap<mir::Local, HybridBitSet<mir::Local>>,
|
||||||
|
maybe_live: DataflowResultsCursor<'a, 'tcx, MaybeStorageLive<'a, 'tcx>>,
|
||||||
|
// Caches to avoid allocation of `BitSet` on every query
|
||||||
|
bitset: (BitSet<mir::Local>, BitSet<mir::Local>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PossibleBorrower<'_, '_> {
|
||||||
|
/// Returns true if the set of borrowers of `borrowed` living at `at` matches with `borrowers`.
|
||||||
|
fn only_borrowers(&mut self, borrowers: &[mir::Local], borrowed: mir::Local, at: mir::Location) -> bool {
|
||||||
|
self.maybe_live.seek(at);
|
||||||
|
|
||||||
|
self.bitset.0.clear();
|
||||||
|
let maybe_live = &mut self.maybe_live;
|
||||||
|
if let Some(bitset) = self.map.get(&borrowed) {
|
||||||
|
for b in bitset.iter().filter(move |b| maybe_live.contains(*b)) {
|
||||||
|
self.bitset.0.insert(b);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.bitset.1.clear();
|
||||||
|
for b in borrowers {
|
||||||
|
self.bitset.1.insert(*b);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.bitset.0 == self.bitset.1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1542,7 +1542,7 @@ pub const ALL_LINTS: [Lint; 318] = [
|
||||||
},
|
},
|
||||||
Lint {
|
Lint {
|
||||||
name: "redundant_clone",
|
name: "redundant_clone",
|
||||||
group: "nursery",
|
group: "perf",
|
||||||
desc: "`clone()` of an owned value that is going to be dropped immediately",
|
desc: "`clone()` of an owned value that is going to be dropped immediately",
|
||||||
deprecation: None,
|
deprecation: None,
|
||||||
module: "redundant_clone",
|
module: "redundant_clone",
|
||||||
|
|
|
@ -38,8 +38,7 @@ fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
|
||||||
|
|
||||||
let cfg_mode = mode.parse().expect("Invalid mode");
|
let cfg_mode = mode.parse().expect("Invalid mode");
|
||||||
if let Ok(name) = var::<&str>("TESTNAME") {
|
if let Ok(name) = var::<&str>("TESTNAME") {
|
||||||
let s: String = name.to_owned();
|
config.filter = Some(name)
|
||||||
config.filter = Some(s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if rustc_test_suite().is_some() {
|
if rustc_test_suite().is_some() {
|
||||||
|
|
|
@ -30,7 +30,7 @@ pub fn macro_test(input_stream: TokenStream) -> TokenStream {
|
||||||
TokenTree::Ident(Ident::new("true", Span::call_site())),
|
TokenTree::Ident(Ident::new("true", Span::call_site())),
|
||||||
TokenTree::Group(clause.clone()),
|
TokenTree::Group(clause.clone()),
|
||||||
TokenTree::Ident(Ident::new("else", Span::call_site())),
|
TokenTree::Ident(Ident::new("else", Span::call_site())),
|
||||||
TokenTree::Group(clause.clone()),
|
TokenTree::Group(clause),
|
||||||
])
|
])
|
||||||
})),
|
})),
|
||||||
])
|
])
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
#![feature(box_syntax)]
|
#![feature(box_syntax)]
|
||||||
#![allow(clippy::borrowed_box, clippy::needless_pass_by_value, clippy::unused_unit)]
|
#![allow(
|
||||||
|
clippy::borrowed_box,
|
||||||
|
clippy::needless_pass_by_value,
|
||||||
|
clippy::unused_unit,
|
||||||
|
clippy::redundant_clone
|
||||||
|
)]
|
||||||
#![warn(clippy::boxed_local)]
|
#![warn(clippy::boxed_local)]
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error: local variable doesn't need to be boxed here
|
error: local variable doesn't need to be boxed here
|
||||||
--> $DIR/escape_analysis.rs:34:13
|
--> $DIR/escape_analysis.rs:39:13
|
||||||
|
|
|
|
||||||
LL | fn warn_arg(x: Box<A>) {
|
LL | fn warn_arg(x: Box<A>) {
|
||||||
| ^
|
| ^
|
||||||
|
@ -7,13 +7,13 @@ LL | fn warn_arg(x: Box<A>) {
|
||||||
= note: `-D clippy::boxed-local` implied by `-D warnings`
|
= note: `-D clippy::boxed-local` implied by `-D warnings`
|
||||||
|
|
||||||
error: local variable doesn't need to be boxed here
|
error: local variable doesn't need to be boxed here
|
||||||
--> $DIR/escape_analysis.rs:125:12
|
--> $DIR/escape_analysis.rs:130:12
|
||||||
|
|
|
|
||||||
LL | pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {}
|
LL | pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {}
|
||||||
| ^^^^^^^^^^^
|
| ^^^^^^^^^^^
|
||||||
|
|
||||||
error: local variable doesn't need to be boxed here
|
error: local variable doesn't need to be boxed here
|
||||||
--> $DIR/escape_analysis.rs:165:23
|
--> $DIR/escape_analysis.rs:170:23
|
||||||
|
|
|
|
||||||
LL | fn closure_borrow(x: Box<A>) {
|
LL | fn closure_borrow(x: Box<A>) {
|
||||||
| ^
|
| ^
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// run-rustfix
|
// run-rustfix
|
||||||
#![warn(clippy::all, clippy::pedantic)]
|
#![warn(clippy::all, clippy::pedantic)]
|
||||||
#![allow(clippy::iter_cloned_collect)]
|
#![allow(clippy::iter_cloned_collect)]
|
||||||
#![allow(clippy::clone_on_copy)]
|
#![allow(clippy::clone_on_copy, clippy::redundant_clone)]
|
||||||
#![allow(clippy::missing_docs_in_private_items)]
|
#![allow(clippy::missing_docs_in_private_items)]
|
||||||
#![allow(clippy::redundant_closure_for_method_calls)]
|
#![allow(clippy::redundant_closure_for_method_calls)]
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// run-rustfix
|
// run-rustfix
|
||||||
#![warn(clippy::all, clippy::pedantic)]
|
#![warn(clippy::all, clippy::pedantic)]
|
||||||
#![allow(clippy::iter_cloned_collect)]
|
#![allow(clippy::iter_cloned_collect)]
|
||||||
#![allow(clippy::clone_on_copy)]
|
#![allow(clippy::clone_on_copy, clippy::redundant_clone)]
|
||||||
#![allow(clippy::missing_docs_in_private_items)]
|
#![allow(clippy::missing_docs_in_private_items)]
|
||||||
#![allow(clippy::redundant_closure_for_method_calls)]
|
#![allow(clippy::redundant_closure_for_method_calls)]
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
clippy::single_match,
|
clippy::single_match,
|
||||||
clippy::redundant_pattern_matching,
|
clippy::redundant_pattern_matching,
|
||||||
clippy::many_single_char_names,
|
clippy::many_single_char_names,
|
||||||
clippy::option_option
|
clippy::option_option,
|
||||||
|
clippy::redundant_clone
|
||||||
)]
|
)]
|
||||||
|
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:16:23
|
--> $DIR/needless_pass_by_value.rs:17:23
|
||||||
|
|
|
|
||||||
LL | fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
|
LL | fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T> {
|
||||||
| ^^^^^^ help: consider changing the type to: `&[T]`
|
| ^^^^^^ help: consider changing the type to: `&[T]`
|
||||||
|
@ -7,25 +7,25 @@ LL | fn foo<T: Default>(v: Vec<T>, w: Vec<T>, mut x: Vec<T>, y: Vec<T>) -> Vec<T
|
||||||
= note: `-D clippy::needless-pass-by-value` implied by `-D warnings`
|
= note: `-D clippy::needless-pass-by-value` implied by `-D warnings`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:30:11
|
--> $DIR/needless_pass_by_value.rs:31:11
|
||||||
|
|
|
|
||||||
LL | fn bar(x: String, y: Wrapper) {
|
LL | fn bar(x: String, y: Wrapper) {
|
||||||
| ^^^^^^ help: consider changing the type to: `&str`
|
| ^^^^^^ help: consider changing the type to: `&str`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:30:22
|
--> $DIR/needless_pass_by_value.rs:31:22
|
||||||
|
|
|
|
||||||
LL | fn bar(x: String, y: Wrapper) {
|
LL | fn bar(x: String, y: Wrapper) {
|
||||||
| ^^^^^^^ help: consider taking a reference instead: `&Wrapper`
|
| ^^^^^^^ help: consider taking a reference instead: `&Wrapper`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:36:71
|
--> $DIR/needless_pass_by_value.rs:37:71
|
||||||
|
|
|
|
||||||
LL | fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
|
LL | fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
|
||||||
| ^ help: consider taking a reference instead: `&V`
|
| ^ help: consider taking a reference instead: `&V`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:48:18
|
--> $DIR/needless_pass_by_value.rs:49:18
|
||||||
|
|
|
|
||||||
LL | fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
|
LL | fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -36,13 +36,13 @@ LL | match *x {
|
||||||
|
|
|
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:61:24
|
--> $DIR/needless_pass_by_value.rs:62:24
|
||||||
|
|
|
|
||||||
LL | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
|
LL | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
|
||||||
| ^^^^^^^ help: consider taking a reference instead: `&Wrapper`
|
| ^^^^^^^ help: consider taking a reference instead: `&Wrapper`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:61:36
|
--> $DIR/needless_pass_by_value.rs:62:36
|
||||||
|
|
|
|
||||||
LL | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
|
LL | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
|
||||||
| ^^^^^^^
|
| ^^^^^^^
|
||||||
|
@ -55,19 +55,19 @@ LL | let Wrapper(_) = *y; // still not moved
|
||||||
|
|
|
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:77:49
|
--> $DIR/needless_pass_by_value.rs:78:49
|
||||||
|
|
|
|
||||||
LL | fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
|
LL | fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
|
||||||
| ^ help: consider taking a reference instead: `&T`
|
| ^ help: consider taking a reference instead: `&T`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:79:18
|
--> $DIR/needless_pass_by_value.rs:80:18
|
||||||
|
|
|
|
||||||
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
|
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
|
||||||
| ^^^^^^ help: consider taking a reference instead: `&String`
|
| ^^^^^^ help: consider taking a reference instead: `&String`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:79:29
|
--> $DIR/needless_pass_by_value.rs:80:29
|
||||||
|
|
|
|
||||||
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
|
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
|
||||||
| ^^^^^^
|
| ^^^^^^
|
||||||
|
@ -81,13 +81,13 @@ LL | let _ = t.to_string();
|
||||||
| ^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:79:40
|
--> $DIR/needless_pass_by_value.rs:80:40
|
||||||
|
|
|
|
||||||
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
|
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
|
||||||
| ^^^^^^^^ help: consider taking a reference instead: `&Vec<i32>`
|
| ^^^^^^^^ help: consider taking a reference instead: `&Vec<i32>`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:79:53
|
--> $DIR/needless_pass_by_value.rs:80:53
|
||||||
|
|
|
|
||||||
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
|
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
|
||||||
| ^^^^^^^^
|
| ^^^^^^^^
|
||||||
|
@ -101,61 +101,61 @@ LL | let _ = v.to_owned();
|
||||||
| ^^^^^^^^^^^^
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:92:12
|
--> $DIR/needless_pass_by_value.rs:93:12
|
||||||
|
|
|
|
||||||
LL | s: String,
|
LL | s: String,
|
||||||
| ^^^^^^ help: consider changing the type to: `&str`
|
| ^^^^^^ help: consider changing the type to: `&str`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:93:12
|
--> $DIR/needless_pass_by_value.rs:94:12
|
||||||
|
|
|
|
||||||
LL | t: String,
|
LL | t: String,
|
||||||
| ^^^^^^ help: consider taking a reference instead: `&String`
|
| ^^^^^^ help: consider taking a reference instead: `&String`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:102:23
|
--> $DIR/needless_pass_by_value.rs:103:23
|
||||||
|
|
|
|
||||||
LL | fn baz(&self, _u: U, _s: Self) {}
|
LL | fn baz(&self, _u: U, _s: Self) {}
|
||||||
| ^ help: consider taking a reference instead: `&U`
|
| ^ help: consider taking a reference instead: `&U`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:102:30
|
--> $DIR/needless_pass_by_value.rs:103:30
|
||||||
|
|
|
|
||||||
LL | fn baz(&self, _u: U, _s: Self) {}
|
LL | fn baz(&self, _u: U, _s: Self) {}
|
||||||
| ^^^^ help: consider taking a reference instead: `&Self`
|
| ^^^^ help: consider taking a reference instead: `&Self`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:124:24
|
--> $DIR/needless_pass_by_value.rs:125:24
|
||||||
|
|
|
|
||||||
LL | fn bar_copy(x: u32, y: CopyWrapper) {
|
LL | fn bar_copy(x: u32, y: CopyWrapper) {
|
||||||
| ^^^^^^^^^^^ help: consider taking a reference instead: `&CopyWrapper`
|
| ^^^^^^^^^^^ help: consider taking a reference instead: `&CopyWrapper`
|
||||||
|
|
|
|
||||||
help: consider marking this type as Copy
|
help: consider marking this type as Copy
|
||||||
--> $DIR/needless_pass_by_value.rs:122:1
|
--> $DIR/needless_pass_by_value.rs:123:1
|
||||||
|
|
|
|
||||||
LL | struct CopyWrapper(u32);
|
LL | struct CopyWrapper(u32);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:130:29
|
--> $DIR/needless_pass_by_value.rs:131:29
|
||||||
|
|
|
|
||||||
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
|
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
|
||||||
| ^^^^^^^^^^^ help: consider taking a reference instead: `&CopyWrapper`
|
| ^^^^^^^^^^^ help: consider taking a reference instead: `&CopyWrapper`
|
||||||
|
|
|
|
||||||
help: consider marking this type as Copy
|
help: consider marking this type as Copy
|
||||||
--> $DIR/needless_pass_by_value.rs:122:1
|
--> $DIR/needless_pass_by_value.rs:123:1
|
||||||
|
|
|
|
||||||
LL | struct CopyWrapper(u32);
|
LL | struct CopyWrapper(u32);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:130:45
|
--> $DIR/needless_pass_by_value.rs:131:45
|
||||||
|
|
|
|
||||||
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
|
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
|
||||||
| ^^^^^^^^^^^
|
| ^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
help: consider marking this type as Copy
|
help: consider marking this type as Copy
|
||||||
--> $DIR/needless_pass_by_value.rs:122:1
|
--> $DIR/needless_pass_by_value.rs:123:1
|
||||||
|
|
|
|
||||||
LL | struct CopyWrapper(u32);
|
LL | struct CopyWrapper(u32);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -168,13 +168,13 @@ LL | let CopyWrapper(_) = *y; // still not moved
|
||||||
|
|
|
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:130:61
|
--> $DIR/needless_pass_by_value.rs:131:61
|
||||||
|
|
|
|
||||||
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
|
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
|
||||||
| ^^^^^^^^^^^
|
| ^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
help: consider marking this type as Copy
|
help: consider marking this type as Copy
|
||||||
--> $DIR/needless_pass_by_value.rs:122:1
|
--> $DIR/needless_pass_by_value.rs:123:1
|
||||||
|
|
|
|
||||||
LL | struct CopyWrapper(u32);
|
LL | struct CopyWrapper(u32);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -185,13 +185,13 @@ LL | let CopyWrapper(s) = *z; // moved
|
||||||
|
|
|
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:142:40
|
--> $DIR/needless_pass_by_value.rs:143:40
|
||||||
|
|
|
|
||||||
LL | fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {}
|
LL | fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {}
|
||||||
| ^ help: consider taking a reference instead: `&S`
|
| ^ help: consider taking a reference instead: `&S`
|
||||||
|
|
||||||
error: this argument is passed by value, but not consumed in the function body
|
error: this argument is passed by value, but not consumed in the function body
|
||||||
--> $DIR/needless_pass_by_value.rs:147:20
|
--> $DIR/needless_pass_by_value.rs:148:20
|
||||||
|
|
|
|
||||||
LL | fn more_fun(_item: impl Club<'static, i32>) {}
|
LL | fn more_fun(_item: impl Club<'static, i32>) {}
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>`
|
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>`
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#![allow(unused, clippy::many_single_char_names)]
|
#![allow(unused, clippy::many_single_char_names, clippy::redundant_clone)]
|
||||||
#![warn(clippy::ptr_arg)]
|
#![warn(clippy::ptr_arg)]
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
|
@ -104,7 +104,7 @@ fn main() {
|
||||||
};
|
};
|
||||||
move_struct.ref_func();
|
move_struct.ref_func();
|
||||||
move_struct.clone().mov_func_reuse();
|
move_struct.clone().mov_func_reuse();
|
||||||
move_struct.clone().mov_func_no_use();
|
move_struct.mov_func_no_use();
|
||||||
|
|
||||||
let so = SeemsOption::Some(45);
|
let so = SeemsOption::Some(45);
|
||||||
returns_something_similar_to_option(so);
|
returns_something_similar_to_option(so);
|
||||||
|
|
132
tests/ui/redundant_clone.fixed
Normal file
132
tests/ui/redundant_clone.fixed
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
// run-rustfix
|
||||||
|
// rustfix-only-machine-applicable
|
||||||
|
use std::ffi::OsString;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _s = ["lorem", "ipsum"].join(" ");
|
||||||
|
|
||||||
|
let s = String::from("foo");
|
||||||
|
let _s = s;
|
||||||
|
|
||||||
|
let s = String::from("foo");
|
||||||
|
let _s = s;
|
||||||
|
|
||||||
|
let s = String::from("foo");
|
||||||
|
let _s = s;
|
||||||
|
|
||||||
|
let _s = Path::new("/a/b/").join("c");
|
||||||
|
|
||||||
|
let _s = Path::new("/a/b/").join("c");
|
||||||
|
|
||||||
|
let _s = OsString::new();
|
||||||
|
|
||||||
|
let _s = OsString::new();
|
||||||
|
|
||||||
|
// Check that lint level works
|
||||||
|
#[allow(clippy::redundant_clone)]
|
||||||
|
let _s = String::new().to_string();
|
||||||
|
|
||||||
|
let tup = (String::from("foo"),);
|
||||||
|
let _t = tup.0;
|
||||||
|
|
||||||
|
let tup_ref = &(String::from("foo"),);
|
||||||
|
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
|
||||||
|
|
||||||
|
{
|
||||||
|
let x = String::new();
|
||||||
|
let y = &x;
|
||||||
|
|
||||||
|
let _x = x.clone(); // ok; `x` is borrowed by `y`
|
||||||
|
|
||||||
|
let _ = y.len();
|
||||||
|
}
|
||||||
|
|
||||||
|
let x = (String::new(),);
|
||||||
|
let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
|
||||||
|
|
||||||
|
with_branch(Alpha, true);
|
||||||
|
cannot_move_from_type_with_drop();
|
||||||
|
borrower_propagation();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Alpha;
|
||||||
|
fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
|
||||||
|
if b {
|
||||||
|
(a.clone(), a)
|
||||||
|
} else {
|
||||||
|
(Alpha, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TypeWithDrop {
|
||||||
|
x: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TypeWithDrop {
|
||||||
|
fn drop(&mut self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cannot_move_from_type_with_drop() -> String {
|
||||||
|
let s = TypeWithDrop { x: String::new() };
|
||||||
|
s.x.clone() // removing this `clone()` summons E0509
|
||||||
|
}
|
||||||
|
|
||||||
|
fn borrower_propagation() {
|
||||||
|
let s = String::new();
|
||||||
|
let t = String::new();
|
||||||
|
|
||||||
|
{
|
||||||
|
fn b() -> bool {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
let _u = if b() { &s } else { &t };
|
||||||
|
|
||||||
|
// ok; `s` and `t` are possibly borrowed
|
||||||
|
let _s = s.clone();
|
||||||
|
let _t = t.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let _u = || s.len();
|
||||||
|
let _v = [&t; 32];
|
||||||
|
let _s = s.clone(); // ok
|
||||||
|
let _t = t.clone(); // ok
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let _u = {
|
||||||
|
let u = Some(&s);
|
||||||
|
let _ = s.clone(); // ok
|
||||||
|
u
|
||||||
|
};
|
||||||
|
let _s = s.clone(); // ok
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
use std::convert::identity as id;
|
||||||
|
let _u = id(id(&s));
|
||||||
|
let _s = s.clone(); // ok, `u` borrows `s`
|
||||||
|
}
|
||||||
|
|
||||||
|
let _s = s;
|
||||||
|
let _t = t;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Foo {
|
||||||
|
x: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let f = Foo { x: 123 };
|
||||||
|
let _x = Some(f.x);
|
||||||
|
let _f = f;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let f = Foo { x: 123 };
|
||||||
|
let _x = &f.x;
|
||||||
|
let _f = f.clone(); // ok
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,37 +1,53 @@
|
||||||
#![warn(clippy::redundant_clone)]
|
// run-rustfix
|
||||||
|
// rustfix-only-machine-applicable
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = ["lorem", "ipsum"].join(" ").to_string();
|
let _s = ["lorem", "ipsum"].join(" ").to_string();
|
||||||
|
|
||||||
let s = String::from("foo");
|
let s = String::from("foo");
|
||||||
let _ = s.clone();
|
let _s = s.clone();
|
||||||
|
|
||||||
let s = String::from("foo");
|
let s = String::from("foo");
|
||||||
let _ = s.to_string();
|
let _s = s.to_string();
|
||||||
|
|
||||||
let s = String::from("foo");
|
let s = String::from("foo");
|
||||||
let _ = s.to_owned();
|
let _s = s.to_owned();
|
||||||
|
|
||||||
let _ = Path::new("/a/b/").join("c").to_owned();
|
let _s = Path::new("/a/b/").join("c").to_owned();
|
||||||
|
|
||||||
let _ = Path::new("/a/b/").join("c").to_path_buf();
|
let _s = Path::new("/a/b/").join("c").to_path_buf();
|
||||||
|
|
||||||
let _ = OsString::new().to_owned();
|
let _s = OsString::new().to_owned();
|
||||||
|
|
||||||
let _ = OsString::new().to_os_string();
|
let _s = OsString::new().to_os_string();
|
||||||
|
|
||||||
// Check that lint level works
|
// Check that lint level works
|
||||||
#[allow(clippy::redundant_clone)]
|
#[allow(clippy::redundant_clone)]
|
||||||
let _ = String::new().to_string();
|
let _s = String::new().to_string();
|
||||||
|
|
||||||
let tup = (String::from("foo"),);
|
let tup = (String::from("foo"),);
|
||||||
let _ = tup.0.clone();
|
let _t = tup.0.clone();
|
||||||
|
|
||||||
let tup_ref = &(String::from("foo"),);
|
let tup_ref = &(String::from("foo"),);
|
||||||
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
|
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
|
||||||
|
|
||||||
|
{
|
||||||
|
let x = String::new();
|
||||||
|
let y = &x;
|
||||||
|
|
||||||
|
let _x = x.clone(); // ok; `x` is borrowed by `y`
|
||||||
|
|
||||||
|
let _ = y.len();
|
||||||
|
}
|
||||||
|
|
||||||
|
let x = (String::new(),);
|
||||||
|
let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
|
||||||
|
|
||||||
|
with_branch(Alpha, true);
|
||||||
|
cannot_move_from_type_with_drop();
|
||||||
|
borrower_propagation();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -56,3 +72,61 @@ fn cannot_move_from_type_with_drop() -> String {
|
||||||
let s = TypeWithDrop { x: String::new() };
|
let s = TypeWithDrop { x: String::new() };
|
||||||
s.x.clone() // removing this `clone()` summons E0509
|
s.x.clone() // removing this `clone()` summons E0509
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn borrower_propagation() {
|
||||||
|
let s = String::new();
|
||||||
|
let t = String::new();
|
||||||
|
|
||||||
|
{
|
||||||
|
fn b() -> bool {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
let _u = if b() { &s } else { &t };
|
||||||
|
|
||||||
|
// ok; `s` and `t` are possibly borrowed
|
||||||
|
let _s = s.clone();
|
||||||
|
let _t = t.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let _u = || s.len();
|
||||||
|
let _v = [&t; 32];
|
||||||
|
let _s = s.clone(); // ok
|
||||||
|
let _t = t.clone(); // ok
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let _u = {
|
||||||
|
let u = Some(&s);
|
||||||
|
let _ = s.clone(); // ok
|
||||||
|
u
|
||||||
|
};
|
||||||
|
let _s = s.clone(); // ok
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
use std::convert::identity as id;
|
||||||
|
let _u = id(id(&s));
|
||||||
|
let _s = s.clone(); // ok, `u` borrows `s`
|
||||||
|
}
|
||||||
|
|
||||||
|
let _s = s.clone();
|
||||||
|
let _t = t.clone();
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Foo {
|
||||||
|
x: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let f = Foo { x: 123 };
|
||||||
|
let _x = Some(f.x);
|
||||||
|
let _f = f.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let f = Foo { x: 123 };
|
||||||
|
let _x = &f.x;
|
||||||
|
let _f = f.clone(); // ok
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,123 +1,159 @@
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
--> $DIR/redundant_clone.rs:7:41
|
--> $DIR/redundant_clone.rs:7:42
|
||||||
|
|
|
|
||||||
LL | let _ = ["lorem", "ipsum"].join(" ").to_string();
|
LL | let _s = ["lorem", "ipsum"].join(" ").to_string();
|
||||||
| ^^^^^^^^^^^^ help: remove this
|
| ^^^^^^^^^^^^ help: remove this
|
||||||
|
|
|
|
||||||
= note: `-D clippy::redundant-clone` implied by `-D warnings`
|
= note: `-D clippy::redundant-clone` implied by `-D warnings`
|
||||||
note: this value is dropped without further use
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:7:13
|
--> $DIR/redundant_clone.rs:7:14
|
||||||
|
|
|
|
||||||
LL | let _ = ["lorem", "ipsum"].join(" ").to_string();
|
LL | let _s = ["lorem", "ipsum"].join(" ").to_string();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
|
--> $DIR/redundant_clone.rs:10:15
|
||||||
|
|
|
||||||
|
LL | let _s = s.clone();
|
||||||
|
| ^^^^^^^^ help: remove this
|
||||||
|
|
|
||||||
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:10:14
|
--> $DIR/redundant_clone.rs:10:14
|
||||||
|
|
|
|
||||||
LL | let _ = s.clone();
|
LL | let _s = s.clone();
|
||||||
| ^^^^^^^^ help: remove this
|
| ^
|
||||||
|
|
|
||||||
note: this value is dropped without further use
|
|
||||||
--> $DIR/redundant_clone.rs:10:13
|
|
||||||
|
|
|
||||||
LL | let _ = s.clone();
|
|
||||||
| ^
|
|
||||||
|
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
|
--> $DIR/redundant_clone.rs:13:15
|
||||||
|
|
|
||||||
|
LL | let _s = s.to_string();
|
||||||
|
| ^^^^^^^^^^^^ help: remove this
|
||||||
|
|
|
||||||
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:13:14
|
--> $DIR/redundant_clone.rs:13:14
|
||||||
|
|
|
|
||||||
LL | let _ = s.to_string();
|
LL | let _s = s.to_string();
|
||||||
| ^^^^^^^^^^^^ help: remove this
|
| ^
|
||||||
|
|
|
||||||
note: this value is dropped without further use
|
|
||||||
--> $DIR/redundant_clone.rs:13:13
|
|
||||||
|
|
|
||||||
LL | let _ = s.to_string();
|
|
||||||
| ^
|
|
||||||
|
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
|
--> $DIR/redundant_clone.rs:16:15
|
||||||
|
|
|
||||||
|
LL | let _s = s.to_owned();
|
||||||
|
| ^^^^^^^^^^^ help: remove this
|
||||||
|
|
|
||||||
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:16:14
|
--> $DIR/redundant_clone.rs:16:14
|
||||||
|
|
|
|
||||||
LL | let _ = s.to_owned();
|
LL | let _s = s.to_owned();
|
||||||
| ^^^^^^^^^^^ help: remove this
|
| ^
|
||||||
|
|
|
||||||
note: this value is dropped without further use
|
|
||||||
--> $DIR/redundant_clone.rs:16:13
|
|
||||||
|
|
|
||||||
LL | let _ = s.to_owned();
|
|
||||||
| ^
|
|
||||||
|
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
--> $DIR/redundant_clone.rs:18:41
|
--> $DIR/redundant_clone.rs:18:42
|
||||||
|
|
|
|
||||||
LL | let _ = Path::new("/a/b/").join("c").to_owned();
|
LL | let _s = Path::new("/a/b/").join("c").to_owned();
|
||||||
| ^^^^^^^^^^^ help: remove this
|
| ^^^^^^^^^^^ help: remove this
|
||||||
|
|
|
|
||||||
note: this value is dropped without further use
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:18:13
|
--> $DIR/redundant_clone.rs:18:14
|
||||||
|
|
|
|
||||||
LL | let _ = Path::new("/a/b/").join("c").to_owned();
|
LL | let _s = Path::new("/a/b/").join("c").to_owned();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
--> $DIR/redundant_clone.rs:20:41
|
--> $DIR/redundant_clone.rs:20:42
|
||||||
|
|
|
|
||||||
LL | let _ = Path::new("/a/b/").join("c").to_path_buf();
|
LL | let _s = Path::new("/a/b/").join("c").to_path_buf();
|
||||||
| ^^^^^^^^^^^^^^ help: remove this
|
| ^^^^^^^^^^^^^^ help: remove this
|
||||||
|
|
|
|
||||||
note: this value is dropped without further use
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:20:13
|
--> $DIR/redundant_clone.rs:20:14
|
||||||
|
|
|
|
||||||
LL | let _ = Path::new("/a/b/").join("c").to_path_buf();
|
LL | let _s = Path::new("/a/b/").join("c").to_path_buf();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
--> $DIR/redundant_clone.rs:22:28
|
--> $DIR/redundant_clone.rs:22:29
|
||||||
|
|
|
|
||||||
LL | let _ = OsString::new().to_owned();
|
LL | let _s = OsString::new().to_owned();
|
||||||
| ^^^^^^^^^^^ help: remove this
|
| ^^^^^^^^^^^ help: remove this
|
||||||
|
|
|
|
||||||
note: this value is dropped without further use
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:22:13
|
--> $DIR/redundant_clone.rs:22:14
|
||||||
|
|
|
|
||||||
LL | let _ = OsString::new().to_owned();
|
LL | let _s = OsString::new().to_owned();
|
||||||
| ^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
--> $DIR/redundant_clone.rs:24:28
|
--> $DIR/redundant_clone.rs:24:29
|
||||||
|
|
|
|
||||||
LL | let _ = OsString::new().to_os_string();
|
LL | let _s = OsString::new().to_os_string();
|
||||||
| ^^^^^^^^^^^^^^^ help: remove this
|
| ^^^^^^^^^^^^^^^ help: remove this
|
||||||
|
|
|
|
||||||
note: this value is dropped without further use
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:24:13
|
--> $DIR/redundant_clone.rs:24:14
|
||||||
|
|
|
|
||||||
LL | let _ = OsString::new().to_os_string();
|
LL | let _s = OsString::new().to_os_string();
|
||||||
| ^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
--> $DIR/redundant_clone.rs:31:18
|
--> $DIR/redundant_clone.rs:31:19
|
||||||
|
|
|
|
||||||
LL | let _ = tup.0.clone();
|
LL | let _t = tup.0.clone();
|
||||||
| ^^^^^^^^ help: remove this
|
| ^^^^^^^^ help: remove this
|
||||||
|
|
|
|
||||||
note: this value is dropped without further use
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:31:13
|
--> $DIR/redundant_clone.rs:31:14
|
||||||
|
|
|
|
||||||
LL | let _ = tup.0.clone();
|
LL | let _t = tup.0.clone();
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
||||||
error: redundant clone
|
error: redundant clone
|
||||||
--> $DIR/redundant_clone.rs:41:22
|
--> $DIR/redundant_clone.rs:57:22
|
||||||
|
|
|
|
||||||
LL | (a.clone(), a.clone())
|
LL | (a.clone(), a.clone())
|
||||||
| ^^^^^^^^ help: remove this
|
| ^^^^^^^^ help: remove this
|
||||||
|
|
|
|
||||||
note: this value is dropped without further use
|
note: this value is dropped without further use
|
||||||
--> $DIR/redundant_clone.rs:41:21
|
--> $DIR/redundant_clone.rs:57:21
|
||||||
|
|
|
|
||||||
LL | (a.clone(), a.clone())
|
LL | (a.clone(), a.clone())
|
||||||
| ^
|
| ^
|
||||||
|
|
||||||
error: aborting due to 10 previous errors
|
error: redundant clone
|
||||||
|
--> $DIR/redundant_clone.rs:113:15
|
||||||
|
|
|
||||||
|
LL | let _s = s.clone();
|
||||||
|
| ^^^^^^^^ help: remove this
|
||||||
|
|
|
||||||
|
note: this value is dropped without further use
|
||||||
|
--> $DIR/redundant_clone.rs:113:14
|
||||||
|
|
|
||||||
|
LL | let _s = s.clone();
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: redundant clone
|
||||||
|
--> $DIR/redundant_clone.rs:114:15
|
||||||
|
|
|
||||||
|
LL | let _t = t.clone();
|
||||||
|
| ^^^^^^^^ help: remove this
|
||||||
|
|
|
||||||
|
note: this value is dropped without further use
|
||||||
|
--> $DIR/redundant_clone.rs:114:14
|
||||||
|
|
|
||||||
|
LL | let _t = t.clone();
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: redundant clone
|
||||||
|
--> $DIR/redundant_clone.rs:124:19
|
||||||
|
|
|
||||||
|
LL | let _f = f.clone();
|
||||||
|
| ^^^^^^^^ help: remove this
|
||||||
|
|
|
||||||
|
note: this value is dropped without further use
|
||||||
|
--> $DIR/redundant_clone.rs:124:18
|
||||||
|
|
|
||||||
|
LL | let _f = f.clone();
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: aborting due to 13 previous errors
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
#![warn(clippy::all)]
|
#![warn(clippy::all)]
|
||||||
#![allow(clippy::blacklisted_name, clippy::no_effect, redundant_semicolon, unused_assignments)]
|
#![allow(
|
||||||
|
clippy::blacklisted_name,
|
||||||
|
clippy::no_effect,
|
||||||
|
clippy::redundant_clone,
|
||||||
|
redundant_semicolon,
|
||||||
|
unused_assignments
|
||||||
|
)]
|
||||||
|
|
||||||
struct Foo(u32);
|
struct Foo(u32);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error: this looks like you are swapping elements of `foo` manually
|
error: this looks like you are swapping elements of `foo` manually
|
||||||
--> $DIR/swap.rs:27:5
|
--> $DIR/swap.rs:33:5
|
||||||
|
|
|
|
||||||
LL | / let temp = foo[0];
|
LL | / let temp = foo[0];
|
||||||
LL | | foo[0] = foo[1];
|
LL | | foo[0] = foo[1];
|
||||||
|
@ -9,7 +9,7 @@ LL | | foo[1] = temp;
|
||||||
= note: `-D clippy::manual-swap` implied by `-D warnings`
|
= note: `-D clippy::manual-swap` implied by `-D warnings`
|
||||||
|
|
||||||
error: this looks like you are swapping elements of `foo` manually
|
error: this looks like you are swapping elements of `foo` manually
|
||||||
--> $DIR/swap.rs:36:5
|
--> $DIR/swap.rs:42:5
|
||||||
|
|
|
|
||||||
LL | / let temp = foo[0];
|
LL | / let temp = foo[0];
|
||||||
LL | | foo[0] = foo[1];
|
LL | | foo[0] = foo[1];
|
||||||
|
@ -17,7 +17,7 @@ LL | | foo[1] = temp;
|
||||||
| |_________________^ help: try: `foo.swap(0, 1)`
|
| |_________________^ help: try: `foo.swap(0, 1)`
|
||||||
|
|
||||||
error: this looks like you are swapping elements of `foo` manually
|
error: this looks like you are swapping elements of `foo` manually
|
||||||
--> $DIR/swap.rs:45:5
|
--> $DIR/swap.rs:51:5
|
||||||
|
|
|
|
||||||
LL | / let temp = foo[0];
|
LL | / let temp = foo[0];
|
||||||
LL | | foo[0] = foo[1];
|
LL | | foo[0] = foo[1];
|
||||||
|
@ -25,7 +25,7 @@ LL | | foo[1] = temp;
|
||||||
| |_________________^ help: try: `foo.swap(0, 1)`
|
| |_________________^ help: try: `foo.swap(0, 1)`
|
||||||
|
|
||||||
error: this looks like you are swapping `a` and `b` manually
|
error: this looks like you are swapping `a` and `b` manually
|
||||||
--> $DIR/swap.rs:65:7
|
--> $DIR/swap.rs:71:7
|
||||||
|
|
|
|
||||||
LL | ; let t = a;
|
LL | ; let t = a;
|
||||||
| _______^
|
| _______^
|
||||||
|
@ -36,7 +36,7 @@ LL | | b = t;
|
||||||
= note: or maybe you should use `std::mem::replace`?
|
= note: or maybe you should use `std::mem::replace`?
|
||||||
|
|
||||||
error: this looks like you are swapping `c.0` and `a` manually
|
error: this looks like you are swapping `c.0` and `a` manually
|
||||||
--> $DIR/swap.rs:74:7
|
--> $DIR/swap.rs:80:7
|
||||||
|
|
|
|
||||||
LL | ; let t = c.0;
|
LL | ; let t = c.0;
|
||||||
| _______^
|
| _______^
|
||||||
|
@ -47,7 +47,7 @@ LL | | a = t;
|
||||||
= note: or maybe you should use `std::mem::replace`?
|
= note: or maybe you should use `std::mem::replace`?
|
||||||
|
|
||||||
error: this looks like you are trying to swap `a` and `b`
|
error: this looks like you are trying to swap `a` and `b`
|
||||||
--> $DIR/swap.rs:62:5
|
--> $DIR/swap.rs:68:5
|
||||||
|
|
|
|
||||||
LL | / a = b;
|
LL | / a = b;
|
||||||
LL | | b = a;
|
LL | | b = a;
|
||||||
|
@ -57,7 +57,7 @@ LL | | b = a;
|
||||||
= note: or maybe you should use `std::mem::replace`?
|
= note: or maybe you should use `std::mem::replace`?
|
||||||
|
|
||||||
error: this looks like you are trying to swap `c.0` and `a`
|
error: this looks like you are trying to swap `c.0` and `a`
|
||||||
--> $DIR/swap.rs:71:5
|
--> $DIR/swap.rs:77:5
|
||||||
|
|
|
|
||||||
LL | / c.0 = a;
|
LL | / c.0 = a;
|
||||||
LL | | a = c.0;
|
LL | | a = c.0;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// does not test any rustfixable lints
|
// does not test any rustfixable lints
|
||||||
|
|
||||||
#![warn(clippy::clone_on_ref_ptr)]
|
#![warn(clippy::clone_on_ref_ptr)]
|
||||||
#![allow(unused)]
|
#![allow(unused, clippy::redundant_clone)]
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::{self, Rc};
|
use std::rc::{self, Rc};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue