Search code examples
javaintellij-ideasuppress-warningscode-inspection

IntelliJ IDEA @SuppressWarnings for inspection with name of tool


I know I can suppress warnings from IntelliJ IDEA inspections like that:

@SuppressWarnings("CollectionDeclaredAsConcreteClass")
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

For person from outside it may not be clear for which tool this suppression is needed.

PMD uses prefixes for that:

@SuppressWarnings("PMD.UnusedLocalVariable")

FindBugs uses dedicated annotation:

@SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")

Is there any way to clearly indicate that this suppression is just for IntelliJ IDEA?


Solution

  • A way of suppressing this for method arguments is to use a javadoc tag instead:

    /**
     * @noinspection CollectionDeclaredAsConcreteClass
     */
    public PropertiesExpander(Properties properties) {
        this.properties.putAll(properties);
    }
    

    This javadoc tag is very IntelliJ specific so no other tool should have any troubles with it. And you can easily identify it by just recognize it as it is or by adding some comment to it:

    /**
     * @noinspection IntelliJ CollectionDeclaredAsConcreteClass
     */
    public PropertiesExpander(Properties properties) {
        this.properties.putAll(properties);
    }
    

    And of course you can shorten it up even more:

    /** @noinspection CollectionDeclaredAsConcreteClass */
    public PropertiesExpander(Properties properties) {
        this.properties.putAll(properties);
    }
    

    General approach

    It is possible to suppress some warnings on a one-by-one basis with special comments instead of the @SuppressWarnings annotation.

    Here's an example with lots of warnings:

    enter image description here

    If you press Alt+Enter on notUsedLocalVariable you can then choose Suppress for statement with comment on the context menu (after going right when choosing Remove variable ...).

    On some variables/fields/methods the selected suppression is just Suppress for statement.

    And now most (not all) of the warnings has been suppressed if you look in the right-hand field:

    enter image description here

    As you can see there can be several suppressions on the same comment line.

    This can seem to be a bit tedious but I think it's the best you can do.