You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2022/12/13 02:13:28 UTC

[lucene] branch branch_9x updated: Enable LongDoubleConversion error-prone check (#12010)

This is an automated email from the ASF dual-hosted git repository.

rmuir pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new f57e48ba450 Enable LongDoubleConversion error-prone check (#12010)
f57e48ba450 is described below

commit f57e48ba4505c95b1c6295cef92bbe3a046e7e03
Author: Robert Muir <rm...@apache.org>
AuthorDate: Mon Dec 12 20:55:39 2022 -0500

    Enable LongDoubleConversion error-prone check (#12010)
---
 gradle/validation/error-prone.gradle               |  2 +-
 .../lucene60/TestLucene60PointsFormat.java         |  6 +++--
 .../lucene86/TestLucene86PointsFormat.java         |  6 +++--
 .../BooleanPerceptronClassifier.java               |  4 ++--
 .../lucene/document/LongDistanceFeatureQuery.java  |  4 ++--
 .../org/apache/lucene/index/LogMergePolicy.java    |  4 ++--
 .../org/apache/lucene/index/TieredMergePolicy.java |  2 +-
 .../lucene/search/similarities/BasicModelIne.java  |  2 +-
 .../codecs/lucene90/TestLucene90PointsFormat.java  |  6 +++--
 .../apache/lucene/search/TestLRUQueryCache.java    |  5 +++-
 .../apache/lucene/search/TestTermInSetQuery.java   |  3 ++-
 .../apache/lucene/util/packed/TestPackedInts.java  |  2 +-
 .../lucene/facet/range/TestRangeFacetCounts.java   | 10 ++++----
 .../apache/lucene/search/join/TestJoinUtil.java    |  2 +-
 .../misc/search/TestDocValuesStatsCollector.java   |  4 ++--
 .../TestByteWritesTrackingDirectoryWrapper.java    | 28 +++++++++++-----------
 16 files changed, 50 insertions(+), 40 deletions(-)

diff --git a/gradle/validation/error-prone.gradle b/gradle/validation/error-prone.gradle
index 7c2aecc8cd9..ef8ca778274 100644
--- a/gradle/validation/error-prone.gradle
+++ b/gradle/validation/error-prone.gradle
@@ -378,7 +378,7 @@ allprojects { prj ->
             // '-Xep:LiteProtoToString:OFF', // we don't use protobuf
             // '-Xep:LockNotBeforeTry:OFF', // TODO: there are problems
             '-Xep:LogicalAssignment:WARN',
-            // '-Xep:LongDoubleConversion:OFF', // TODO: there are problems
+            '-Xep:LongDoubleConversion:WARN',
             '-Xep:LongFloatConversion:WARN',
             '-Xep:LoopOverCharArray:WARN',
             '-Xep:MalformedInlineTag:WARN',
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene60/TestLucene60PointsFormat.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene60/TestLucene60PointsFormat.java
index cedac3db07d..3b92ccca46a 100644
--- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene60/TestLucene60PointsFormat.java
+++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene60/TestLucene60PointsFormat.java
@@ -51,7 +51,8 @@ public class TestLucene60PointsFormat extends BasePointsFormatTestCase {
     return codec;
   }
 
-  @SuppressWarnings("NarrowCalculation")
+  // TODO: clean up the math/estimation here rather than suppress so many warnings
+  @SuppressWarnings({"NarrowCalculation", "LongDoubleConversion"})
   public void testEstimatePointCount() throws IOException {
     Directory dir = newDirectory();
     IndexWriterConfig iwc = newIndexWriterConfig();
@@ -177,7 +178,8 @@ public class TestLucene60PointsFormat extends BasePointsFormatTestCase {
 
   // The tree is always balanced in the N dims case, and leaves are
   // not all full so things are a bit different
-  @SuppressWarnings("NarrowCalculation")
+  // TODO: clean up the math/estimation here rather than suppress so many warnings
+  @SuppressWarnings({"NarrowCalculation", "LongDoubleConversion"})
   public void testEstimatePointCount2Dims() throws IOException {
     Directory dir = newDirectory();
     IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene86/TestLucene86PointsFormat.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene86/TestLucene86PointsFormat.java
index 94d39695c3e..36323dfc891 100644
--- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene86/TestLucene86PointsFormat.java
+++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene86/TestLucene86PointsFormat.java
@@ -99,7 +99,8 @@ public class TestLucene86PointsFormat extends BasePointsFormatTestCase {
     super.testMergeStability();
   }
 
-  @SuppressWarnings("NarrowCalculation")
+  // TODO: clean up the math/estimation here rather than suppress so many warnings
+  @SuppressWarnings({"NarrowCalculation", "LongDoubleConversion"})
   public void testEstimatePointCount() throws IOException {
     Directory dir = newDirectory();
     IndexWriterConfig iwc = newIndexWriterConfig();
@@ -225,7 +226,8 @@ public class TestLucene86PointsFormat extends BasePointsFormatTestCase {
 
   // The tree is always balanced in the N dims case, and leaves are
   // not all full so things are a bit different
-  @SuppressWarnings("NarrowCalculation")
+  // TODO: clean up the math/estimation here rather than suppress so many warnings
+  @SuppressWarnings({"NarrowCalculation", "LongDoubleConversion"})
   public void testEstimatePointCount2Dims() throws IOException {
     Directory dir = newDirectory();
     IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/BooleanPerceptronClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/BooleanPerceptronClassifier.java
index 7cd47ccae40..1c59ebbe7ce 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/BooleanPerceptronClassifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/BooleanPerceptronClassifier.java
@@ -147,8 +147,8 @@ public class BooleanPerceptronClassifier implements Classifier<Boolean> {
         Boolean assignedClass = classificationResult.getAssignedClass();
 
         Boolean correctClass = Boolean.valueOf(classField.stringValue());
-        long modifier = correctClass.compareTo(assignedClass);
-        if (modifier != 0) {
+        double modifier = Math.signum(correctClass.compareTo(assignedClass));
+        if (modifier != 0D) {
           updateWeights(
               indexReader,
               scoreDoc.doc,
diff --git a/lucene/core/src/java/org/apache/lucene/document/LongDistanceFeatureQuery.java b/lucene/core/src/java/org/apache/lucene/document/LongDistanceFeatureQuery.java
index 7ab4d3cae34..8f4785ffd0a 100644
--- a/lucene/core/src/java/org/apache/lucene/document/LongDistanceFeatureQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/document/LongDistanceFeatureQuery.java
@@ -267,8 +267,8 @@ final class LongDistanceFeatureQuery extends Query {
       return doc;
     }
 
-    private float score(double distance) {
-      return (float) (boost * (pivotDistance / (pivotDistance + distance)));
+    private float score(long distance) {
+      return (float) (boost * (pivotDistance / (pivotDistance + (double) distance)));
     }
 
     /**
diff --git a/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java b/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java
index c3aa6ea26b0..f78833acded 100644
--- a/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java
+++ b/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java
@@ -494,7 +494,7 @@ public abstract class LogMergePolicy extends MergePolicy {
       }
 
       final SegmentInfoAndLevel infoLevel =
-          new SegmentInfoAndLevel(info, (float) Math.log(size) / norm);
+          new SegmentInfoAndLevel(info, (float) Math.log((double) size) / norm);
       levels.add(infoLevel);
 
       if (verbose(mergeContext)) {
@@ -517,7 +517,7 @@ public abstract class LogMergePolicy extends MergePolicy {
 
     final float levelFloor;
     if (minMergeSize <= 0) levelFloor = (float) 0.0;
-    else levelFloor = (float) (Math.log(minMergeSize) / norm);
+    else levelFloor = (float) (Math.log((double) minMergeSize) / norm);
 
     // Now, we quantize the log values into levels.  The
     // first level is any segment whose log size is within
diff --git a/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.java b/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.java
index abb751da367..304bbdd2e57 100644
--- a/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.java
+++ b/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.java
@@ -693,7 +693,7 @@ public class TieredMergePolicy extends MergePolicy {
     // don't want to make this exponent too large else we
     // can end up doing poor merges of small segments in
     // order to avoid the large merges:
-    mergeScore *= Math.pow(totAfterMergeBytes, 0.05);
+    mergeScore *= Math.pow((double) totAfterMergeBytes, 0.05);
 
     // Strongly favor merges that reclaim deletes:
     final double nonDelRatio = ((double) totAfterMergeBytes) / totBeforeMergeBytes;
diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIne.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIne.java
index a3cadc2fd3c..32e38e39094 100644
--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIne.java
+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIne.java
@@ -34,7 +34,7 @@ public class BasicModelIne extends BasicModel {
   public final double score(BasicStats stats, double tfn, double aeTimes1pTfn) {
     long N = stats.getNumberOfDocuments();
     long F = stats.getTotalTermFreq();
-    double ne = N * (1 - Math.pow((N - 1) / (double) N, F));
+    double ne = N * (1 - Math.pow((N - 1) / (double) N, (double) F));
     double A = log2((N + 1) / (ne + 0.5));
 
     // basic model I(ne) should return A * tfn
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90PointsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90PointsFormat.java
index d1ff4247eec..f5940e7ad3d 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90PointsFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90PointsFormat.java
@@ -98,7 +98,8 @@ public class TestLucene90PointsFormat extends BasePointsFormatTestCase {
     super.testMergeStability();
   }
 
-  @SuppressWarnings("NarrowCalculation")
+  // TODO: clean up the math/estimation here rather than suppress so many warnings
+  @SuppressWarnings({"NarrowCalculation", "LongDoubleConversion"})
   public void testEstimatePointCount() throws IOException {
     Directory dir = newDirectory();
     IndexWriterConfig iwc = newIndexWriterConfig();
@@ -224,7 +225,8 @@ public class TestLucene90PointsFormat extends BasePointsFormatTestCase {
 
   // The tree is always balanced in the N dims case, and leaves are
   // not all full so things are a bit different
-  @SuppressWarnings("NarrowCalculation")
+  // TODO: clean up the math/estimation here rather than suppress so many warnings
+  @SuppressWarnings({"NarrowCalculation", "LongDoubleConversion"})
   public void testEstimatePointCount2Dims() throws IOException {
     Directory dir = newDirectory();
     IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
index 53a8accfc0b..e90be74e234 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
@@ -541,7 +541,10 @@ public class TestLRUQueryCache extends LuceneTestCase {
     final long actualRamBytesUsed = RamUsageTester.ramUsed(queryCache, acc);
     final long expectedRamBytesUsed = queryCache.ramBytesUsed();
     // error < 30%
-    assertEquals(actualRamBytesUsed, expectedRamBytesUsed, 30.d * actualRamBytesUsed / 100.d);
+    assertEquals(
+        (double) actualRamBytesUsed,
+        (double) expectedRamBytesUsed,
+        30.d * actualRamBytesUsed / 100.d);
 
     reader.close();
     w.close();
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestTermInSetQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestTermInSetQuery.java
index 670e699b43f..bac984b4cdc 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestTermInSetQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestTermInSetQuery.java
@@ -238,7 +238,8 @@ public class TestTermInSetQuery extends LuceneTestCase {
     final long actualRamBytesUsed = RamUsageTester.ramUsed(query);
     final long expectedRamBytesUsed = query.ramBytesUsed();
     // error margin within 5%
-    assertEquals(expectedRamBytesUsed, actualRamBytesUsed, actualRamBytesUsed / 20.d);
+    assertEquals(
+        (double) expectedRamBytesUsed, (double) actualRamBytesUsed, actualRamBytesUsed / 20.d);
   }
 
   private static class TermsCountingDirectoryReaderWrapper extends FilterDirectoryReader {
diff --git a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java
index bb857472ab8..2a524a763fd 100644
--- a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java
+++ b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java
@@ -703,7 +703,7 @@ public class TestPackedInts extends LuceneTestCase {
     }
 
     // test ramBytesUsed
-    assertEquals(RamUsageTester.ramUsed(writer), writer.ramBytesUsed(), 8.d);
+    assertEquals((double) RamUsageTester.ramUsed(writer), (double) writer.ramBytesUsed(), 8.d);
 
     // test copy
     PagedGrowableWriter copy =
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/range/TestRangeFacetCounts.java b/lucene/facet/src/test/org/apache/lucene/facet/range/TestRangeFacetCounts.java
index 1ce7642cfd1..97cf11c8e1a 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/range/TestRangeFacetCounts.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/range/TestRangeFacetCounts.java
@@ -556,8 +556,8 @@ public class TestRangeFacetCounts extends FacetTestCase {
     Document doc = new Document();
     DoubleDocValuesField field = new DoubleDocValuesField("field", 0.0);
     doc.add(field);
-    for (long l = 0; l < 100; l++) {
-      field.setDoubleValue(l);
+    for (int i = 0; i < 100; i++) {
+      field.setDoubleValue(i);
       w.addDocument(doc);
     }
 
@@ -591,9 +591,9 @@ public class TestRangeFacetCounts extends FacetTestCase {
     SortedNumericDocValuesField field2 = new SortedNumericDocValuesField("field", 0);
     doc.add(field1);
     doc.add(field2);
-    for (long l = 0; l < 100; l++) {
-      field1.setLongValue(NumericUtils.doubleToSortableLong(l));
-      field2.setLongValue(NumericUtils.doubleToSortableLong(l));
+    for (int i = 0; i < 100; i++) {
+      field1.setLongValue(NumericUtils.doubleToSortableLong(i));
+      field2.setLongValue(NumericUtils.doubleToSortableLong(i));
       w.addDocument(doc);
     }
 
diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java b/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java
index 7700d96e3b5..1dc9afb8461 100644
--- a/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java
+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java
@@ -1943,7 +1943,7 @@ public class TestJoinUtil extends LuceneTestCase {
       document.add(new SortedNumericDocValuesField(fieldName + "LONG", linkLong));
       document.add(
           new SortedNumericDocValuesField(
-              fieldName + "DOUBLE", Double.doubleToRawLongBits(linkLong)));
+              fieldName + "DOUBLE", Double.doubleToRawLongBits((double) linkLong)));
     } else {
       document.add(new SortedDocValuesField(fieldName, new BytesRef(linkValue)));
       document.add(new NumericDocValuesField(fieldName + "INT", linkInt));
diff --git a/lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.java b/lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.java
index b91acf0ad25..2e892900ce4 100644
--- a/lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.java
+++ b/lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.java
@@ -301,8 +301,8 @@ public class TestDocValuesStatsCollector extends LuceneTestCase {
         if (stats.count() > 0) {
           DoubleSummaryStatistics sumStats =
               filterAndFlatValues(docValues, (v) -> v != null).summaryStatistics();
-          assertEquals(sumStats.getMax(), stats.max().longValue(), 0.00001);
-          assertEquals(sumStats.getMin(), stats.min().longValue(), 0.00001);
+          assertEquals(sumStats.getMax(), stats.max(), 0.00001);
+          assertEquals(sumStats.getMin(), stats.min(), 0.00001);
           assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);
           assertEquals(sumStats.getSum(), stats.sum().doubleValue(), 0.00001);
           assertEquals(sumStats.getCount(), stats.valuesCount());
diff --git a/lucene/misc/src/test/org/apache/lucene/misc/store/TestByteWritesTrackingDirectoryWrapper.java b/lucene/misc/src/test/org/apache/lucene/misc/store/TestByteWritesTrackingDirectoryWrapper.java
index 74d9e6e1b57..0b64ed71095 100644
--- a/lucene/misc/src/test/org/apache/lucene/misc/store/TestByteWritesTrackingDirectoryWrapper.java
+++ b/lucene/misc/src/test/org/apache/lucene/misc/store/TestByteWritesTrackingDirectoryWrapper.java
@@ -31,8 +31,8 @@ public class TestByteWritesTrackingDirectoryWrapper extends BaseDirectoryTestCas
   public void testEmptyDir() throws Exception {
     ByteWritesTrackingDirectoryWrapper dir =
         new ByteWritesTrackingDirectoryWrapper(new ByteBuffersDirectory());
-    assertEquals(0.0, dir.getFlushedBytes(), 0.0);
-    assertEquals(0.0, dir.getMergedBytes(), 0.0);
+    assertEquals(0, dir.getFlushedBytes());
+    assertEquals(0, dir.getMergedBytes());
   }
 
   public void testRandomOutput() throws Exception {
@@ -49,8 +49,8 @@ public class TestByteWritesTrackingDirectoryWrapper extends BaseDirectoryTestCas
       flushBytesArr[i] = (byte) random().nextInt(127);
     }
     output.writeBytes(flushBytesArr, flushBytesArr.length);
-    assertEquals(0.0, dir.getFlushedBytes(), 0.0);
-    assertEquals(0.0, dir.getMergedBytes(), 0.0);
+    assertEquals(0, dir.getFlushedBytes());
+    assertEquals(0, dir.getMergedBytes());
     output.close();
 
     // now merge bytes
@@ -61,12 +61,12 @@ public class TestByteWritesTrackingDirectoryWrapper extends BaseDirectoryTestCas
       mergeBytesArr[i] = (byte) random().nextInt(127);
     }
     output.writeBytes(mergeBytesArr, mergeBytesArr.length);
-    assertEquals(expectedFlushBytes, dir.getFlushedBytes(), 0.0);
-    assertEquals(0.0, dir.getMergedBytes(), 0.0);
+    assertEquals(expectedFlushBytes, dir.getFlushedBytes());
+    assertEquals(0, dir.getMergedBytes());
     output.close();
 
-    assertEquals(expectedFlushBytes, dir.getFlushedBytes(), 0.0);
-    assertEquals(expectedMergeBytes, dir.getMergedBytes(), 0.0);
+    assertEquals(expectedFlushBytes, dir.getFlushedBytes());
+    assertEquals(expectedMergeBytes, dir.getMergedBytes());
   }
 
   public void testRandomTempOutput() throws Exception {
@@ -83,8 +83,8 @@ public class TestByteWritesTrackingDirectoryWrapper extends BaseDirectoryTestCas
       flushBytesArr[i] = (byte) random().nextInt(127);
     }
     output.writeBytes(flushBytesArr, flushBytesArr.length);
-    assertEquals(0.0, dir.getFlushedBytes(), 0.0);
-    assertEquals(0.0, dir.getMergedBytes(), 0.0);
+    assertEquals(0, dir.getFlushedBytes());
+    assertEquals(0, dir.getMergedBytes());
     output.close();
 
     // now merge bytes
@@ -96,12 +96,12 @@ public class TestByteWritesTrackingDirectoryWrapper extends BaseDirectoryTestCas
       mergeBytesArr[i] = (byte) random().nextInt(127);
     }
     output.writeBytes(mergeBytesArr, mergeBytesArr.length);
-    assertEquals(expectedFlushBytes, dir.getFlushedBytes(), 0.0);
-    assertEquals(0.0, dir.getMergedBytes(), 0.0);
+    assertEquals(expectedFlushBytes, dir.getFlushedBytes());
+    assertEquals(0, dir.getMergedBytes());
     output.close();
 
-    assertEquals(expectedFlushBytes, dir.getFlushedBytes(), 0.0);
-    assertEquals(expectedMergeBytes, dir.getMergedBytes(), 0.0);
+    assertEquals(expectedFlushBytes, dir.getFlushedBytes());
+    assertEquals(expectedMergeBytes, dir.getMergedBytes());
   }
 
   @Override