70

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 Is it possible to disable duplicate code detection in Intellij?

Sebastian
  • 5,721
  • 3
  • 43
  • 69
  • Possible duplicate of [Is it possible to disable duplicate code detection in Intellij?](https://stackoverflow.com/questions/36627002/is-it-possible-to-disable-duplicate-code-detection-in-intellij) – lealceldeiro Feb 14 '18 at 18:38
  • 2
    I don't think so. The linked question is about disabling this inspection completely. – Sebastian Feb 15 '18 at 07:01
  • 1
    For those using PyCharm: `# noinspection DuplicatedCode` – djvg Jul 30 '20 at 15:34

3 Answers3

138

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() {
    ...
}
crea1
  • 11,077
  • 3
  • 36
  • 46
20

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)

user6658417
  • 211
  • 2
  • 3
  • Don't get excited, You know it a bad habit to suppress 'em, right? – Amanuel Nega Nov 04 '16 at 12:35
  • 2
    @AmanuelNega, if you have local test classes to try things out which are not part of the code line, you don't want them to interfere with these types of warnings anywhere else. This is an appreciated response to a valid question posted by the OP. – Pawel Zieminski Oct 20 '17 at 16:41
  • Apparently the comment has to be immediately before the open brace, e.g. if (...) //noinspection Duplicates { ... } -- which forces the open brace onto the next line. – John Velonis Feb 05 '19 at 21:21
  • @JohnVelonis, In idea 2017.3 I have added it just before if block and it works. Note: You can not put any other comment line after this. – Emdadul Sawon Apr 17 '19 at 06:16
18

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

Sebastian
  • 5,721
  • 3
  • 43
  • 69