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

Incorrect warning explicit lifetimes could be elided #13811

Open
ikezedev opened this issue Dec 11, 2024 · 2 comments
Open

Incorrect warning explicit lifetimes could be elided #13811

ikezedev opened this issue Dec 11, 2024 · 2 comments
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

@ikezedev
Copy link

Summary

I get false positive to remove a lifetime, when indeed that lifetime is used.

Lint Name

needless_lifetimes

Reproducer

I tried this code:

use derivative::Derivative;

struct TestContext;

#[derive(Derivative)]
#[derivative(Debug)]
struct SessionTest<'a> {
    #[derivative(Debug = "ignore")]
    context: &'a TestContext,
}

I saw this happen:

warning: the following explicit lifetimes could be elided: 'a
  --> iam-smoke-tests/tests/sessions.rs:16:20
   |
16 | struct SessionTest<'a> {
   |                    ^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
   = note: `#[warn(clippy::needless_lifetimes)]` on by default
help: elide the lifetimes
   |
16 - struct SessionTest<'a> {
16 + struct SessionTest'_> {

I expected to see this happen:

Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1

Additional Labels

No response

@ikezedev ikezedev 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 Dec 11, 2024
@samueltardieu
Copy link
Contributor

The suggested change is also syntactically incorrect: struct SessionTest'_>

@samueltardieu
Copy link
Contributor

samueltardieu commented Dec 11, 2024

This is due to a bug in the derivative crate, which is no longer maintained. derivative generates code but misuses quote::quote_spanned to do so. Using quote instead would fix the problem. This prevents Clippy from distinguishing code generated from the derivative macro attribute from code written by the user. Only the latter should be linted, as indeed the code generated by the macro contains useless references but this should not be reported.

The bug will likely never be fixed in the unmaintained derivative crate, and should no longer be used. See this security advisory for an alternative.

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

No branches or pull requests

2 participants