URL objects are compared with equals or hashCode, which resolve host names over the network

tx0/java:url-equalityrecommendedwarningbug, performance

What it finds

URL.equals and URL.hashCode resolve the host name to compare addresses, as their documentation warns: each call may wait for DNS, and two URLs of virtual hosts on one address count as equal. Keeping URLs in a HashSet or as HashMap keys does the same on every lookup. URI compares by text.

Reported is equals or hashCode called on a java.net.URL variable, and a set or map of URLs created with URL as its element or key type.

From the rule's source, url_equality.rs.

Example from the rule's tests
import java.net.URL;import java.util.HashSet;import java.util.Set; class Links {  boolean same(URL a, URL b) {    Set<URL> seen = new HashSet<URL>();tx0 reports: URL keys are compared by resolving host names    return a.equals(b);tx0 reports: URL compares by resolving host names  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:url-equality" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:url-equality] reason="..."

tx0 explain tx0/java:url-equality shows the same in the terminal. Found a false positive? Report it.