Bug 344520

Summary: Incorrect identification of dead code when using not-null checks
Product: [Eclipse Project] JDT Reporter: Emil Koutanov <ekoutanov>
Component: CoreAssignee: Ayushman Jain <amj87.iitr>
Status: VERIFIED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: srikanth_sankaran
Version: 3.7   
Target Milestone: 3.7 RC1   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Emil Koutanov CLA 2011-05-03 01:26:09 EDT
Build Identifier: 20110218-0911

Eclipse is reporting null code on else-if branches when null checks are used.

Example: the following code reports dead on the second else-if branch and the else branch:
  static Filter locateAndParse(File dir) throws IOException
  {
    final File includesFile = new File(dir.getPath() + "/includes");
    final File excludesFile = new File(dir.getPath() + "/excludes");
    
    if (includesFile != null && excludesFile != null)
    {
      throw new IOException(String.format("Directory %s has both an 'includes' and an 'excludes' file: remove one or both", dir));
    }
    else if (includesFile != null)
    {
      return createIncludes(parseFile(includesFile));
    }
    else if (excludesFile != null)
    {
      return createExcludes(parseFile(excludesFile));
    }
    else
    {
      return createAllPass();
    }
  }

Now, substituting the xxxFile != null with a boolean hasXxx that has been assigned from the non-null check fixes it.

  static Filter locateAndParseX(File dir) throws IOException
  {
    final File includesFile = new File(dir.getPath() + "/includes");
    final File excludesFile = new File(dir.getPath() + "/excludes");
    final boolean hasIncludes = includesFile != null;
    final boolean hasExcludes = excludesFile != null;
    
    if (hasIncludes && hasExcludes)
    {
      throw new IOException(String.format("Directory %s has both an 'includes' and an 'excludes' file: remove one or both", dir));
    }
    else if (hasIncludes)
    {
      return createIncludes(parseFile(includesFile));
    }
    else if (hasExcludes)
    {
      return createExcludes(parseFile(excludesFile));
    }
    else
    {
      return createAllPass();
    }
  }

The second method is semantically the same as the first. The only differences is instead of non-null checks, I've substituted booleans. If the first method complains, so should the second one.

Reproducible: Always

Steps to Reproduce:
1.
Open Java editor - create a new class.
2.
Add two static methods as described in 'Details'
3.
Method 1 will complain with "dead code", while method 2 will not complain. The two methods are equivalent.
Comment 1 Emil Koutanov CLA 2011-05-03 01:47:22 EDT
NOT A BUG.

Realised that file is always not null
Comment 2 Srikanth Sankaran CLA 2011-05-12 02:37:41 EDT
Verified for 3.7 RC1