Skip to content

Commit 509a154

Browse files
authored
Replace NOT operator with explicit false check - part 7 (#68454)
Part 7. We have an in-house rule to compare explicitly against `false` instead of using the logical not operator (`!`). However, this hasn't historically been enforced, meaning that there are many violations in the source at present. We now have a Checkstyle rule that can detect these cases, but before we can turn it on, we need to fix the existing violations. This is being done over a series of PRs, since there are a lot to fix.
1 parent 8b51548 commit 509a154

File tree

119 files changed

+211
-239
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+211
-239
lines changed

libs/dissect/src/main/java/org/elasticsearch/dissect/DissectKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public final class DissectKey {
9090
break;
9191
}
9292

93-
if (name == null || (name.isEmpty() && !skip)) {
93+
if (name == null || (name.isEmpty() && skip == false)) {
9494
throw new DissectException.KeyParse(key, "The key name could not be determined");
9595
}
9696
}

server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ public void testGetSnapshotsFromIndexBlobOnly() throws Exception {
16581658
boolean atLeastOne = false;
16591659
List<String> indices = new ArrayList<>();
16601660
for (int j = 0; j < numIndices; j++) {
1661-
if (all || randomBoolean() || !atLeastOne) {
1661+
if (all || randomBoolean() || atLeastOne == false) {
16621662
indices.add("test-idx-" + j);
16631663
atLeastOne = true;
16641664
}

server/src/main/java/org/elasticsearch/bootstrap/BootstrapChecks.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static class MlockallCheck implements BootstrapCheck {
306306

307307
@Override
308308
public BootstrapCheckResult check(BootstrapContext context) {
309-
if (BootstrapSettings.MEMORY_LOCK_SETTING.get(context.settings()) && !isMemoryLocked()) {
309+
if (BootstrapSettings.MEMORY_LOCK_SETTING.get(context.settings()) && isMemoryLocked() == false) {
310310
return BootstrapCheckResult.failure("memory locking requested for elasticsearch process but memory is not locked");
311311
} else {
312312
return BootstrapCheckResult.success();
@@ -529,7 +529,7 @@ static class SystemCallFilterCheck implements BootstrapCheck {
529529

530530
@Override
531531
public BootstrapCheckResult check(BootstrapContext context) {
532-
if (BootstrapSettings.SYSTEM_CALL_FILTER_SETTING.get(context.settings()) && !isSystemCallFilterInstalled()) {
532+
if (BootstrapSettings.SYSTEM_CALL_FILTER_SETTING.get(context.settings()) && isSystemCallFilterInstalled() == false) {
533533
final String message = "system call filters failed to install; " +
534534
"check the logs and fix your configuration or disable system call filters at your own risk";
535535
return BootstrapCheckResult.failure(message);

server/src/main/java/org/elasticsearch/cluster/ClusterName.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public boolean equals(Object o) {
5757

5858
ClusterName that = (ClusterName) o;
5959

60-
if (value != null ? !value.equals(that.value) : that.value != null) return false;
61-
62-
return true;
60+
return Objects.equals(value, that.value);
6361
}
6462

6563
@Override

server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
270270
concreteIndices.add(writeIndex.getIndex());
271271
}
272272
} else {
273-
if (indexAbstraction.getIndices().size() > 1 && !options.allowAliasesToMultipleIndices()) {
273+
if (indexAbstraction.getIndices().size() > 1 && options.allowAliasesToMultipleIndices() == false) {
274274
String[] indexNames = new String[indexAbstraction.getIndices().size()];
275275
int i = 0;
276276
for (IndexMetadata indexMetadata : indexAbstraction.getIndices()) {
@@ -828,7 +828,7 @@ public List<String> resolve(Context context, List<String> expressions) {
828828
if (result == null) {
829829
return expressions;
830830
}
831-
if (result.isEmpty() && !options.allowNoIndices()) {
831+
if (result.isEmpty() && options.allowNoIndices() == false) {
832832
IndexNotFoundException infe = new IndexNotFoundException((String)null);
833833
infe.setResources("index_or_alias", expressions.toArray(new String[0]));
834834
throw infe;

server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ public static void toXContent(Metadata metadata, XContentBuilder builder, ToXCon
14801480
metadata.coordinationMetadata().toXContent(builder, params);
14811481
builder.endObject();
14821482

1483-
if (context != XContentContext.API && !metadata.persistentSettings().isEmpty()) {
1483+
if (context != XContentContext.API && metadata.persistentSettings().isEmpty() == false) {
14841484
builder.startObject("settings");
14851485
metadata.persistentSettings().toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true")));
14861486
builder.endObject();

server/src/main/java/org/elasticsearch/cluster/routing/ShardRouting.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.util.Collections;
2525
import java.util.List;
26+
import java.util.Objects;
2627

2728
/**
2829
* {@link ShardRouting} immutably encapsulates information about shard
@@ -519,43 +520,25 @@ public boolean isRelocationSourceOf(ShardRouting other) {
519520

520521
/** returns true if the current routing is identical to the other routing in all but meta fields, i.e., unassigned info */
521522
public boolean equalsIgnoringMetadata(ShardRouting other) {
522-
if (primary != other.primary) {
523-
return false;
524-
}
525-
if (shardId != null ? !shardId.equals(other.shardId) : other.shardId != null) {
526-
return false;
527-
}
528-
if (currentNodeId != null ? !currentNodeId.equals(other.currentNodeId) : other.currentNodeId != null) {
529-
return false;
530-
}
531-
if (relocatingNodeId != null ? !relocatingNodeId.equals(other.relocatingNodeId) : other.relocatingNodeId != null) {
532-
return false;
533-
}
534-
if (allocationId != null ? !allocationId.equals(other.allocationId) : other.allocationId != null) {
535-
return false;
536-
}
537-
if (state != other.state) {
538-
return false;
539-
}
540-
if (recoverySource != null ? !recoverySource.equals(other.recoverySource) : other.recoverySource != null) {
541-
return false;
542-
}
543-
return true;
523+
return primary == other.primary
524+
&& Objects.equals(shardId, other.shardId)
525+
&& Objects.equals(currentNodeId, other.currentNodeId)
526+
&& Objects.equals(relocatingNodeId, other.relocatingNodeId)
527+
&& Objects.equals(allocationId, other.allocationId)
528+
&& state == other.state
529+
&& Objects.equals(recoverySource, other.recoverySource);
544530
}
545531

546532
@Override
547533
public boolean equals(Object o) {
548534
if (this == o) {
549535
return true;
550536
}
551-
if (o == null || !(o instanceof ShardRouting)) {
537+
if (o == null || getClass() != o.getClass()) {
552538
return false;
553539
}
554540
ShardRouting that = (ShardRouting) o;
555-
if (unassignedInfo != null ? !unassignedInfo.equals(that.unassignedInfo) : that.unassignedInfo != null) {
556-
return false;
557-
}
558-
return equalsIgnoringMetadata(that);
541+
return Objects.equals(unassignedInfo, that.unassignedInfo) && equalsIgnoringMetadata(that);
559542
}
560543

561544
/**

server/src/main/java/org/elasticsearch/gateway/PrimaryShardAllocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public AllocateUnassignedDecision makeAllocationDecision(final ShardRouting unas
136136
unassignedShard.index(), unassignedShard.id(), unassignedShard, decidedNode.nodeShardState.getNode());
137137
node = decidedNode.nodeShardState.getNode();
138138
allocationId = decidedNode.nodeShardState.allocationId();
139-
} else if (nodesToAllocate.throttleNodeShards.isEmpty() && !nodesToAllocate.noNodeShards.isEmpty()) {
139+
} else if (nodesToAllocate.throttleNodeShards.isEmpty() && nodesToAllocate.noNodeShards.isEmpty() == false) {
140140
// The deciders returned a NO decision for all nodes with shard copies, so we check if primary shard
141141
// can be force-allocated to one of the nodes.
142142
nodesToAllocate = buildNodesToAllocate(allocation, nodeShardsResult.orderedAllocationCandidates, unassignedShard, true);

server/src/main/java/org/elasticsearch/http/DefaultRestChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private void setHeaderField(HttpResponse response, String headerField, String va
145145
}
146146

147147
private void setHeaderField(HttpResponse response, String headerField, String value, boolean override) {
148-
if (override || !response.containsHeader(headerField)) {
148+
if (override || response.containsHeader(headerField) == false) {
149149
response.addHeader(headerField, value);
150150
}
151151
}

server/src/main/java/org/elasticsearch/ingest/IngestService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ static String getProcessorName(Processor processor) {
604604
sb.append(pipelineName);
605605
}
606606
String tag = processor.getTag();
607-
if(tag != null && !tag.isEmpty()){
607+
if (tag != null && tag.isEmpty() == false) {
608608
sb.append(":");
609609
sb.append(tag);
610610
}

0 commit comments

Comments
 (0)