How do I mark Java methods as “must use results” for static analysis?

When compiling C or C with GCC, you can mark functions with attributes ((warn_unused_result)). If you call a function that returns something and then don't assign it to anything, the compiler will complain

There are some methods in the Java library I developed. There are such methods - calling them and discarding them always results in a bug I hope API users can identify such errors through static analysis, such as checking with findbugs or IntelliJ

I wonder if there is a method comment that is usually used to mark a method or function as "must use results" Findbugs provides some special bug finding programs for the standard library, but the general method will be very useful

Solution

There is completely a standard comment, which is @ checkreturnvalue Findbugs owns it; See, for example, here

Guava uses it internally – for example, in the configuration methods for splitter – from JSR 305

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>