Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive: cow and into_owned #13624

Open
Hofer-Julian opened this issue Oct 29, 2024 · 1 comment · May be fixed by #13853
Open

False positive: cow and into_owned #13624

Hofer-Julian opened this issue Oct 29, 2024 · 1 comment · May be fixed by #13853
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Hofer-Julian
Copy link

Summary

When using a Cow type clippy warns about the usage of into_owned but it's suggested alternative doesn't work.

Lint Name

clippy::unnecessary_to_owned

Reproducer

I tried this code:

use std::borrow::Cow;

fn main() {
    test_function();
}

fn test_function() -> impl IntoIterator {
    let cow: Cow<'_, Vec<String>> = Cow::Owned(vec![String::from("example")]);

    // Suggestion which fails to compile
    // return cow.iter().cloned();

    cow.into_owned().into_iter()
}

I saw this happen:

unnecessary use of `into_owned`
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
`#[warn(clippy::unnecessary_to_owned)]` on by default

I expected to see this happen:

Either no warning, or a suggestion that works

Version

rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: x86_64-unknown-linux-gnu
release: 1.80.1
LLVM version: 18.1.7

Additional Labels

No response

@Hofer-Julian Hofer-Julian added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Oct 29, 2024
@samueltardieu
Copy link
Contributor

samueltardieu commented Oct 30, 2024

This is indeed a false positive, as well as a false negative: it should have suggested to drop .into_iter().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants