IntelliJ IDEA 2024.3 Help

Analyze duplicates

IntelliJ IDEA helps you find repetitive blocks of code in a certain set of files by means of the Duplicated code fragment inspection. If you create a duplicate by writing or pasting code, IntelliJ IDEA highlights it right away and suggests quick-fixes.

The inspection works out of the box and has a number of settings that you can change to adjust its behavior.

To see all duplicates in the selected set of files at once, run the inspection by name.

Detecting duplicates on-fly

Configure the inspection

  1. Press Ctrl+Alt+S to open settings and then select Editor | Inspections.

  2. Make sure that the necessary inspection profile is selected in the Profile list.

  3. In the search field of the Inspections dialog, type duplicated code fragment to locate the inspection in the list.

    Once you click the inspection, its settings appear on the right.

    • Severity: select a severity level from the list or create a new one.

    • Scope: select the scope of files in which the inspection is going to operate (the code on which the inspection in going to run).

    • Show duplicates from: select the set of files from which duplicates are going to be shown.

      You can restrict the analysis to see duplicates only from the same module or from the same file to avoid false-positive results.

    • Skip duplicates simpler than: configure the size of the code fragment that is compared against the rest of the code in the selected set of files in units.

      The default value is a compromise between accuracy and the number of found duplicates. Increase the value to get more accurate results. Decrease the value to find more duplicates.

      The exact value of units can be calculated as follows: units = 2 * number of statements + number of expressions.

    • Select the languages for which this inspection is going to work.

  4. Apply the changes and close the dialog.

Configuring the inspection

Ignore names and values while searching for duplicates

For each file type, you can define whether identical entities that only differ by names or values should be treated as duplicates.

  1. Press Ctrl+Alt+S to open settings and then select Editor | Duplicates.

  2. Select file types to which the analysis should apply and select the checkboxes next to the constructs that you want to anonymize.

  3. Apply the changes and close the dialog.

The dialog also allows you to completely exclude a language from the analysis and indexing. If you disable a language on this page, this language will be hidden in the inspection settings.

Anonymize constructs

Suppress the inspection

When you suppress the inspection, the code analysis engine doesn't highlight the problem found by this inspection in the specific piece of code (class, method, field, or statement).

  1. Place the caret at the code element for which you want to suppress the inspection and press Alt+Enter.

  2. Click the arrow next to the Show all duplicates like this action and select the necessary suppress action.

    Suppressing the inspection

For more information, refer to Suppress inspections.

Disable the inspection

When you disable the inspection, you turn it off. It means that the code analysis engine stops searching project files for the problem that this inspection is designed to detect.

  1. Place the caret at the code element for which you want to disable the inspection and press Alt+Enter.

  2. Click the arrow next to the Show all duplicates like this action and select Disable inspection.

    Disabling the inspection

To re-enable the inspection, go to Settings | Editor | Inspections and select the checkbox next to Duplicated code fragment.

For more information, refer to Disable inspections.

Locate duplicates manually (deprecated)

  1. In the main menu, go to Code | Analyze Code | Locate Duplicates....

  2. In the Specify Code Duplication Analysis Scope dialog, specify the analysis scope: whole project, current file, uncommitted files (for the projects under version control), or some custom scope. In addition, you can include test sources in the analysis, too.

  3. In the Code Duplication Analysis Settings dialog, select the languages that you want to analyze.

    For each language, check the options to define the analysis preferences. For example, you can opt to request identical match for code fragments to be considered duplicates, or specify a certain limit below which the code constructs are not considered duplicates (for example, to avoid reporting each if construct in the source code).

    Code Duplication Analysis Settings
  4. In the Duplicates tool window, explore the analysis results.

    Duplicates tool window

Specify Code Duplication Analysis Scope

Item

Description

Whole project

Inspect the whole project.

Module <name>

Inspect the module that is currently selected in the Project tool window.

File <name>

Inspect the file that is currently selected in the Project tool window or opened in the editor.

Selected files

Inspect the files that are currently selected in the Project tool window.

Uncommitted files

This scope is only available for the projects under version control.

Inspect only the files that have not been committed to the version control system.

Directory

Inspect the directory that is currently selected in the Project tool window.

Custom scope

Inspect a custom scope of files. Select a pre-defined scope from the list, or click the Browse button and define the scope in the Scopes dialog that opens.

Include test sources

Inspect the test sources included in the analysis scope.

Inspect injected code

Inspect pieces of code in other languages embedded in your code.

Code Duplication Analysis Settings

Use this dialog to define the sensitivity of search, and set limitation that will help you avoid reporting about every similar code construct. Your preferences are specified in a language-specific context.

Item

Description

Java

To anonymize an element means to define whether identical entities that only differ by names or values should be treated as duplicates. Select elements to anonymize:

  • Anonymize Literals

  • Anonymize Local Variables

  • Anonymize Fields

  • Anonymize Methods

  • Anonymize Types

Configure duplicate parameters:

  • Do not show duplicates simpler than: Set the size of duplicated language constructs that are shown in the result window. By default, the constructs less than 10 units are not included (and this limitation cannot be changed).

  • Visible from outside of the scope only: If this checkbox is selected, verify that the discarded subelement is valid outside the current construct. If the subelement is senseless, it cannot be discarded and should not be considered duplicated.

  • Anonymize uncommon subexpressions simpler than: Set the value of the subelements within language constructs that can be considered similar, to show the construct as duplicate in the result window. The larger is the number, the larger constructs are taken as similar by IntelliJ IDEA.

    The values are set as arbitrary weights based on the element size calculated with additive algorithm. The larger the element, the higher is the calculated value.

ActionScript

ECMAScript 6

Flow JS

Groovy

JavaScript

TypeScript

TypeScript JSX

To anonymize an element means to define whether identical entities that only differ by names or values should be treated as duplicates. Select elements to anonymize:

  • Anonymize Variables

  • Anonymize Literals

  • Anonymize Functions

Configure duplicate parameters:

  • Do not show duplicates simpler than: Set the size of duplicated language constructs that are shown in the result window. By default, the constructs less than 10 units are not included (and this limitation cannot be changed).

  • Anonymize uncommon subexpressions simpler than: Set the value of the subelements within language constructs that can be considered similar, to show the construct as duplicate in the result window. The larger is the number, the larger constructs are taken as similar by IntelliJ IDEA.

    The values are set as arbitrary weights based on the element size calculated with additive algorithm. The larger the element, the higher is the calculated value.

CSS

  • Do not show duplicates containing less than <number> CSS properties: Set the size of duplicated language constructs that are shown in the results window.

XHTML

XML

HTML

  • Do not show duplicates containing less than <number> tags: Set the size of duplicated language constructs that are shown in the results window.

  • Anonymize values of tags and attributes

Last modified: 11 October 2024