New Lint: Pass small trivially copyable objects by value

Fixes #1680

Hardcoded for 64-bit "trivial" size for now
This commit is contained in:
Wim Looman 2018-05-27 14:32:03 +02:00 committed by Wim Looman
parent 0c23112846
commit 7547a4ddef
14 changed files with 269 additions and 9 deletions

View file

@ -5,6 +5,7 @@ pub fn dec_read_dec(i: &mut i32) -> i32 {
ret
}
#[allow(trivially_copy_pass_by_ref)]
pub fn minus_1(i: &i32) -> i32 {
dec_read_dec(&mut i.clone())
}

View file

@ -1,6 +1,6 @@
#![allow(unknown_lints, unused, no_effect, redundant_closure_call, many_single_char_names, needless_pass_by_value, option_map_unit_fn)]
#![allow(unknown_lints, unused, no_effect, redundant_closure_call, many_single_char_names, needless_pass_by_value, option_map_unit_fn, trivially_copy_pass_by_ref)]
#![warn(redundant_closure, needless_borrow)]
fn main() {

View file

@ -1,7 +1,7 @@
#![feature(iterator_for_each)]
use std::iter::repeat;
#[allow(trivially_copy_pass_by_ref)]
fn square_is_lower_64(x: &u32) -> bool { x * x < 64 }
#[allow(maybe_infinite_iter)]

View file

@ -1,4 +1,4 @@
#![allow(trivially_copy_pass_by_ref)]
fn fn_val(i: i32) -> i32 { unimplemented!() }

View file

@ -2,7 +2,7 @@
#![warn(needless_lifetimes, extra_unused_lifetimes)]
#![allow(dead_code, needless_pass_by_value)]
#![allow(dead_code, needless_pass_by_value, trivially_copy_pass_by_ref)]
fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) { }

View file

@ -1,6 +1,6 @@
#![allow(unused)]
#![allow(unused, trivially_copy_pass_by_ref)]
#![warn(mut_from_ref)]
struct Foo;

View file

@ -1,7 +1,7 @@
#![allow(unused_variables)]
#![allow(unused_variables, trivially_copy_pass_by_ref)]
fn takes_an_immutable_reference(a: &i32) {}
fn takes_a_mutable_reference(a: &mut i32) {}

View file

@ -1,6 +1,6 @@
use std::borrow::Cow;
#[allow(trivially_copy_pass_by_ref)]
fn x(y: &i32) -> i32 {
*y
}

View file

@ -0,0 +1,57 @@
#![allow(many_single_char_names, blacklisted_name)]
#[derive(Copy, Clone)]
struct Foo(u32);
#[derive(Copy, Clone)]
struct Bar([u8; 24]);
type Baz = u32;
fn good(a: &mut u32, b: u32, c: &Bar) {
}
fn bad(x: &u32, y: &Foo, z: &Baz) {
}
impl Foo {
fn good(self, a: &mut u32, b: u32, c: &Bar) {
}
fn good2(&mut self) {
}
fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
}
fn bad2(x: &u32, y: &Foo, z: &Baz) {
}
}
impl AsRef<u32> for Foo {
fn as_ref(&self) -> &u32 {
&self.0
}
}
impl Bar {
fn good(&self, a: &mut u32, b: u32, c: &Bar) {
}
fn bad2(x: &u32, y: &Foo, z: &Baz) {
}
}
fn main() {
let (mut foo, bar) = (Foo(0), Bar([0; 24]));
let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);
good(&mut a, b, &c);
bad(&x, &y, &z);
foo.good(&mut a, b, &c);
foo.good2();
foo.bad(&x, &y, &z);
Foo::bad2(&x, &y, &z);
bar.good(&mut a, b, &c);
Bar::bad2(&x, &y, &z);
foo.as_ref();
}

View file

@ -0,0 +1,82 @@
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:14:11
|
14 | fn bad(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `u32`
|
= note: `-D trivially-copy-pass-by-ref` implied by `-D warnings`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:14:20
|
14 | fn bad(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Foo`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:14:29
|
14 | fn bad(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Baz`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:24:12
|
24 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
| ^^^^^ help: consider passing by value instead: `self`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:24:22
|
24 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `u32`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:24:31
|
24 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Foo`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:24:40
|
24 | fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Baz`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:27:16
|
27 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `u32`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:27:25
|
27 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Foo`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:27:34
|
27 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Baz`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:41:16
|
41 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `u32`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:41:25
|
41 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Foo`
error: this argument is passed by reference, but would be more efficient if passed by value
--> $DIR/trivially_copy_pass_by_ref.rs:41:34
|
41 | fn bad2(x: &u32, y: &Foo, z: &Baz) {
| ^^^^ help: consider passing by value instead: `Baz`
error: aborting due to 13 previous errors

View file

@ -1,6 +1,6 @@
#![allow(unused, dead_code, needless_lifetimes, needless_pass_by_value)]
#![allow(unused, dead_code, needless_lifetimes, needless_pass_by_value, trivially_copy_pass_by_ref)]
#![warn(extra_unused_lifetimes)]
fn empty() {

View file

@ -3,7 +3,7 @@
#![warn(wrong_self_convention)]
#![warn(wrong_pub_self_convention)]
#![allow(dead_code)]
#![allow(dead_code, trivially_copy_pass_by_ref)]
fn main() {}