mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 01:04:03 +00:00
Allow configuring the trivial copy size limit
This commit is contained in:
parent
7547a4ddef
commit
700ece5648
@ -400,7 +400,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
|
||||
reg.register_late_lint_pass(box large_enum_variant::LargeEnumVariant::new(conf.enum_variant_size_threshold));
|
||||
reg.register_late_lint_pass(box explicit_write::Pass);
|
||||
reg.register_late_lint_pass(box needless_pass_by_value::NeedlessPassByValue);
|
||||
reg.register_late_lint_pass(box trivially_copy_pass_by_ref::TriviallyCopyPassByRef);
|
||||
reg.register_late_lint_pass(box trivially_copy_pass_by_ref::TriviallyCopyPassByRef::new(
|
||||
conf.trivial_copy_size_limit,
|
||||
®.sess.target,
|
||||
));
|
||||
reg.register_early_lint_pass(box literal_representation::LiteralDigitGrouping);
|
||||
reg.register_early_lint_pass(box literal_representation::LiteralRepresentation::new(
|
||||
conf.literal_representation_threshold
|
||||
|
@ -1,8 +1,11 @@
|
||||
use std::cmp;
|
||||
|
||||
use rustc::hir::*;
|
||||
use rustc::hir::map::*;
|
||||
use rustc::hir::intravisit::FnKind;
|
||||
use rustc::lint::*;
|
||||
use rustc::ty::TypeVariants;
|
||||
use rustc::session::config::Config as SessionConfig;
|
||||
use rustc_target::spec::abi::Abi;
|
||||
use rustc_target::abi::LayoutOf;
|
||||
use syntax::ast::NodeId;
|
||||
@ -17,6 +20,14 @@ use crate::utils::{in_macro, is_copy, is_self, span_lint_and_sugg, snippet};
|
||||
/// be passed through registers if they fit into two or less general purpose
|
||||
/// registers.
|
||||
///
|
||||
/// **Known problems:** This lint is target register size dependent, it is
|
||||
/// limited to 32-bit to try and reduce portability problems between 32 and
|
||||
/// 64-bit, but if you are compiling for 8 or 16-bit targets then the limit
|
||||
/// will be different.
|
||||
///
|
||||
/// The configuration option `trivial_copy_size_limit` can be set to override
|
||||
/// this limit for a project.
|
||||
///
|
||||
/// **Example:**
|
||||
/// ```rust
|
||||
/// fn foo(v: &u32) {
|
||||
@ -33,7 +44,24 @@ declare_clippy_lint! {
|
||||
"functions taking small copyable arguments by reference"
|
||||
}
|
||||
|
||||
pub struct TriviallyCopyPassByRef;
|
||||
pub struct TriviallyCopyPassByRef {
|
||||
limit: u64,
|
||||
}
|
||||
|
||||
impl TriviallyCopyPassByRef {
|
||||
pub fn new(limit: Option<u64>, target: &SessionConfig) -> Self {
|
||||
let limit = limit.unwrap_or_else(|| {
|
||||
let bit_width = target.usize_ty.bit_width().expect("usize should have a width") as u64;
|
||||
// Cap the calculated bit width at 32-bits to reduce
|
||||
// portability problems between 32 and 64-bit targets
|
||||
let bit_width = cmp::min(bit_width, 32);
|
||||
let byte_width = bit_width / 8;
|
||||
// Use a limit of 2 times the register bit width
|
||||
byte_width * 2
|
||||
});
|
||||
Self { limit }
|
||||
}
|
||||
}
|
||||
|
||||
impl LintPass for TriviallyCopyPassByRef {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
@ -94,7 +122,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef {
|
||||
if let TypeVariants::TyRef(_, ty, Mutability::MutImmutable) = ty.sty;
|
||||
if is_copy(cx, ty);
|
||||
if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
|
||||
if size < 16;
|
||||
if size <= self.limit;
|
||||
if let Ty_::TyRptr(_, MutTy { ty: ref decl_ty, .. }) = input.node;
|
||||
then {
|
||||
let value_type = if is_self(arg) {
|
||||
|
@ -156,6 +156,8 @@ define_Conf! {
|
||||
(verbose_bit_mask_threshold, "verbose_bit_mask_threshold", 1 => u64),
|
||||
/// Lint: DECIMAL_LITERAL_REPRESENTATION. The lower bound for linting decimal literals
|
||||
(literal_representation_threshold, "literal_representation_threshold", 16384 => u64),
|
||||
/// Lint: TRIVIALLY_COPY_PASS_BY_REF. The maximum size (in bytes) to consider a `Copy` type for passing by value instead of by reference.
|
||||
(trivial_copy_size_limit, "trivial_copy_size_limit", None => Option<u64>),
|
||||
}
|
||||
|
||||
/// Search for the configuration file.
|
||||
|
1
tests/ui-toml/toml_trivially_copy/clippy.toml
Normal file
1
tests/ui-toml/toml_trivially_copy/clippy.toml
Normal file
@ -0,0 +1 @@
|
||||
trivial-copy-size-limit = 2
|
19
tests/ui-toml/toml_trivially_copy/test.rs
Normal file
19
tests/ui-toml/toml_trivially_copy/test.rs
Normal file
@ -0,0 +1,19 @@
|
||||
#![allow(many_single_char_names)]
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct Foo(u8);
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct Bar(u32);
|
||||
|
||||
fn good(a: &mut u32, b: u32, c: &Bar, d: &u32) {
|
||||
}
|
||||
|
||||
fn bad(x: &u16, y: &Foo) {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let (mut a, b, c, d, x, y) = (0, 0, Bar(0), 0, 0, Foo(0));
|
||||
good(&mut a, b, &c, &d);
|
||||
bad(&x, &y);
|
||||
}
|
16
tests/ui-toml/toml_trivially_copy/test.stderr
Normal file
16
tests/ui-toml/toml_trivially_copy/test.stderr
Normal file
@ -0,0 +1,16 @@
|
||||
error: this argument is passed by reference, but would be more efficient if passed by value
|
||||
--> $DIR/test.rs:12:11
|
||||
|
|
||||
12 | fn bad(x: &u16, y: &Foo) {
|
||||
| ^^^^ help: consider passing by value instead: `u16`
|
||||
|
|
||||
= 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/test.rs:12:20
|
||||
|
|
||||
12 | fn bad(x: &u16, y: &Foo) {
|
||||
| ^^^^ help: consider passing by value instead: `Foo`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
@ -1,4 +1,4 @@
|
||||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `third-party`
|
||||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `third-party`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
#![deny(useless_asref)]
|
||||
|
||||
#![allow(trivially_copy_pass_by_ref)]
|
||||
use std::fmt::Debug;
|
||||
|
||||
struct FakeAsRef;
|
||||
|
Loading…
Reference in New Issue
Block a user