0

I would like to suppress the "Unused declaration" inpection in IntelliJ (2021.1, also tested on 2020.2) for all fields matching a certain pattern. I was able to suppress the warning for methods using the "Code patterns", but fields don't seem to be affected.

Due to project size, I really need to do that with a pattern to avoid adding annotations to every relevant field.

Here is my test class:

public class TestInspection {

    public static final String SUPPRESSED_NAME = "test";
    public static final String CONSTANT_NAME = "importantConstant";
    
    public void testSuppressedMethod() {
    }
    
    public void testMethod() {
    }
}

These are my settings: enter image description here

And here are the warnings: I would like to remove the first warning (Field 'SUPPRESSED_NAME') enter image description here

Does anyone have any ideas?

  • Feel free to create feature request on YouTrack: https://youtrack.jetbrains.com/issues/IDEA – y.bedrov Apr 13 '21 at 11:06
  • @y.bedrov no need, `@SuppressWarnings("unused")` is available for IDEA – Dropout Apr 13 '21 at 11:11
  • I should clarify that it is important to do that with a pattern, because the project is rather large and adding annotations is not really feasible, I will add it to the question description – PavolGanzarcik Apr 13 '21 at 11:19

2 Answers2

0

Yes, you can annotate the field with @SupressWarnings("unused")

@SuppressWarnings("unused")
public static final String SUPPRESSED_NAME = "test";

As stated in What is the list of valid @SuppressWarnings warning names in Java? you need to check what types of suppressions are available to you based on your environment.

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • Thanks @Dropout, this really seems to be the only way to do it right now (even though it does not really work for big projects) – PavolGanzarcik Apr 14 '21 at 15:08
  • @PavolGanzarcik what problem do you have on large scale? You can annotate classes with it as well, although I'd recommend using it only for specific situations. In other words, yes it is "only a warning" but you ideally don't want to have unused stuff in your code. – Dropout Apr 14 '21 at 21:11
  • the project generates descriptors for fields from the database and then uses reflection to build a UI for them, the descriptor fields are thus unused, but cannot be deleted. It would be a shame to annotate the whole class and lose the warning for other fields and methods. – PavolGanzarcik Apr 19 '21 at 13:05
0

It does not seem to be possible right now. If you want, you can vote for this feature request:

https://youtrack.jetbrains.com/issue/IDEA-232239