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

5

56

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?

Ias answered 13/12, 2016 at 15:20 Comment(1)
Show us the code, please.Antidote
O
69

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

Otherwhere answered 13/12, 2016 at 15:33 Comment(4)
Adding the @Inject annotation to the "mark as entry point" section instead the "mark field as implicitly written" did the job. However, before I did that the warning message was Private field 'logger' is never assigned and not never used as you said. I am using the logger field later in the code. Simultaneously the code inspector reported "No suspicious code found" - maybe it is a bug in Idea.Ias
Also note that Alt-Enter provides menu entries to how to turn warnings off.Terrijo
"Alt-Enter provides menu entries to how to turn warnings off" - yes, but in my idea it only directs to "unused declaration". one has to know to click into "entry points" and "annotations"Ias
In my case, I had Spring Value annotation, and I had to add it to the subsection "Mark field as explicitly written if annotated by".Scevour
V
35

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}.

Vicinal answered 24/11, 2017 at 7:1 Comment(3)
I do not have this option in IDEA 2019.1.2 (Ultimate Edition). Where can I find this option now?Hodosh
same here. With light bulb I only see other stuff (Add consturctor parameters, change access modifiert, ..) but no supression 2022.3.1 (Community Edition)Mumps
found it, see my response below were I could add a picture: https://mcmap.net/q/1776027/-variable-is-never-assigned-warning-in-intellij-idea-can-be-suppressed-only-quot-partially-quotMumps
C
10

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

Correy answered 17/6, 2020 at 20:39 Comment(2)
I had used this in the past but totally forgot how to bring this dialogue :D. Thanks for reminding.Strickler
I think it's kinda irritating that it says "suppress unused warning..." but it's in fact the "never assigned warning".Pillowcase
I
6

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).

Ias answered 14/12, 2016 at 7:30 Comment(0)
M
2

Update 2023

I had this issue before, but the screens look different now:

  • I don't have Suppress unused warning if annotated...
  • Now, it is Assume fields annotated by ...@Autowired as implicitly written

The first option didn't appear. I had to select the variable name, instead of the type.

Context Actions Dialog

Mumps answered 3/2, 2023 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.