Eclipse SonarLint 误报 "Unused assignments should be removed (java:S1854)"

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

我正在使用 Eclipse 2020-03 (4.15.0) 和 Sonarlint for Eclipse 5.1.0.17086,我在以下代码中收到 IMO 误报 S1854 警告(摘自本书 "Java 8 In Action") .使用 Java OpenJDK 13.0.2。这不是一个亮点,因为我只是在学习 Java 8 种技术。我只是想了解为什么标记这些行...

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;
}
}

我是不是漏掉了什么? "length"、"leftTask" 和 "rightTask" 都在代码中多次使用...

上面的class是用下面的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;
    }
}

我对 Sonar 上的警告 S1854 做了一个简短的调查,发现 Github 和 Sonar 网站本身出现了几个问题,显然它已在 February/2020 中修复。检查您的 Sonar 是否是最新的,如果是,则错误可能尚未修复 :(

https://jira.sonarsource.com/browse/SONARJAVA-3281

我终于解决了这个问题,我在sonarlint论坛(https://community.sonarsource.com/t/java-s2166-rule-not-working/22943/15)上被一个post设置在了正确的轨道上。 我的项目使用 Java 13,而我的 Windows cmd "Java -version" 返回 Java 1.8 作为 运行 Java 版本。这种不匹配(Eclipse 以 Java 1.8 启动,而项目使用 Java 13)导致了 SonarLint FP。