-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
When trying to return an iterator over values in an Rc<[T]> that is owned by the function by first converting it to a Vec, the unnecessary_to_owned lint gets triggered. Clippy also suggested a "fix" which wouldn't compile.
error from clippy's suggested code:
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:13:5
|
13 | rc_slice_provider()
| -^^^^^^^^^^^^^^^^^^
| |
| _____creates a temporary value which is freed while still in use
| |
14 | | .iter()
15 | | .copied()
| |_________________- opaque type requires that borrow lasts for `'static`
16 | }
| - temporary value is freed at the end of this statement
Lint Name
unnecessary_to_owned
Reproducer
I tried this code:
#![deny(clippy::unnecessary_to_owned)]
use std::rc::Rc;
#[derive(Copy, Clone)]
struct Foo;
fn rc_slice_provider() -> Rc<[Foo]> {
Rc::from([Foo])
}
fn iterator_provider() -> impl Iterator<Item = Foo> {
rc_slice_provider()
.to_vec() // can't return a reference since the rc is owned by the function
.into_iter()
}I saw this happen:
error: unnecessary use of `to_vec`
--> src/main.rs:13:5
|
13 | / rc_slice_provider()
14 | | .to_vec() // can't return a reference since the rc is owned by the function
15 | | .into_iter()
| |____________________^ help: use: `rc_slice_provider().iter().copied()`
|
I expected to see this happen:
Nothing I suppose. (You could technically create a user-defined iterator that takes ownership of the rc to avoid the reallocation so I guess the lint is somewhat warranted)
Version
example ran in playground but here is my local:
rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: aarch64-apple-darwin
release: 1.84.1
LLVM version: 19.1.5
Additional Labels
No response
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