Suppress "variable is never assigned" warning in IntelliJ IDEA
Asked Answered
I

6

59

We use reflection extensively to set class field values in our code. The fields are accessed in code but they are never assigned except via reflection. So IDEA displays "is never assigned" warning. If I ask IDEA to suppress the inspection, it inserts

@SuppressWarnings({"UnusedDeclaration"})

but this also disables the check of whether the field is used or not, which we do not want.

Is it anyhow possible to disable only "not assigned" check and leave "not used" check for specific fields only?

IDEA version is 10.5

Inappreciative answered 20/7, 2011 at 12:39 Comment(3)
Perhaps you can change the code e.g. by assigning the value, so the warning goes away?Osei
yes that works, but does not show the intention on what I try to achieve by assigning e.g. nullInappreciative
how come I don't see this warning? can you give a simple code sample?Tectonics
O
55

You could use an annotation to mark it as an injected field. (similar to how it would treat @EJB). The IntelliJ inspections (at least with version 10.5) allow you to configure your own annotations to mark fields as being injected.

Select Analyze, Inspect Code from the menu and then go to the unused declaration inspection and you can configure an annotation.

Otterburn answered 20/7, 2011 at 12:56 Comment(5)
Worked nicely, thanks. My fields were already annotated with JAXB's @XmlElement and @XmlAttribute annotations, so I didn't even have to change my source!Tontine
Jeff, can you explain what you mean "you can configure an annotation". I'm running IntelliJ 13 and I can do the inspect code analysis and find the "unused declarations" (which are annotated as @Requirement). What do I need to do to to inform IntelliJ that @Requirement means the value will be injected at runtime?Gospodin
In 13.1: File > Settings. Inspections: Declaration redundancy > Unused symbol. Configure annotations...Wheaton
@Wheaton This works fine for the "unused symbol", but not for the "private field is never assigned" inspection.Gastropod
@Gastropod you might want to take a look at my answer, it addresses the exact issue you pointed outStadium
O
23
  1. run analysis: Analyze > Inpect Code...
  2. Right click on Unused Declaration (below the Declaration redundancy tree node)
  3. Click on the "Configure Annotations..."
  4. Add com.google.google.inject.Inject and javax.inject.Inject
Overmatch answered 18/5, 2014 at 17:46 Comment(2)
I much prefer this method, but be aware that it will trigger a "code re-inspection" that could take a while to complete depending on the size of your code base.Africander
I had to: 3. Click on "Edit Settings" 4. Click "Entry points" 5. Click "Annotations...". I believe the UI has changed since 2014 :)Cumquat
S
9

Settings -> Editor -> Inspections -> Declaration Redundancy -> Unused declaration -> Entry points -> Annotations

A new popup opens, split into 2 different areas: "Mark as entry point if annotated by" and "Mark field as implicitly written if annotated by". Only in the latter you click the "+" icon (Add Annotation Class) and select Autowired annotation (or whatever annotation you're using for dependency injection).

This will disable the warning "private field is never assigned" but will not disable the warning "private field is never used" in case you never use the field in the class code, which is the desirable behaviour.

enter image description here

This worked on IntelliJ IDEA Community Edition 2018.3.3 and 2020.2

Stadium answered 10/5, 2019 at 9:23 Comment(1)
Is there a way to do the same for if the class is only ever initialised via a @RequestBody annotation?Insecticide
S
8

To configure annotations in Android Studio 1.1.0 after inspecting your code right click Unused declaration -> Edit Settings -> Configure annotations.

Android Studio 1.1.0

Android Studio 1.1.0

Selfesteem answered 30/3, 2015 at 14:47 Comment(2)
But what do I do after clicking "Configure annotations..."? None of the other answers work for me.Goldshell
@AdamJohns did you figure it out?Chair
O
5

If you popup the actions hint window (alt + Enter on Mac), it should suggest you to suppress warning for Inject annotation

Outfielder answered 22/11, 2015 at 23:27 Comment(0)
R
3

Nope, it seems that IDEA's inspection is not that fine grained. Even with annotation based dependency injected fields the same warning can be suppressed for fields annotated with @Inject. Automatically the warning "not used" is suppressed.

I've just tried running FindBugs-IDEA against the class and no warnings or errors were raised.

Rubino answered 20/7, 2011 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.