Apache Lucene open-source search software
Enable LongDoubleConversion error-prone check (#12010)
Robert Muir06f917929571
diff --git a/gradle/validation/error-prone.gradle b/gradle/validation/error-prone.gradleindex 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.javaindex 00e05ae8dde..ee1cd9f66bb 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@@ -53,7 +53,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();@@ -179,7 +180,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.javaindex cde0dab1c2e..b59ec079d4a 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@@ -101,7 +101,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();@@ -227,7 +228,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.javaindex 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.javaindex 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.javaindex 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 withindiff --git a/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.java b/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.javaindex 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.javaindex 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 * tfndiff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90PointsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/TestLucene90PointsFormat.javaindex 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.javaindex 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.javaindex 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.javaindex 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 copyPagedGrowableWriter 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.javaindex 91790fefbe5..565b06d5d1f 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@@ -593,8 +593,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);}@@ -631,9 +631,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.javaindex 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.javaindex 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.javaindex 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 BaseDirectoryTestCaspublic 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 BaseDirectoryTestCasflushBytesArr[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 BaseDirectoryTestCasmergeBytesArr[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 BaseDirectoryTestCasflushBytesArr[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 BaseDirectoryTestCasmergeBytesArr[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