Suppress duplicate warnings in IntelliJ IDEA by annotation

Intellij IdeaSuppress WarningsCode Duplication

Intellij Idea Problem Overview


Since version 15, IntelliJ warns me about code duplicates.

In some cases this might be intentional, so I want to ignore/suppress this warning by using the @SuppressWarnings annotation. But what is the correct value for this?

Edit: I'm not asking for disabling this kind of inspection completely as in question https://stackoverflow.com/questions/36627002/is-it-possible-to-disable-duplicate-code-detection-in-intellij

Intellij Idea Solutions


Solution 1 - Intellij Idea

This works for me. You have to set it on both classes/methods if you want to suppress the warning both places.

@SuppressWarnings("Duplicates")
private void myDuplicatedMethod() {
    ...
}

Solution 2 - Intellij Idea

Just saw this and thought I would throw this in for posterity. To suppress for just one block rather than the whole method, you can use a line comment:

//noinspection Duplicates

(I also find it handy to do this for unchecked) (I'm using version 2016-2, but I think this has been around awhile)

Solution 3 - Intellij Idea

Thank you for all the answers. There is also another more generic approach to suppress warnings:

  1. Place the cursor within the code that gives you a warning
  2. Hit Alt+Enter
  3. Go to the warning entry (in this case "Navigate to duplicate")
  4. Don't hit Enter but ā†’ (right arrow)
  5. Now you can select some suppressing options (for class/method/statement)

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSebastianView Question on Stackoverflow
Solution 1 - Intellij Ideacrea1View Answer on Stackoverflow
Solution 2 - Intellij Ideauser6658417View Answer on Stackoverflow
Solution 3 - Intellij IdeaSebastianView Answer on Stackoverflow