lambda: initial is assignable check; effectively final; acceptable context

This commit is contained in:
anna
2012-07-19 21:18:30 +02:00
parent 714b565acd
commit 812b9a72ea
15 changed files with 303 additions and 24 deletions
@@ -692,6 +692,27 @@ public class HighlightControlFlowUtil {
final HighlightInfo highlightInfo = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, context, description);
QuickFixAction.registerQuickFixAction(highlightInfo, new VariableAccessFromInnerClassFix(variable, innerClass));
return highlightInfo;
} else {
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(context, PsiLambdaExpression.class);
if (lambdaExpression != null) {
if (variable instanceof PsiParameter) {
final PsiElement parent = variable.getParent();
if (parent instanceof PsiParameterList && parent.getParent() == lambdaExpression) {
return null;
}
}
boolean effectivelyFinal = true;
for (PsiReference reference : ReferencesSearch.search(variable)) {
final PsiElement element = reference.getElement();
if (element instanceof PsiExpression && PsiUtil.isOnAssignmentLeftHand((PsiExpression)element)) {
effectivelyFinal = false;
break;
}
}
if (!effectivelyFinal ) {
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, context, "Variable used in lambda expression should be effectively final");
}
}
}
return null;
}
@@ -331,6 +331,9 @@ public class HighlightUtil {
PsiType checkType = typeElement.getType();
PsiType operandType = operand.getType();
if (operandType == null) return null;
if (operandType instanceof PsiLambdaExpressionType) {
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, expression, "Lambda expression is not expected here");
}
if (TypeConversionUtil.isPrimitiveAndNotNull(operandType)
|| TypeConversionUtil.isPrimitiveAndNotNull(checkType)
|| !TypeConversionUtil.areTypesConvertible(operandType, checkType)) {