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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.