-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have
Description
Summary
Started showing up with Rust 1.65.0 .
Lint Name
needless_borrow
Reproducer
I tried this code:
fn foo<T: AsRef<[u8]>>(t: T) {
println!("{}", std::mem::size_of::<T>());
let _t: &[u8] = t.as_ref();
}
fn main() {
let a: [u8; 100] = [0u8; 100];
// 100
foo::<[u8; 100]>(a);
foo(a);
// 16
foo::<&[u8]>(&a);
foo(a.as_slice());
// 8
foo::<&[u8; 100]>(&a);
foo(&a);
}
I saw this happen:
warning: the borrowed expression implements the required traits
--> src/main.rs:14:18
|
14 | foo::<&[u8]>(&a);
| ^^ help: change this to: `a`
|
= note: `#[warn(clippy::needless_borrow)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
warning: the borrowed expression implements the required traits
--> src/main.rs:18:23
|
18 | foo::<&[u8; 100]>(&a);
| ^^ help: change this to: `a`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
warning: the borrowed expression implements the required traits
--> src/main.rs:19:9
|
19 | foo(&a);
| ^^ help: change this to: `a`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
warning: `a` (bin "a") generated 3 warnings
I expected to see this happen:
no lint
While it is true that the trait is implemented by the borrowed expression, it is not true that this operation "is going to be dereferenced immediately by the compiler", and it is wrong to suggest that these instances should be changed to not use a reference. First, the calls where the type of T is specified no longer compile with the suggestion, and more importantly this changes the semantics of the code. There might be very different implementations based on the type or a reference was intentionally used to not copy a large object into the function.
Version
rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-unknown-linux-gnu
release: 1.65.0
LLVM version: 15.0.0
Additional Labels
No response
nlordell, cloudhead and lucy
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have