Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::needless_borrow: this expression creates a reference which is immediately dereferenced by the compiler
What it meansChecks for address of operations (&) that are going to be dereferenced immediately by the compiler. It is in the style group and warns by default.
What you see
warning: this expression creates a reference which is immediately dereferenced by the compiler --> main.rs:5:15 | 5 | let x: &i32 = &&&&&&5; | ^^^^^^^ help: change this to: `&5` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
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?
Suggests that the receiver of the expression borrows the expression.
Code that works but is not idiomatic Rust. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
fn fun(_a: &i32) {}
let x: &i32 = &&&&&&5;
fun(&x);
Write this instead
let x: &i32 = &5;
fun(x);
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::needless_borrow)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
needless_borrow = "allow"One run
cargo clippy -- -A clippy::needless-borrowThe [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
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Checks for address of operations (&) that are going to
be dereferenced immediately by the compiler.
Why is this bad?
Suggests that the receiver of the expression borrows the expression.
Known problems
The lint cannot tell when the implementation of a trait
for &T and T do different things. Removing a borrow
in such a case can change the semantics of the code.
Example
fn fun(_a: &i32) {}
let x: &i32 = &&&&&&5;
fun(&x);
Use instead:
let x: &i32 = &5;
fun(x);
Past names
- ref_in_deref
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- multiple_bound_locationsbound is defined in more than one place
- must_use_unitthis unit-returning function has a `#[must_use]` attribute
- mut_mutex_lockcalling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
- needless_borrows_for_generic_argsthe borrowed expression implements the required traits
- needless_doctest_mainChecks for fn main() {
- needless_elsethis `else` branch is empty
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::needless_returnunneeded `return` statementRead it next →Did this answer your question?