Bug 342707 - Invalid redundant null check warning
Summary: Invalid redundant null check warning
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: 3.7 M7   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-13 09:04 EDT by Greg Watson CLA
Modified: 2011-04-25 02:23 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Greg Watson CLA 2011-04-13 09:04:20 EDT
Eclipse 3.7M6:

In the following code, at line 121 I'm seeing the warning "Potential null pointer access: The variable filePath may be null at this location" but at line 122 I'm seeing the warning "Redundant null check: The variable filePath cannot be null at this location". I believe the second warning is incorrect as filePath could still be null at this point.


	protected boolean computeChildren(OpenableInfo info, IProgressMonitor pm, IResource res) throws CModelException {
		ArrayList<ICElement> vChildren = new ArrayList<ICElement>();
		IPath filePath = null;
		if (fPath != null) {
			filePath = fPath;
		} else if (fIncludeEntry != null) {
			filePath = fIncludeEntry.getFullIncludePath();
		}
121		System.out.println("computeChildren: "+ filePath.toString());
122		if (filePath != null) {
			if (!filePath.isUNC()) {
				File file = filePath.toFile();
				String[] names = null;
				if (file != null && file.isDirectory()) {
					names = file.list();
		
					if (names != null) {
						IPath path = new Path(file.getAbsolutePath());
						for (String name : names) {
							File child = new File(file, name);
							ICElement celement = null;
							if (child.isDirectory()) {
								celement = new IncludeReference(this, fIncludeEntry, new Path(child.getAbsolutePath()));
							} else if (child.isFile()){
								String id = CoreModel.getRegistedContentTypeId(getCProject().getProject(), name);
								if (id != null) {
									// TODO:  should use URI
									celement = new ExternalTranslationUnit(this, URIUtil.toURI(path.append(name)), id);
								}
							}
							if (celement != null) {
								vChildren.add(celement);
							}
						}
					}
				}
			} else {
				try {
					IFileStore store = EFS.getStore(URIPathConverter.toURI(filePath));
					IFileStore children[] = store.childStores(EFS.NONE, pm);
					for (IFileStore child : children) {
						ICElement celement = null;
						if (child.fetchInfo().isDirectory()) {
							celement = new IncludeReference(this, fIncludeEntry, filePath.append(child.getName()));
						} else {
							String id = CoreModel.getRegistedContentTypeId(getCProject().getProject(), child.getName());
							if (id != null) {
								// TODO:  should use URI
								celement = new ExternalTranslationUnit(this, child.toURI(), id);
							}
						}
						if (celement != null) {
							vChildren.add(celement);
						}
					}
				} catch (CoreException e) {
				}
			}
		}
		info.setChildren(vChildren);
		return true;
	}
Comment 1 Ayushman Jain CLA 2011-04-13 09:47:33 EDT
Both warnings are correct. The potential NPE in line 121 is because the value assigned to filePath from fIncludeEntry.getFullIncludePath() may be null. 
Again, at line 122, filePath can no longer be null. If it were null, line 121 would've thrown an NPE and the program would've exited. Hence, in no scenario can filepath still be null if line 122 is reached.

Closing as invalid.
Comment 2 Srikanth Sankaran CLA 2011-04-25 02:23:21 EDT
Verified for 3.7 M7