ComputerDruid
ComputerDruid

Reputation: 17586

Java equivalent of warn_unused_result

GCC supports declaring methods with the attribute warn_unused_result so that any caller who does not save the return value of the method gets a warning.

Does Java have any comparable feature?

Upvotes: 1

Views: 160

Answers (2)

Amir Pashazadeh
Amir Pashazadeh

Reputation: 7302

As far as I know there is no such a thing, but some IDEs such as IntelliJ IDEA warn you so about some known methods (for example methods on a String which return another String).

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691825

No, Java doesn't have such a feature.

This could be accomplished by static code analysis tools like FindBugs, based on an annotation on the method, but I don't think such a rule exists. You could create one, though: FindBugs is free software.

FindBugs does a similar check already for specific API calls like File.delete() (it throws a warning if the result of the method is ignored. See http://findbugs.sourceforge.net/bugDescriptions.html#RV_RETURN_VALUE_IGNORED_BAD_PRACTICE). So you could take this rule as an example.

Upvotes: 2

Related Questions