Skip to content

Commit 27aa309

Browse files
authored
Merge pull request #1556 from sinkuu/take_by_value
Lint needless take-by-value
2 parents 55ea081 + bf21c84 commit 27aa309

25 files changed

+481
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ All notable changes to this project will be documented in this file.
381381
[`needless_bool`]: https://github.com/Manishearth/rust-clippy/wiki#needless_bool
382382
[`needless_borrow`]: https://github.com/Manishearth/rust-clippy/wiki#needless_borrow
383383
[`needless_lifetimes`]: https://github.com/Manishearth/rust-clippy/wiki#needless_lifetimes
384+
[`needless_pass_by_value`]: https://github.com/Manishearth/rust-clippy/wiki#needless_pass_by_value
384385
[`needless_range_loop`]: https://github.com/Manishearth/rust-clippy/wiki#needless_range_loop
385386
[`needless_return`]: https://github.com/Manishearth/rust-clippy/wiki#needless_return
386387
[`needless_update`]: https://github.com/Manishearth/rust-clippy/wiki#needless_update

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ transparently:
180180

181181
## Lints
182182

183-
There are 191 lints included in this crate:
183+
There are 192 lints included in this crate:
184184

185185
name | default | triggers on
186186
-----------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@@ -287,6 +287,7 @@ name
287287
[needless_bool](https://github.com/Manishearth/rust-clippy/wiki#needless_bool) | warn | if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }`
288288
[needless_borrow](https://github.com/Manishearth/rust-clippy/wiki#needless_borrow) | warn | taking a reference that is going to be automatically dereferenced
289289
[needless_lifetimes](https://github.com/Manishearth/rust-clippy/wiki#needless_lifetimes) | warn | using explicit lifetimes for references in function arguments when elision rules would allow omitting them
290+
[needless_pass_by_value](https://github.com/Manishearth/rust-clippy/wiki#needless_pass_by_value) | warn | functions taking arguments by value, but not consuming them in its body
290291
[needless_range_loop](https://github.com/Manishearth/rust-clippy/wiki#needless_range_loop) | warn | for-looping over a range of indices where an iterator over items would do
291292
[needless_return](https://github.com/Manishearth/rust-clippy/wiki#needless_return) | warn | using a return statement like `return expr;` where an expression would suffice
292293
[needless_update](https://github.com/Manishearth/rust-clippy/wiki#needless_update) | warn | using `Foo { ..base }` when there are no missing fields

clippy_lints/src/array_indexing.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
8383
.map(|end| constcx.eval(end, ExprTypeChecked))
8484
.map(|v| v.ok());
8585

86-
if let Some((start, end)) = to_const_range(start, end, range.limits, size) {
86+
if let Some((start, end)) = to_const_range(&start, &end, range.limits, size) {
8787
if start > size || end > size {
8888
utils::span_lint(cx, OUT_OF_BOUNDS_INDEXING, e.span, "range is out of bounds");
8989
}
@@ -109,18 +109,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
109109

110110
/// Returns an option containing a tuple with the start and end (exclusive) of the range.
111111
fn to_const_range(
112-
start: Option<Option<ConstVal>>,
113-
end: Option<Option<ConstVal>>,
112+
start: &Option<Option<ConstVal>>,
113+
end: &Option<Option<ConstVal>>,
114114
limits: RangeLimits,
115115
array_size: ConstInt
116116
) -> Option<(ConstInt, ConstInt)> {
117-
let start = match start {
117+
let start = match *start {
118118
Some(Some(ConstVal::Integral(x))) => x,
119119
Some(_) => return None,
120120
None => ConstInt::Infer(0),
121121
};
122122

123-
let end = match end {
123+
let end = match *end {
124124
Some(Some(ConstVal::Integral(x))) => {
125125
if limits == RangeLimits::Closed {
126126
(x + ConstInt::Infer(1)).expect("such a big array is not realistic")

clippy_lints/src/consts.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ pub fn lit_to_constant(lit: &LitKind) -> Constant {
203203
}
204204
}
205205

206-
fn constant_not(o: Constant) -> Option<Constant> {
206+
fn constant_not(o: &Constant) -> Option<Constant> {
207207
use self::Constant::*;
208-
match o {
208+
match *o {
209209
Bool(b) => Some(Bool(!b)),
210210
Int(value) => (!value).ok().map(Int),
211211
_ => None,
@@ -216,12 +216,12 @@ fn constant_negate(o: Constant) -> Option<Constant> {
216216
use self::Constant::*;
217217
match o {
218218
Int(value) => (-value).ok().map(Int),
219-
Float(is, ty) => Some(Float(neg_float_str(is), ty)),
219+
Float(is, ty) => Some(Float(neg_float_str(&is), ty)),
220220
_ => None,
221221
}
222222
}
223223

224-
fn neg_float_str(s: String) -> String {
224+
fn neg_float_str(s: &str) -> String {
225225
if s.starts_with('-') {
226226
s[1..].to_owned()
227227
} else {
@@ -271,7 +271,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
271271
},
272272
ExprUnary(op, ref operand) => {
273273
self.expr(operand).and_then(|o| match op {
274-
UnNot => constant_not(o),
274+
UnNot => constant_not(&o),
275275
UnNeg => constant_negate(o),
276276
UnDeref => Some(o),
277277
})

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub mod mut_reference;
107107
pub mod mutex_atomic;
108108
pub mod needless_bool;
109109
pub mod needless_borrow;
110+
pub mod needless_pass_by_value;
110111
pub mod needless_update;
111112
pub mod neg_multiply;
112113
pub mod new_without_default;
@@ -299,6 +300,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
299300
reg.register_late_lint_pass(box unused_io_amount::UnusedIoAmount);
300301
reg.register_late_lint_pass(box large_enum_variant::LargeEnumVariant::new(conf.enum_variant_size_threshold));
301302
reg.register_late_lint_pass(box should_assert_eq::ShouldAssertEq);
303+
reg.register_late_lint_pass(box needless_pass_by_value::NeedlessPassByValue);
302304

303305
reg.register_lint_group("clippy_restrictions", vec![
304306
arithmetic::FLOAT_ARITHMETIC,
@@ -455,6 +457,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
455457
needless_bool::BOOL_COMPARISON,
456458
needless_bool::NEEDLESS_BOOL,
457459
needless_borrow::NEEDLESS_BORROW,
460+
needless_pass_by_value::NEEDLESS_PASS_BY_VALUE,
458461
needless_update::NEEDLESS_UPDATE,
459462
neg_multiply::NEG_MULTIPLY,
460463
new_without_default::NEW_WITHOUT_DEFAULT,

clippy_lints/src/loops.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,8 @@ fn check_for_loop_range<'a, 'tcx>(
549549
|db| {
550550
multispan_sugg(db,
551551
"consider using an iterator".to_string(),
552-
&[(pat.span, &format!("({}, <item>)", ident.node)),
553-
(arg.span, &format!("{}.iter().enumerate(){}{}", indexed, take, skip))]);
552+
vec![(pat.span, format!("({}, <item>)", ident.node)),
553+
(arg.span, format!("{}.iter().enumerate(){}{}", indexed, take, skip))]);
554554
});
555555
} else {
556556
let repl = if starts_at_zero && take.is_empty() {
@@ -568,7 +568,7 @@ fn check_for_loop_range<'a, 'tcx>(
568568
|db| {
569569
multispan_sugg(db,
570570
"consider using an iterator".to_string(),
571-
&[(pat.span, "<item>"), (arg.span, &repl)]);
571+
vec![(pat.span, "<item>".to_string()), (arg.span, repl)]);
572572
});
573573
}
574574
}
@@ -816,8 +816,8 @@ fn check_for_loop_over_map_kv<'a, 'tcx>(
816816
let map = sugg::Sugg::hir(cx, arg, "map");
817817
multispan_sugg(db,
818818
"use the corresponding method".into(),
819-
&[(pat_span, &snippet(cx, new_pat_span, kind)),
820-
(arg_span, &format!("{}.{}s{}()", map.maybe_par(), kind, mutbl))]);
819+
vec![(pat_span, snippet(cx, new_pat_span, kind).into_owned()),
820+
(arg_span, format!("{}.{}s{}()", map.maybe_par(), kind, mutbl))]);
821821
});
822822
}
823823
}

clippy_lints/src/matches.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ fn check_match_ref_pats(cx: &LateContext, ex: &Expr, arms: &[Arm], source: Match
395395
"you don't need to add `&` to both the expression and the patterns",
396396
|db| {
397397
let inner = Sugg::hir(cx, inner, "..");
398-
let template = match_template(expr.span, source, inner);
398+
let template = match_template(expr.span, source, &inner);
399399
db.span_suggestion(expr.span, "try", template);
400400
});
401401
} else {
@@ -405,7 +405,7 @@ fn check_match_ref_pats(cx: &LateContext, ex: &Expr, arms: &[Arm], source: Match
405405
"you don't need to add `&` to all patterns",
406406
|db| {
407407
let ex = Sugg::hir(cx, ex, "..");
408-
let template = match_template(expr.span, source, ex.deref());
408+
let template = match_template(expr.span, source, &ex.deref());
409409
db.span_suggestion(expr.span,
410410
"instead of prefixing all patterns with `&`, you can dereference the expression",
411411
template);
@@ -509,7 +509,7 @@ fn has_only_ref_pats(arms: &[Arm]) -> bool {
509509
mapped.map_or(false, |v| v.iter().any(|el| *el))
510510
}
511511

512-
fn match_template(span: Span, source: MatchSource, expr: Sugg) -> String {
512+
fn match_template(span: Span, source: MatchSource, expr: &Sugg) -> String {
513513
match source {
514514
MatchSource::Normal => format!("match {} {{ .. }}", expr),
515515
MatchSource::IfLetDesugar { .. } => format!("if let .. = {} {{ .. }}", expr),

0 commit comments

Comments
 (0)