Can I suppress IntelliJ "Unused declaration" inspection for fields?

661 Views Asked by At

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?

2

There are 2 best solutions below

3
On BEST ANSWER

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.

0
On

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