Skip to content

needless_lifetimes false positive with impl Trait #10307

Closed
@Lokathor

Description

@Lokathor

Summary

Seems that clippy is too aggressive with lifetime elision (code below)

Lint Name

needless_lifetimes

Reproducer

I tried this code:

#[derive(Clone)]
#[repr(transparent)]
pub struct ZWString(Vec<u16>);
impl ZWString {
  #[inline]
  #[must_use]
  pub fn as_ptr(&self) -> *const u16 {
    self.0.as_ptr()
  }
  #[inline]
  #[must_use]
  pub fn as_mut_ptr(&mut self) -> *mut u16 {
    self.0.as_mut_ptr()
  }
  #[inline]
  pub fn chars<'a>(&'a self) -> impl Iterator<Item = char> + 'a {
    let live_slice: &[u16] = &self.0[..(self.0.len() - 1)];
    core::char::decode_utf16(live_slice.iter().copied())
      .map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))
  }
}

I saw this happen:

the following explicit lifetimes could be elided: 'a
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
`#[warn(clippy::needless_lifetimes)]` on by default

I expected to see this happen:

If you remove the lifetime in question it causes a build error (the return value continues the borrow of &self).

Version

rustc 1.68.0-nightly (c6fcdb690 2022-12-10)
binary: rustc
commit-hash: c6fcdb690609769a240fc8ab0de0ce68d5ea7dba
commit-date: 2022-12-10
host: x86_64-pc-windows-msvc
release: 1.68.0-nightly
LLVM version: 15.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions