IntelliJ IDEA 15 EAP Adds On-the-Fly Code Duplicates Detection
It’s been a while since we added the Analyze → Locate Duplicates feature. If you’ve had a chance to use it, you know how helpful it is: by eliminating duplicates in your code, you make it more concise and stable. If you’ve never used it, you definitely should; it’s truly surprising how many duplicates an average codebase usually contains.
The fresh IntelliJ IDEA 15 EAP build released today takes the duplicates analysis to an entirely new level by allowing you to spot them on-the-fly. Now, if you stumble upon an existing duplicate, or somehow create one either by writing or pasting code, you will know it instantly:
The analysis comes with quick-fixes, so you can navigate to duplicates or have them shown in a tool window:
The new inspection is called Duplicated Code and is enabled by default. To toggle it, go to Settings → Editor → Inspections. And, of course, you can run it on the entire project (or a custom scope) any time via Run Inspection by Name.
Note that this feature is currently available only for Java. As the work is still in progress, rare false positives are possible. Don’t forget to report them as well as any other bugs to our tracker.
You’re welcome to download IntelliJ IDEA 15 EAP right away, give this new feature a try, and share your feedback.
Among other enhancements, you’ll find fixes for:
- Big and blurry fonts and icons in the Editor on Linux
- Not working Terminal on Windows 10
Develop with Pleasure!
Oleg says:
August 20, 2015Could you please add a way to configure this inspection similar to how you can configure “find duplicates” action from the menu? For example, I’d like to change “do not show duplicates smaller than” and “anonymize common subexpressions smaller than” parameters for it.
Maxim Mossienko says:
September 2, 2015There are no such options at the moment
Arteom Sedykh says:
November 3, 2015I’ve just installed new IDEA 15.0, and I cant find Analyze → Locate Duplicates neither Settings → Editor → Inspections. What should I do to access these features?
Maksim Sobolevskiy says:
November 3, 2015Hi! What happens if you search Inspections in Settings search bar?
Alexander says:
November 3, 2015Hi.
I have the same issue: IDEA 15.0 (just re-downloaded) and there’s no “Locate duplicates” in Analyze. In settings -> inspections there’s many “duplicate …” but no single “locate duplicates” or “duplicated code”.
Andrey Cheptsov says:
November 3, 2015Find my answer below
Alexander says:
November 4, 2015It looks like I’m using a wrong version.
https://www.dropbox.com/s/niiy0mzvchi0yq3/Screen%20Shot%202015-11-04%20at%2014.49.54.png?dl=0
What is the correct version for now?
And there I don’t have any “duplicated code”:
1. Menu: https://www.dropbox.com/s/lyuhc10gcjokz6i/Screen%20Shot%202015-11-04%20at%2014.58.05.png?dl=0
2. Settings:
https://www.dropbox.com/s/8mhhko7bqz4057w/Screen%20Shot%202015-11-04%20at%2014.51.49.png?dl=0
https://www.dropbox.com/s/xusqrbdb5cvmdab/Screen%20Shot%202015-11-04%20at%2014.52.48.png?dl=0
Andrey Cheptsov says:
November 3, 2015This is how my Analyze menu looks like: https://www.dropbox.com/s/24qfntjhlvbazun/Screenshot%202015-11-04%2001.16.38.png?dl=0
In fact with IntelliJ IDEA 15 you don’t need this ‘Locate Duplicates’ feature anymore as the new ‘Duplicated Code’ inspection does its work much faster:
https://www.dropbox.com/s/jzhirbf9kmh0umf/Screenshot%202015-11-04%2001.16.26.png?dl=0
It’s enabled by default. However you can always run it against the entire project via Analyze | Run Inspection by Name:
https://www.dropbox.com/s/8tikbgbsw2hcfui/Screenshot%202015-11-04%2001.22.08.png?dl=0
Anna Kozlova says:
November 11, 2015This inspection is not available in Community Edition
Lenny Krueger says:
November 3, 2015just found the setting, it’s in File –>Settings –> Editor –> Inspections Once the Inspections menu opens, it’s under General –> Duplicated Code
Gordon Child says:
November 3, 2015Is there a way to disable code duplicates detection for tests? In some cases, I’d prefer to not abstract code when writing tests and don’t mind duplicate code.
Andrey Cheptsov says:
November 3, 2015Yes, you can change the scope of the Duplicated Code inspection from ‘In All Scopes’ to say ‘Production’:
https://www.dropbox.com/s/jzhirbf9kmh0umf/Screenshot%202015-11-04%2001.16.26.png?dl=0
Bartosz Walacik says:
November 3, 2015Is there a way to configure the threshold (now defaulted to 5 lines) when two code blocks are classified as duplicate?
Andrey Cheptsov says:
November 3, 2015Right now there’s no way to configure it.
David says:
November 10, 2015Just downloaded Intellij15 and try to test this out, but when I go to File->Settings->Editor->Inspections and search for Duplicated Code I can’t seem to find it.
Was this options moved?
Maksim Sobolevskiy says:
November 11, 2015This is how my Analyze menu looks like: https://www.dropbox.com/s/24qfntjhlvbazun/Screenshot%202015-11-04%2001.16.38.png?dl=0
In fact with IntelliJ IDEA 15 you don’t need this ‘Locate Duplicates’ feature anymore as the new ‘Duplicated Code’ inspection does its work much faster:
https://www.dropbox.com/s/jzhirbf9kmh0umf/Screenshot%202015-11-04%2001.16.26.png?dl=0
It’s enabled by default. However you can always run it against the entire project via Analyze | Run Inspection by Name:
https://www.dropbox.com/s/8tikbgbsw2hcfui/Screenshot%202015-11-04%2001.22.08.png?dl=0
ILya says:
November 10, 2015It would be convenient to have possibility to define exact methods, or method templates, for which duplicate detection will be disabled.
For example hashCode, equals, toString methods are usually auto generated, and Idea often fires “duplication warning” in the places.
Maxim Mossienko says:
November 12, 2015Sure, we will do it, please watch https://youtrack.jetbrains.com/issue/IDEA-137441
Chris says:
November 30, 20151. Shows duplicates warning where duplicates do not exist. Alt+Enter>show duplicates like this: only one usage found
2. Balloon shows message “Found duplicated code in module/src/main/java/com…” even when I press ctrl+F1. Showing just the class name/es would be more helpful.
Maksim Sobolevskiy says:
December 1, 2015Hi, Chris! Could you provide more information about pint 1?
Give us an example, please.
Chris says:
December 1, 2015Hi,
Not sure what kind of information could provide, given that this depends on the rest of the codebase, not the class in question.
There is a method in one of my classes where all lines of the body are marked as duplicate.
When I Alt+Enter>show duplicates like this: I get “only one usage found” and the body of the method is highlighted.
I did search in the hole project for parts of the method but didn’t find any other occurrences.
It would be helpful if I was given the class name instead of the beginning of it’s path in the balloon.
Vasiliy says:
November 16, 2017I have the same problem. IDEA tells me “Found duplicated code in this file”, but “View duplicates like this” results in “Only one usage found”.
Brooke says:
June 3, 2016This is a great feature. Though I find it gets in the way sometimes and I’d like to change from showing a squiggly line under every line to just the first (or similar). For example I’ve copied code from another dev with the intention to refactor for my purposes though I want to read through it first to understand the changes I need to make. The squiggles everywhere is a bit like someone shouting at me! I don’t want to turn off the feature nor suppress it. I’d just like it to collapse temporarily (until file is closed or committed).
Zsolti says:
August 9, 2016Can I suppress the duplicate code marking for just one particular occurrence?
Cuero Qu says:
September 7, 2016Find an example of ‘Show duplicate warning but actually it’s not duplicate’. Here’s the code:
method test and test2 are reported as duplicates code, while the parameters to inner class Test are different.
public class TestMain {
int anInt = 1;
public void test(int a)
{
TestMain testMain = new TestMain();
System.out.println(“haha”);
System.out.println(anInt);
System.out.println(“hahaa”);
new Test(testMain::test);
System.out.println(anInt);
System.out.println(“hahaaa”);
}
public void test2(int a)
{
TestMain testMain = new TestMain();
System.out.println(“haha”);
System.out.println(anInt);
System.out.println(“hahaa”);
new Test(testMain::test2);
System.out.println(anInt);
System.out.println(“hahaaa”);
}
public class Test {
Test(handler h) {
}
}
public interface handler {
void entitySelector(int a);
}
}
Slava Tyryshkin says:
June 25, 2020hello guys
thank you so much for this new feature
could you please show some scientific papers or internal logic of this algorithm?