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

ptr_arg false positive when Cow's lifetime appears in return type #9218

Closed
dtolnay opened this issue Jul 21, 2022 · 1 comment · Fixed by #11019
Closed

ptr_arg false positive when Cow's lifetime appears in return type #9218

dtolnay opened this issue Jul 21, 2022 · 1 comment · Fixed by #11019
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

@dtolnay
Copy link
Member

dtolnay commented Jul 21, 2022

Summary

Clippy's warn-by-default ptr_arg lint is displeased about function arguments with the type &Cow<T>. That's fine, but &Cow<'lifetime, T> should be exempt from this lint when the Cow's lifetime is not elided.

I hit this false positive in serde_yaml code that deals with deserializing borrowed &str. Since YAML deserializers can be constructed from either a &'de str as input (from_str) or std::io::Read as input (from_reader), and only the former supports deserializing output values that borrow from the input data, some stuff is represented using a Cow internally which is Borrowed if deserializing from str and Owned if deserializing from reader.

Lint Name

ptr_arg

Reproducer

use std::borrow::Cow;

fn repro<'a>(input: &Cow<'a, str>) -> (usize, Option<&'a str>) {
    let wow = input.len() / 2;
    let huh = match input {
        Cow::Borrowed(borrowed) => Some(&borrowed[wow..]),
        Cow::Owned(_) => None,
    };
    (wow, huh)
}

fn main() {
    let input = Cow::Borrowed("asdfjkl");
    assert_eq!(repro(&input), (3, Some("fjkl")));

    let input = Cow::Owned("asdfjkl".to_owned());
    assert_eq!(repro(&input), (3, None));
}
$ cargo clippy
warning: using a reference to `Cow` is not recommended
 --> src/main.rs:3:21
  |
3 | fn repro<'a>(input: &Cow<'a, str>) -> (usize, Option<&'a str>) {
  |                     ^^^^^^^^^^^^^ help: change this to: `&str`
  |
  = note: `#[warn(clippy::ptr_arg)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg

warning: `testing` (bin "testing") generated 1 warning

Version

rustc 1.64.0-nightly (9a7b7d5e5 2022-07-19)
binary: rustc
commit-hash: 9a7b7d5e50ab0b59c6d349bbf005680a7c880e98
commit-date: 2022-07-19
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6

Additional Labels

No response

@dtolnay dtolnay 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 Jul 21, 2022
dtolnay added a commit to dtolnay/serde-yaml that referenced this issue Jul 21, 2022
rust-lang/rust-clippy#9218

    error: using a reference to `Cow` is not recommended
       --> src/libyaml/parser.rs:101:12
        |
    101 |     input: &Cow<'input, [u8]>,
        |            ^^^^^^^^^^^^^^^^^^ help: change this to: `&[u8]`
        |
        = note: `-D clippy::ptr-arg` implied by `-D clippy::all`
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg
@Diggsey
Copy link

Diggsey commented Jun 23, 2023

I also ran into this same issue 👍

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