Bug 319102

Summary: Suggesting new compiler warning: This method will always return null (false);
Product: [Eclipse Project] JDT Reporter: Lars Svensson <oxvalley>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: VERIFIED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, markus.kell.r, Olivier_Thomann, remy.suen
Version: 3.7   
Target Milestone: 3.7 M1   
Hardware: All   
OS: All   
Whiteboard:

Description Lars Svensson CLA 2010-07-07 06:48:24 EDT
Build Identifier: 

To avoid not returning a valid value.

Reproducible: Always

Steps to Reproduce:
When I use "Create new method" from under the lightbulb menu I get this:

private String getString(){

// TODO auto-generated message stub
return null;
}


Sometims I implement it like this:

private String getString(){
String retVal = "Hello";

if(timeofDay() == EVENING){
retVal="God Evening";
}

return null;
}


Can you spot the error?? I forget to return the value of RetVal, and keeps the "return null;" instead. 

If I hade that warning turned on it would signal now... 

And if I forget to implement the original method all together it will signal as well..
Comment 1 Markus Keller CLA 2010-07-08 11:48:49 EDT
"return null;" is also often exactly what is intended. This warning would yield so many false positives that it would be useless. If you really want to see all these places, you can perform a simple text search.

A solution for you could be to avoid generating such default return statements in the first place: On "Preferences > Java > Code Style > Templates", remove ${body_statement} from the "Method body" template.
Comment 2 Dani Megert CLA 2010-07-09 02:22:17 EDT
I think a check with not too much false positives could be to report a problem if the method has a single 'return null;' along with other statements.
Comment 3 Olivier Thomann CLA 2011-01-25 11:19:31 EST
Verified.