javaeclipsesonarlint-eclipse

Eclipse SonarLint false positive "Unused assignments should be removed (java:S1854)"


I'm using Eclipse 2020-03 (4.15.0) with Sonarlint for Eclipse 5.1.0.17086 and I get , IMO, false positive S1854 warnings in the following code (taken from the book "Java 8 In Action"). Working with Java OpenJDK 13.0.2. This is not a showstopper since I am merely studying Java 8 techniques. I just want to understand why these lines are flagged...

package nl.paul.forkjoin;

import java.util.concurrent.RecursiveTask;

public class ForkJoinSumCalculator extends RecursiveTask<Long> {
private static final long serialVersionUID = 1L;

private final long[] numbers;
private final int start;
private final int end;

public static final long THRESHOLD = 10_000;

public ForkJoinSumCalculator(long[] numbers) {
    this(numbers, 0, numbers.length);
}

private ForkJoinSumCalculator(long[] numbers, int start, int end) {
    this.numbers = numbers;
    this.start = start;
    this.end = end;
}

@Override
protected Long compute() {
    int length = end - start; //SonarLint S1854 warning
    if (length <= THRESHOLD) {
        return computeSequentially();
    }
    ForkJoinSumCalculator leftTask = new ForkJoinSumCalculator(numbers, start, start + length / 2); //SonarLint S1854 warning
    ForkJoinSumCalculator rightTask = new ForkJoinSumCalculator(numbers, start + length / 2, end); //SonarLint S1854 warning
    leftTask.fork();
    return leftTask.join() + rightTask.compute();
}

private long computeSequentially() {
    long sum = 0;
    for (int i = start; i < end; i++) {
        sum += numbers[i];
    }
    return sum;
}
}

Did I miss something here? Both "length", "leftTask" and "rightTask" are used several times in the code...

Above class is tested with the following class:

package nl.paul.forkjoin;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class ForkJoinTest {

    public static void main(String[] args) {
        System.out.println(new ForkJoinTest().doIt(10_000_000));
    }

    private long doIt(long n) {
        long[] numbers = LongStream.rangeClosed(0, n).toArray();
        long start = System.currentTimeMillis();
        ForkJoinTask<Long> task = new ForkJoinSumCalculator(numbers);
        long result = new ForkJoinPool().invoke(task);
        long finish = System.currentTimeMillis();
        System.out.println("Processing took " + (finish - start) + " msec.");
        return result;
    }
}

Solution

  • I finally solved the problem, I was set on the right track by a post on the sonarlint forum (https://community.sonarsource.com/t/java-s2166-rule-not-working/22943/15). My projects were working with Java 13 while my Windows cmd "Java -version" returned Java 1.8 as the running Java version. This mismatch (Eclipse starting up with Java 1.8 and the project working with Java 13) caused the SonarLint FP's.