Inspectopedia Help

Multiple occurrences of the same expression

Reports multiple equivalent occurrences of the same expression within a method (or constructor, or class initializer) if the result of the expression can be reused.

The expression is reported if it's free of side effects and its result is always the same (in terms of Object.equals()). The examples of such expressions are a + b, Math.max(a, b), a.equals(b), s.substring(a,b). To make sure the result is always the same, it's verified that the variables used in the expression don't change their values between the occurrences of the expression.

Such expressions may contain methods of immutable classes like String, BigDecimal, and so on, and of utility classes like Objects, Math (except random()). The well-known methods, such as Object.equals(), Object.hashCode(), Object.toString(), Comparable.compareTo(), and Comparator.compare() are OK as well because they normally don't have any observable side effects.

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

DuplicateExpressions
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Verbose or redundant code constructs

Use the Expression complexity threshold option to specify the minimal expression complexity threshold. Specifying bigger numbers will remove reports on short expressions.

Path.of and Paths.get calls are treated as equivalent calls if they have the same arguments. These calls are always reported no matter how complex their arguments are. This behaviour can be tweaked using different complexity threshold.

New in 2018.3

Inspection options

Here you can find the description of settings available for the Multiple occurrences of the same expression inspection, and the reference of their default values.

Expression complexity threshold

70

Availability

By default bundled with

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin

Java, 241.18072

Last modified: 18 June 2024