RS.CLIPPY.TOPLEVEL_REF_ARG

An entire binding declared as `ref`, in a function argument or a `let` statement

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: toplevel_ref_arg. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for function arguments and let bindings denoted as ref.

Why is this bad?

The ref declaration makes the function take an owned value, but turns the argument into a reference (which means that the value is destroyed when exiting the function). This adds not much value: either take a reference type, or take an owned value and create references in the body.

For let bindings, let x = &foo; is preferred over let ref x = foo. The type of x is more obvious with the former.

Known problems

If the argument is dereferenced within the function, removing the ref will lead to errors. This can be fixed by removing the dereferences, e.g., changing *x to x within the function.

Example

fn foo(ref _x: u8) {}

Use instead:

fn foo(_x: &u8) {}