Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

“Variable is never assigned” warning in IntelliJ IDEA can be suppressed only "partially"

Java EE + IntelliJ Idea 2016.3: I've written a class and declared a private field with a @Inject annotation.

I have successfully got rid of the "unused declaration" notification from the "inspection results" window by adding javax.inject.Inject to settings -> editor -> inspections -> Java -> declaration redundancy -> unused declarations -> entry points -> annotations -> mark field as implicitly written when annotated by (based on this post).

Unfortunately the field is still underlined and a yellow marker is present on the scroll bar. Do I configure this separately or am I doing something wrong?

like image 827
rychu Avatar asked Dec 13 '16 15:12

rychu


4 Answers

There are two separate warnings. One says the field was never assigned a value, which is the one you have disabled. The other says the field was never used, which is the warning you are getting.

Add Inject to settings -> editor -> inspections -> java -> declaration redundancy -> unused declarations -> entry points -> annotations -> Mark as entry point if annotated by

like image 80
MarcG Avatar answered Oct 05 '22 14:10

MarcG


Highlight the field, type Alt + Enter "Windows" or ⌥⏎ "Mac" or Just click on the Light Bulb. Select the "Suppress for fields annotated by {annotation name}.

like image 43
Ronny Shibley Avatar answered Oct 05 '22 15:10

Ronny Shibley


You can just choose to ignore it in some cases where it actually make sense,

enter image description here

What you can do is hover the affected area and press Alt + Enter in "Windows" or alt/option/⌥ + Enter in "Mac", and chose to suppress it.

You'll see,

enter image description here

like image 36
prime Avatar answered Oct 05 '22 14:10

prime


It seems that it was a bug in IDEA. I've just updated to IDEA 2016.3.1 and now @Inject and @Resource annotations put in the "entry points" section do not work - they have to be put in the "fields" window (although when they are in "entry points" section I can still observe the "old" behavior i.e. annotated fields are not detected by code inspection yet they are underlined in the editor and yellow marker is visible).

like image 38
rychu Avatar answered Oct 05 '22 15:10

rychu