简体   繁体   English

Eclipse SonarLint 误报“应删除未使用的分配 (java:S1854)”

[英]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").我正在将 Eclipse 2020-03 (4.15.0) 与 Sonarlint 一起用于 Eclipse 5.1.0.17086 并且我得到,IMO,误报 S1854 警告在下面的代码中”(取自书中的“Java8 中的操作中的警告”)。 Working with Java OpenJDK 13.0.2.使用 Java OpenJDK 13.0.2。 This is not a showstopper since I am merely studying Java 8 techniques.这不是什么大事,因为我只是在研究 Java 8 技术。 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... “length”、“leftTask”和“rightTask”都在代码中多次使用...

Above class is tested with the following class:上面的 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;
    }
}

I did a brief survey about Warning S1854 on Sonar and saw several issues created on Github and on the Sonar website itself, apparently it was fixed in February/2020.我对 Sonar 上的警告 S1854 进行了简短调查,发现在 Github 和 Sonar 网站本身上创建了几个问题,显然它已在 2020 年 2 月修复。 Check if your Sonar is up to date, and if it is, the error probably hasn't been fixed:(检查您的声纳是否是最新的,如果是,则错误可能尚未修复:(

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

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 ).我终于解决了这个问题,我被 sonarlint 论坛上的帖子设置在正确的轨道上( 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. 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.这种不匹配(Eclipse 启动 Java 1.8 和项目使用 Java 13)导致了 SonarLint FP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM