Email me

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-ref

The [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.
Pedanticgroup
allowdefault level
nofixed by cargo clippy --fix
pre 1.29.0added in Rust

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 a Copy type 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

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ