Skip to content

refactor: drop redundant reference check in Labels.isEmpty() - #2440

Open
david-mollitor-db wants to merge 1 commit into
prometheus:mainfrom
david-mollitor-db:simplify-labels-isempty
Open

refactor: drop redundant reference check in Labels.isEmpty()#2440
david-mollitor-db wants to merge 1 commit into
prometheus:mainfrom
david-mollitor-db:simplify-labels-isempty

Conversation

@david-mollitor-db

Copy link
Copy Markdown

What

Simplify Labels.isEmpty() by dropping the redundant this == EMPTY reference check.

Why

Labels.equals() already begins with if (this == o) return true;, so this.equals(EMPTY) already returns true for the EMPTY singleton that the this == EMPTY clause guards. Removing the clause is therefore behavior-preserving, and it was the sole reason for the @SuppressWarnings("ReferenceEquality") annotation, which is removed as well.

// before
@SuppressWarnings("ReferenceEquality")
public boolean isEmpty() {
  return this == EMPTY || this.equals(EMPTY);
}

// after
public boolean isEmpty() {
  return this.equals(EMPTY);
}

This also aligns isEmpty() with the rest of Labels, which already uses this.equals(EMPTY) (e.g. in toString()).

This pull request and its description were written by Isaac.

The `this == EMPTY` clause is redundant: Labels.equals() already begins with
`if (this == o) return true;`, so `this.equals(EMPTY)` covers the singleton
case the reference check guards. Removing it is behavior-preserving and also
removes the sole reason for the @SuppressWarnings("ReferenceEquality").

Signed-off-by: David Mollitor <david.mollitor@databricks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant