Standard Analyses

SemmleCode Professional comes with a wide range of pre-packaged analyses, from architectural properties and metrics, to statement-level checks for likely bugs and violations of best practice.


Inefficient String Constructor

This query finds code that uses the java.lang.String(String) constructor.

How to Interpret the Query Results

The query flags all such uses and displays a list of all detected occurrences in the results view.

How to Address the Query Results

Since Strings are immutable, the allocated object is functionally indistinguishable from the argument. It is more memory-efficient to use the argument directly whenever possible.

Source Code
import default

from ClassInstanceExpr e
where e.getConstructor().getDeclaringType() instanceof TypeString and
      e.getArgument(0).getType() instanceof TypeString
select e, "Inefficient new String(String) constructor"
References