IntelliJ Idea中未使用无副作用方法的未使用结果的警告 [英] Warnings for unused results of side-effect free methods in IntelliJ Idea

查看:485
本文介绍了IntelliJ Idea中未使用无副作用方法的未使用结果的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我不将BigDecimal.divide()方法的结果分配给变量时,我会从IntelliJ Idea中得到一个很好的警告:

When I don't assign result of BigDecimal.divide() method to a variable, I get a nice warning from IntelliJ Idea:

BigDecimal.divide()的结果将被忽略.

Result of BigDecimal.divide() is ignored.

我可以为自己的(无副作用)功能获得相同的警告吗?为我的函数分配Java注释之类的东西.

Can I somehow get the same warning for my own (side-effect free) functions? Something like assigning a Java annotation to my function.

推荐答案

这是忽略了方法调用的结果"检查.默认情况下,它仅报告几个特殊方法,包括java.lang.BigDecimal的所有方法.在检查配置中,您可以添加应以这种方式报告的其他类和方法.

This is the "Result of method call ignored" inspection. By default, it only reports a couple of special methods, including all methods of java.lang.BigDecimal. In the inspection configuration you can add other classes and methods that should be reported in this way.

报告所有忽略的非库调用"复选框将选择项目中的所有类.

The "Report all ignored non-library calls" check box selects all classes in your project.

如果要使用注释,则可以使用 JSR 305批注

If you want to use annotations, you can annotate single methods or entire classes with the JSR 305 annotation

javax.annotation.CheckReturnValue

自IDEA 2016.3起,您甚至可以使用容易出错的注释

Since IDEA 2016.3 you can even use the error prone annotation

com.google.errorprone.annotations.CanIgnoreReturnValue

将单个方法排除在返回值检查之外.使用这两个注释,您可以编写这样的类:

to exclude single methods from return value checking. Using both annotations, you can write a class like this:

import javax.annotation.CheckReturnValue;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

@CheckReturnValue
class A {
  String a() { return "a"; }

  @CanIgnoreReturnValue
  String b() { return "b"; }

  void run() {
    a(); // Warning
    b(); // No warning
  }
}

这篇关于IntelliJ Idea中未使用无副作用方法的未使用结果的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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