Intellij 警告:从不使用该方法的返回值 [英] Intellij warning: Return value of the method is never used

查看:268
本文介绍了Intellij 警告:从不使用该方法的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些看起来不错的代码,但 Intellij IDEA 警告其许多返回值的方法

I have some code that seems OK, but Intellij IDEA warns about many of its methods that return values that

从不使用方法的返回值


这是下面的实际代码,一个构建器类.


Here is the actual code below, a builder class.

public static class StreamParserBuilder{
    //optional - have defaults:
    private long spanLimit1 = 2000L;
    private long spanLimit2 = 100000L;
    private long spanLimit3 = 3000000L;
    private String[] coordinates = {"L1", "R2"};
    private String outputDirectory = System.getProperty("user.dir");
    private boolean isLastSteam = false;

    //required from the builder.
    private String[] args;
    private String inputFile;
    private String streamData;
    private boolean isPaired;

    public StreamParserBuilder(String[] args, String inputFile, String streamData, boolean isPaired){
        this.args = args;
        this.inputFile = inputFile;
        this.streamData = streamData;
        this.isPaired = isPaired;
    }

    public StreamParserBuilder withSpanLimit1(long spanLimit1){
        this.spanLimit1 = spanLimit1;
        return this;
    }

    public StreamParserBuilder withSpanLimit2(long spanLimit2){
        this.spanLimit2 = spanLimit2;
        return this;
    }

    public StreamParserBuilder withSpanLimit3(long spanLimit3){
        this.spanLimit3 = spanLimit3;
        return this;
    }

    public StreamParserBuilder withCoordinates(String[] coordinates){
        this.coordinates = coordinates;
        return this;
    }

    public StreamParserBuilder withOutputDirectory(String outputDirectory){
        this.outputDirectory = outputDirectory;
        return this;
    }

    public StreamParserBuilder isLastStream(boolean isLastSteam){
        this.isLastSteam = isLastSteam;
        return this;
    }

    public StreamParser build(){
        return new StreamParser(this);
    }

代码是否有问题,也许我错误地实例化了 .build() 方法?我的 StreamParser 构造函数的代码:

Is there an issue with the code, maybe i've instantiated the .build() method incorrectly? The code for my StreamParser constructor:

private StreamParser(StreamParserBuilder streamParserBuilder){
    this.args = streamParserBuilder.args;
    this.inputFile = streamParserBuilder.inputFile;
    this.streamData = streamParserBuilder.streamData;
    this.spanLimit1 = streamParserBuilder.spanLimit1;
    this.spanLimit2 = streamParserBuilder.spanLimit2;
    this.spanLimit3 = streamParserBuilder.spanLimit3;
    this.coordinates = streamParserBuilder.coordinates;
    this.outputDirectory = streamParserBuilder.outputDirectory;
    this.isLastStream = streamParserBuilder.isLastSteam;
    this.isPaired = streamParserBuilder.isPaired;
}

有没有更好的方法来实现这一点?如果代码没问题,是什么原因导致这个警告?

Is there a better way to implement this? If the code is okay, what causes this warning?

使用 StreamParserBuilder,调用 withX 函数:

Usage of the StreamParserBuilder, calling the withX functions:

 StreamParserBuilder streamBuilder = new StreamParserBuilder(args, inputFile, stream, isPaired);
        if (isSpanOneReplaced) streamBuilder.withSpanLimit1(spanLimit1);
        if (isSpanTwoReplaced) streamBuilder.withSpanLimit2(spanLimit2);
        if (isSpanThreeReplaced) streamBuilder.withSpanLimit3(spanLimit3);
        if (areCoordinatesReplaced) streamBuilder.withCoordinates(coordinates);
        if (isOutputDirectoryReplaced) streamBuilder.withOutputDirectory(outputDirectory);
        if (streamCount == streamData.size()) streamBuilder.isLastStream(true);
        StreamParser streamParser = streamBuilder.build();

推荐答案

方法的返回值从未被使用"是来自 Java | 的警告声明冗余 |方法可以是void检查.生成此警告是因为此方法返回的值从未在调用站点使用.可以通过使用 @SuppressWarnings("UnusedReturnValue") 注释类或在 Settings | 中禁用检查来忽略警告.编辑 |检查.

"Return value of the method is never used" is a warning from the Java | Declaration redundancy | Method can be void inspection. This warning is generated because the value returned by this method is never used at the call site. It's possible to ignore the warning by annotating the class with @SuppressWarnings("UnusedReturnValue") or disabling the inspection in Settings | Editor | Inspections.

这篇关于Intellij 警告:从不使用该方法的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆