History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEABKL-3164
Type: New Feature New Feature
Status: Open Open
Assignee: Eugene Vigdorchik
Reporter: Sascha Weinreuter
Votes: 3
Watchers: 2
Available Workflow Actions

Mark as Stalled
Operations

If you were logged in you would be able to see more operations.
IDEA: Backlog

"assert" QuickFix should introduce a variable for potentially non-constant expressions

Created: 27 Jun 05 18:42   Updated: 19 Feb 07 15:09
Component/s: Code Analysis. Inspection
Affects Version/s: None
Fix Version/s: None

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown

Build: 3,378
Severity: Medium


 Description  « Hide
In the code below, IDEA displays a possible NPE warning for "((PsiVariable)element.getParent())". The "assert" QuickFix however doesn't work because element.getParent() could return a different value (null) for the second invocation. That's why the QuickFix doesn't fix anything

public class Test {
public void test(@NotNull PsiElement element) {
if (checkSomething(element)) {
// This doesn't remove the NPE warning:
// assert ((PsiVariable)element.getParent()) != null;
if (((PsiVariable)element.getParent()).getNameIdentifier() != null) { // ... }
}
}

private boolean checkSomething(@NotNull PsiElement element) { return element.getParent() instanceof PsiVariable; }
}

#1 Offering the QuickFix here seems to be a bug because IDEA doesn't realize that the expression is potentially non-constant. (It does realize this for similar expressions without the type cast).

#2 The QuickFix should actually be offered for any possible NPE-warning and automatically introduce a local variable for potentially non-constant expressions:

PsiVariable psiVariable = ((PsiVariable)element.getParent());
assert psiVariable != null;
if (psiVariable.getNameIdentifier() != null) {
// ...
}



 All   Comments   Work Log   Change History      Sort Order:
Sascha Weinreuter - 27 Jun 05 18:46
Whoops, that's more of a feature than a bug (80% feature, 20% bug )