Clippy lints / Pedantic
Clippy lint · Pedantic · checked with Clippy 0.1.99
clippy::trivially_copy_pass_by_ref: this argument (4 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
What it meansChecks for functions taking arguments by reference, where the argument type is Copy and small enough to be more efficient to always pass by value. It is in the pedantic group and off by default.
What you see
warning: this argument (4 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte) --> main.rs:3:11 | 3 | fn foo(v: &u32) {} | ^^^^ help: consider passing by value instead: `u32` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#trivially_copy_pass_by_ref = note: `-W clippy::trivially-copy-pass-by-ref` implied by `-W clippy::pedantic`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
In many calling conventions instances of structs will be passed through registers if they fit into two or less general purpose registers.
Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
fn foo(v: &u32) {}
Write this instead
fn foo(v: u32) {}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
trivially_copy_pass_by_ref = "warn"In code
#![warn(clippy::trivially_copy_pass_by_ref)]One run
cargo clippy -- -W clippy::trivially-copy-pass-by-refThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
avoid-breaking-exported-api: Suppress lints whenever the suggested change would cause breakage for other crates.trivial-copy-size-limit: The maximum size (in bytes) to consider a Copy type for passing by value instead of by reference.
Clippy's own documentation
What it does
Checks for functions taking arguments by reference, where
the argument type is Copy and small enough to be more efficient to always
pass by value.
Why is this bad?
In many calling conventions instances of structs will be passed through registers if they fit into two or less general purpose registers.
Known problems
This lint is target dependent, some cases will lint on 64-bit targets but not 32-bit or lower targets.
The configuration option trivial_copy_size_limit can be set to override
this limit for a project.
This lint attempts to allow passing arguments by reference if a reference to that argument is returned. This is implemented by comparing the lifetime of the argument and return value for equality. However, this can cause false positives in cases involving multiple lifetimes that are bounded by each other.
Also, it does not take account of other similar cases where getting memory addresses matters; namely, returning the pointer to the argument in question, and passing the argument, as both references and pointers, to a function that needs the memory address. For further details, refer to this issue that explains a real case in which this false positive led to an undefined behavior introduced with unsafe code.
Example
fn foo(v: &u32) {}
Use instead:
fn foo(v: u32) {}
Configuration
-
avoid-breaking-exported-api: Suppress lints whenever the suggested change would cause breakage for other crates.(default:
true) -
trivial-copy-size-limit: The maximum size (in bytes) to consider aCopytype for passing by value instead of by reference.(default:
target_pointer_width)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_linesChecks for functions with a large amount of lines
- missing_errors_docChecks the doc comments of publicly visible functions that return a Result type and warns if there is no # Errors section
- must_use_candidateChecks for public functions that have no #[must_use] attribute, but return something not already marked must-use, have no mutable arg and mutate no statics
- struct_field_namesfield name starts with the struct's name
- transmute_ptr_to_ptrtransmute from a pointer to a pointer
- unchecked_time_subtractionLints subtraction between an Instant and a Duration, or between two Duration values
- unicode_not_nfcChecks for string literals that contain Unicode in a form that is not equal to its NFC-recomposition
- uninlined_format_argsDetect when a variable is not inlined in a format string, and suggests to inline it
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →Did this answer your question?