Incorrect package-private field is assigned but never accessed warning

Answered
  • Version : IntelliJ IDEA 2024.2.0.2 (Community Edition)
  • Lombok Plugin: 242.20224.419

This is the ‘unused declaration’ check for java. Imagine this class

@Getter // from lombok
public class DoesThings {
  @Inject // jakarta.inject
  Thing thing;
  public void doThings() {
    getThing().doThing();
  }
}

IntelliJ complaining with the warning “package-private field ‘thing’ is assigned but never accessed” and _encourages me to safely delete the field_. It is, of course, objectively wrong about this, because I really can't safely delete this member at all..

  • jakarta.inject.Inject is already marked as an entrypoint (implicitly written).
  • At runtime, if the field is marked private then there are some complaints from CDI about “using private members with injection, I don't really like it, but OK then”; which is why the fields are package-private.

Yes I realise that I could add a @SuppressWarnings("unused") but that's just silly, because I might genuinely have variables that I _could_ safely delete…

Qualifying the Getter annotation with AccessLevel.PACKAGE does not change behaviour.

1
1 comment

Hello,

Thanks for detailed report!

I passed it to developers via YouTrack: https://youtrack.jetbrains.com/issue/IDEA-358904

Please follow the issue for updates.

0

Please sign in to leave a comment.