javaintellij-ideaintellij-inspections

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


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?


Solution

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