175

Is it possible to disable duplicate code detection in IntelliJ?

I haven't found this feature to be useful and it continues to distract me.

0

5 Answers 5

239

Only available in IntelliJ Ultimate:

To disable duplicate code detection, go to

File → Settings → Editor → Inspections → General → Duplicated code fragment

and uncheck box "Duplicate code fragment". intellij code duplication

5
  • 6
    This option is not available in IDEA community edition.
    – Saikat
    Mar 18, 2017 at 16:35
  • 6
    @takias duplicate code detection isn't a feature in the Community edition at all. It's only present in the Ultimate edition.
    – AER
    Oct 9, 2017 at 22:19
  • 4
    Actually it works after you close the file you work on and reopen it or restart the IDE after you apply these settings
    – ACV
    Mar 5, 2019 at 9:32
  • 1
    The suggested actions should offer to take us to this screen so we can disable these code checks easier.
    – Shadoninja
    Oct 15, 2019 at 22:10
  • The same recipe works for PyCharm Professional
    – akpp
    Jan 25, 2022 at 23:53
170

Add a hint to your code so that others will know your intent:

@SuppressWarnings("Duplicates")
4
  • 17
    Best solution as enables suppression of warring where duplication is intentional.
    – Ian
    Nov 15, 2017 at 11:46
  • 2
    ^ do this, Don't disable it completely. Duplicate code is code smell except in some places Aug 18, 2018 at 0:24
  • 3
    Are you joking? Add code to compensate for IDE deficiency?
    – ACV
    Mar 5, 2019 at 8:25
  • @ACV Well, disable the entire checking is also compensating for IDE deficiency. This at the very least let you fine-tune where the duplication is "acceptable". I don't want to egoistically say others' codes should always contain zero duplication.
    – trungdok
    Nov 9 at 14:59
15

Yes, it's possible, but I would strongly advise against it!

Duplicate code is a form of technical debt. Any duplicated code that contains a bug means you now have a duplicated bug - you then run the risk that when you fix it, you'll only fix it in one place and the duplicate will remain...

If duplicate code warnings are distracting you, then the best strategy for getting rid of them is to remove the code duplication... Your codebase and future maintainers will thank you for it

13
  • 53
    OK, but sometimes I have the old version and the new version of the same class. There duplication is normal, I don't need the warning. It is just very intrusive and hides many other useful warnings. The question is legitimate.
    – Florian F
    Nov 14, 2016 at 11:45
  • 5
    I find it fires erroneously for unit tests with similar setups. Sometimes I can't get it to go away even if I do change the tests fairly significantly. Mar 12, 2018 at 15:57
  • 9
    Its not always as simples as get rid of all duplicates! For example in web controllers there is a lot of similarity which flags as duplicates they are not exact duplicates. The benefits of keeping your controllers logically separated is useful in many ways and that is just one example. Pragmatic not purist gets projects delivered in my experience. Jan 25, 2019 at 8:56
  • 6
    You claim it is possible and yet you don't describe how. There are plenty of situations where it makes no sense to try and abstract. Please at least say "Yes, here is how, BUT ...."
    – Dilapidus
    Jan 31, 2019 at 19:58
  • 4
    this answer is not related to the question at all.
    – ACV
    Mar 5, 2019 at 8:26
10

This answer may be little irrelevant, but I found this helpful, From this answer if you want to disable it for a specific code block, not the entire method or class or ide, then just add the following line just before that code block

//noinspection Duplicates

Note: You can not put any other comment after this line.

0

Could not get any of these answers to work in Webstorm.

To work around this without turning this feature off, I added this line of code:

if (Math.random() === 1) console.info('suppress duplicate code warning')

This makes the warning disappear if you add it in 1 of the 2 dupe files since they are not dupe anymore.

Another alternative if you are using lodash is:

noop('suppress duplicate code warning')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.