Skip to content

Commit 3d3d373

Browse files
authored
Merge pull request #2803 from Nemo157/trivially_copy_pass_by_ref
New Lint: Pass small trivially copyable objects by value
2 parents 0c23112 + 621fdcc commit 3d3d373

35 files changed

+431
-102
lines changed

clippy_lints/src/approx_const.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
7171

7272
fn check_lit(cx: &LateContext, lit: &Lit, e: &Expr) {
7373
match lit.node {
74-
LitKind::Float(ref s, FloatTy::F32) => check_known_consts(cx, e, s, "f32"),
75-
LitKind::Float(ref s, FloatTy::F64) => check_known_consts(cx, e, s, "f64"),
76-
LitKind::FloatUnsuffixed(ref s) => check_known_consts(cx, e, s, "f{32, 64}"),
74+
LitKind::Float(s, FloatTy::F32) => check_known_consts(cx, e, s, "f32"),
75+
LitKind::Float(s, FloatTy::F64) => check_known_consts(cx, e, s, "f64"),
76+
LitKind::FloatUnsuffixed(s) => check_known_consts(cx, e, s, "f{32, 64}"),
7777
_ => (),
7878
}
7979
}
8080

81-
fn check_known_consts(cx: &LateContext, e: &Expr, s: &symbol::Symbol, module: &str) {
81+
fn check_known_consts(cx: &LateContext, e: &Expr, s: symbol::Symbol, module: &str) {
8282
let s = s.as_str();
8383
if s.parse::<f64>().is_ok() {
8484
for &(constant, name, min_digits) in KNOWN_CONSTS {

clippy_lints/src/attrs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
151151

152152
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
153153
if is_relevant_item(cx.tcx, item) {
154-
check_attrs(cx, item.span, &item.name, &item.attrs)
154+
check_attrs(cx, item.span, item.name, &item.attrs)
155155
}
156156
match item.node {
157157
ItemExternCrate(_) | ItemUse(_, _) => {
@@ -195,13 +195,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
195195

196196
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
197197
if is_relevant_impl(cx.tcx, item) {
198-
check_attrs(cx, item.span, &item.name, &item.attrs)
198+
check_attrs(cx, item.span, item.name, &item.attrs)
199199
}
200200
}
201201

202202
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
203203
if is_relevant_trait(cx.tcx, item) {
204-
check_attrs(cx, item.span, &item.name, &item.attrs)
204+
check_attrs(cx, item.span, item.name, &item.attrs)
205205
}
206206
}
207207
}
@@ -260,7 +260,7 @@ fn is_relevant_expr(tcx: TyCtxt, tables: &ty::TypeckTables, expr: &Expr) -> bool
260260
}
261261
}
262262

263-
fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
263+
fn check_attrs(cx: &LateContext, span: Span, name: Name, attrs: &[Attribute]) {
264264
if in_macro(span) {
265265
return;
266266
}

clippy_lints/src/bit_mask.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
112112
if let ExprBinary(ref cmp, ref left, ref right) = e.node {
113113
if cmp.node.is_comparison() {
114114
if let Some(cmp_opt) = fetch_int_literal(cx, right) {
115-
check_compare(cx, left, cmp.node, cmp_opt, &e.span)
115+
check_compare(cx, left, cmp.node, cmp_opt, e.span)
116116
} else if let Some(cmp_val) = fetch_int_literal(cx, left) {
117-
check_compare(cx, right, invert_cmp(cmp.node), cmp_val, &e.span)
117+
check_compare(cx, right, invert_cmp(cmp.node), cmp_val, e.span)
118118
}
119119
}
120120
}
@@ -156,7 +156,7 @@ fn invert_cmp(cmp: BinOp_) -> BinOp_ {
156156
}
157157

158158

159-
fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u128, span: &Span) {
159+
fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u128, span: Span) {
160160
if let ExprBinary(ref op, ref left, ref right) = bit_op.node {
161161
if op.node != BiBitAnd && op.node != BiBitOr {
162162
return;
@@ -167,15 +167,15 @@ fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u12
167167
}
168168
}
169169

170-
fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value: u128, cmp_value: u128, span: &Span) {
170+
fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value: u128, cmp_value: u128, span: Span) {
171171
match cmp_op {
172172
BiEq | BiNe => match bit_op {
173173
BiBitAnd => if mask_value & cmp_value != cmp_value {
174174
if cmp_value != 0 {
175175
span_lint(
176176
cx,
177177
BAD_BIT_MASK,
178-
*span,
178+
span,
179179
&format!(
180180
"incompatible bit mask: `_ & {}` can never be equal to `{}`",
181181
mask_value,
@@ -184,13 +184,13 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
184184
);
185185
}
186186
} else if mask_value == 0 {
187-
span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
187+
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
188188
},
189189
BiBitOr => if mask_value | cmp_value != cmp_value {
190190
span_lint(
191191
cx,
192192
BAD_BIT_MASK,
193-
*span,
193+
span,
194194
&format!(
195195
"incompatible bit mask: `_ | {}` can never be equal to `{}`",
196196
mask_value,
@@ -205,63 +205,63 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
205205
span_lint(
206206
cx,
207207
BAD_BIT_MASK,
208-
*span,
208+
span,
209209
&format!(
210210
"incompatible bit mask: `_ & {}` will always be lower than `{}`",
211211
mask_value,
212212
cmp_value
213213
),
214214
);
215215
} else if mask_value == 0 {
216-
span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
216+
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
217217
},
218218
BiBitOr => if mask_value >= cmp_value {
219219
span_lint(
220220
cx,
221221
BAD_BIT_MASK,
222-
*span,
222+
span,
223223
&format!(
224224
"incompatible bit mask: `_ | {}` will never be lower than `{}`",
225225
mask_value,
226226
cmp_value
227227
),
228228
);
229229
} else {
230-
check_ineffective_lt(cx, *span, mask_value, cmp_value, "|");
230+
check_ineffective_lt(cx, span, mask_value, cmp_value, "|");
231231
},
232-
BiBitXor => check_ineffective_lt(cx, *span, mask_value, cmp_value, "^"),
232+
BiBitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"),
233233
_ => (),
234234
},
235235
BiLe | BiGt => match bit_op {
236236
BiBitAnd => if mask_value <= cmp_value {
237237
span_lint(
238238
cx,
239239
BAD_BIT_MASK,
240-
*span,
240+
span,
241241
&format!(
242242
"incompatible bit mask: `_ & {}` will never be higher than `{}`",
243243
mask_value,
244244
cmp_value
245245
),
246246
);
247247
} else if mask_value == 0 {
248-
span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
248+
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
249249
},
250250
BiBitOr => if mask_value > cmp_value {
251251
span_lint(
252252
cx,
253253
BAD_BIT_MASK,
254-
*span,
254+
span,
255255
&format!(
256256
"incompatible bit mask: `_ | {}` will always be higher than `{}`",
257257
mask_value,
258258
cmp_value
259259
),
260260
);
261261
} else {
262-
check_ineffective_gt(cx, *span, mask_value, cmp_value, "|");
262+
check_ineffective_gt(cx, span, mask_value, cmp_value, "|");
263263
},
264-
BiBitXor => check_ineffective_gt(cx, *span, mask_value, cmp_value, "^"),
264+
BiBitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"),
265265
_ => (),
266266
},
267267
_ => (),

clippy_lints/src/eq_op.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl LintPass for EqOp {
5252

5353
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
5454
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
55-
if let ExprBinary(ref op, ref left, ref right) = e.node {
55+
if let ExprBinary(op, ref left, ref right) = e.node {
5656
if in_macro(e.span) {
5757
return;
5858
}
@@ -157,7 +157,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
157157
}
158158

159159

160-
fn is_valid_operator(op: &BinOp) -> bool {
160+
fn is_valid_operator(op: BinOp) -> bool {
161161
match op.node {
162162
BiSub | BiDiv | BiEq | BiLt | BiLe | BiGt | BiGe | BiNe | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr => true,
163163
_ => false,

clippy_lints/src/excessive_precision.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
4646
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
4747
if_chain! {
4848
let ty = cx.tables.expr_ty(expr);
49-
if let TypeVariants::TyFloat(ref fty) = ty.sty;
49+
if let TypeVariants::TyFloat(fty) = ty.sty;
5050
if let hir::ExprLit(ref lit) = expr.node;
51-
if let LitKind::Float(ref sym, _) | LitKind::FloatUnsuffixed(ref sym) = lit.node;
51+
if let LitKind::Float(sym, _) | LitKind::FloatUnsuffixed(sym) = lit.node;
5252
if let Some(sugg) = self.check(sym, fty);
5353
then {
5454
span_lint_and_sugg(
@@ -66,7 +66,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
6666

6767
impl ExcessivePrecision {
6868
// None if nothing to lint, Some(suggestion) if lint neccessary
69-
fn check(&self, sym: &Symbol, fty: &FloatTy) -> Option<String> {
69+
fn check(&self, sym: Symbol, fty: FloatTy) -> Option<String> {
7070
let max = max_digits(fty);
7171
let sym_str = sym.as_str();
7272
if dot_zero_exclusion(&sym_str) {
@@ -79,7 +79,7 @@ impl ExcessivePrecision {
7979
let digits = count_digits(&sym_str);
8080
if digits > max as usize {
8181
let formatter = FloatFormat::new(&sym_str);
82-
let sr = match *fty {
82+
let sr = match fty {
8383
FloatTy::F32 => sym_str.parse::<f32>().map(|f| formatter.format(f)),
8484
FloatTy::F64 => sym_str.parse::<f64>().map(|f| formatter.format(f)),
8585
};
@@ -115,7 +115,7 @@ fn dot_zero_exclusion(s: &str) -> bool {
115115
}
116116
}
117117

118-
fn max_digits(fty: &FloatTy) -> u32 {
118+
fn max_digits(fty: FloatTy) -> u32 {
119119
match fty {
120120
FloatTy::F32 => f32::DIGITS,
121121
FloatTy::F64 => f64::DIGITS,

clippy_lints/src/functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
126126
}
127127

128128
impl<'a, 'tcx> Functions {
129-
fn check_arg_number(&self, cx: &LateContext, decl: &hir::FnDecl, span: Span) {
129+
fn check_arg_number(self, cx: &LateContext, decl: &hir::FnDecl, span: Span) {
130130
let args = decl.inputs.len() as u64;
131131
if args > self.threshold {
132132
span_lint(
@@ -139,7 +139,7 @@ impl<'a, 'tcx> Functions {
139139
}
140140

141141
fn check_raw_ptr(
142-
&self,
142+
self,
143143
cx: &LateContext<'a, 'tcx>,
144144
unsafety: hir::Unsafety,
145145
decl: &'tcx hir::FnDecl,

clippy_lints/src/inline_fn_without_body.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ impl LintPass for Pass {
3838
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
3939
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
4040
if let TraitItemKind::Method(_, TraitMethod::Required(_)) = item.node {
41-
check_attrs(cx, &item.name, &item.attrs);
41+
check_attrs(cx, item.name, &item.attrs);
4242
}
4343
}
4444
}
4545

46-
fn check_attrs(cx: &LateContext, name: &Name, attrs: &[Attribute]) {
46+
fn check_attrs(cx: &LateContext, name: Name, attrs: &[Attribute]) {
4747
for attr in attrs {
4848
if attr.name() != "inline" {
4949
continue;

clippy_lints/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub mod suspicious_trait_impl;
196196
pub mod swap;
197197
pub mod temporary_assignment;
198198
pub mod transmute;
199+
pub mod trivially_copy_pass_by_ref;
199200
pub mod types;
200201
pub mod unicode;
201202
pub mod unsafe_removed_from_name;
@@ -399,6 +400,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
399400
reg.register_late_lint_pass(box large_enum_variant::LargeEnumVariant::new(conf.enum_variant_size_threshold));
400401
reg.register_late_lint_pass(box explicit_write::Pass);
401402
reg.register_late_lint_pass(box needless_pass_by_value::NeedlessPassByValue);
403+
reg.register_late_lint_pass(box trivially_copy_pass_by_ref::TriviallyCopyPassByRef::new(
404+
conf.trivial_copy_size_limit,
405+
&reg.sess.target,
406+
));
402407
reg.register_early_lint_pass(box literal_representation::LiteralDigitGrouping);
403408
reg.register_early_lint_pass(box literal_representation::LiteralRepresentation::new(
404409
conf.literal_representation_threshold
@@ -672,6 +677,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
672677
transmute::TRANSMUTE_PTR_TO_REF,
673678
transmute::USELESS_TRANSMUTE,
674679
transmute::WRONG_TRANSMUTE,
680+
trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF,
675681
types::ABSURD_EXTREME_COMPARISONS,
676682
types::BORROWED_BOX,
677683
types::BOX_VEC,
@@ -916,6 +922,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
916922
methods::SINGLE_CHAR_PATTERN,
917923
misc::CMP_OWNED,
918924
mutex_atomic::MUTEX_ATOMIC,
925+
trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF,
919926
types::BOX_VEC,
920927
vec::USELESS_VEC,
921928
]);

0 commit comments

Comments
 (0)