Bug 344520 - Incorrect identification of dead code when using not-null checks
Summary: Incorrect identification of dead code when using not-null checks
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 3.7 RC1   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-03 01:26 EDT by Emil Koutanov CLA
Modified: 2011-05-12 02:37 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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