4

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?

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103

2 Answers2

4

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.

maba
  • 47,113
  • 10
  • 108
  • 118
  • Thanks, I am aware of possiblity to add comments instead of @SuppressWarnings, but it doesn't work for me if warning is coming from method parameter. Maybe I was not precise enough, but I'm asking specifically about case from example I gave. – Michal Kordas Sep 03 '15 at 08:35
  • @MichalKordas I edited my answer and suggested another way of suppressing this. And it *does* work for your case. – maba Sep 03 '15 at 09:17
4

I found a way how to achieve that with @SuppressWarnings:

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

Note that there must be space after idea:, otherwise it doesn't work.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
  • 1
    To be honest I like your solution more. Comments in IntelliJ are grayed out and they don't pollute code that much as `@SuppressWarnings`. – Michal Kordas Sep 03 '15 at 20:15