keep annotations for explicit to var fixes (IDEA-193876)

This commit is contained in:
Anna Kozlova
2018-06-14 12:08:25 +03:00
parent 8b375f976d
commit 1e7e741009
7 changed files with 20 additions and 12 deletions

View File

@@ -569,7 +569,7 @@
groupBundle="messages.InspectionsBundle"
groupKey="group.names.declaration.redundancy" enabledByDefault="true" level="INFORMATION"
implementationClass="com.intellij.codeInspection.lambda.RedundantLambdaParameterTypeInspection"
displayName="Remove redundant lambda parameter types"/>
displayName="Redundant lambda parameter types"/>
<localInspection groupPath="Java" language="JAVA" shortName="ReplaceInefficientStreamCount"
groupBundle="messages.InspectionsBundle"
groupKey="group.names.performance.issues" enabledByDefault="true" level="WARNING"

View File

@@ -71,8 +71,9 @@ public class RedundantLambdaParameterTypeInspection extends AbstractBaseJavaLoca
private static void removeTypes(PsiLambdaExpression lambdaExpression) {
if (lambdaExpression != null) {
final PsiParameter[] parameters = lambdaExpression.getParameterList().getParameters();
if (Arrays.stream(parameters).anyMatch(parameter -> parameter.hasModifierProperty(PsiModifier.FINAL) ||
AnonymousCanBeLambdaInspection.hasRuntimeAnnotations(parameter,Collections.emptySet()))) {
if (PsiUtil.isLanguageLevel11OrHigher(lambdaExpression) &&
Arrays.stream(parameters).anyMatch(parameter -> parameter.hasModifierProperty(PsiModifier.FINAL) ||
parameter.getAnnotations().length > 0)) {
for (PsiParameter parameter : parameters) {
PsiTypeElement element = parameter.getTypeElement();
if (element != null) {

View File

@@ -312,7 +312,10 @@ public class PsiTypeElementImpl extends CompositePsiElement implements PsiTypeEl
@Override
public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException {
// neighbouring type annotations are logical part of this type element and should be dropped
PsiImplUtil.markTypeAnnotations(this);
//if replacement is `var`, annotations should be left as they are not inferred from the right side of the assignment
if (!(newElement instanceof PsiTypeElement) || !((PsiTypeElement)newElement).isInferredType()) {
PsiImplUtil.markTypeAnnotations(this);
}
PsiElement result = super.replace(newElement);
if (result instanceof PsiTypeElement) {
PsiImplUtil.deleteTypeAnnotations((PsiTypeElement)result);

View File

@@ -1,6 +1,7 @@
// "Replace explicit type with 'var'" "true"
class Main {
void m(String[] args) {
for (var arg : args) ;
for (@Anno var arg : args) ;
}
}
}
@interface Anno {}

View File

@@ -1,6 +1,7 @@
// "Replace explicit type with 'var'" "true"
class Main {
{
var args = new String[42];
@Anno var args = new String[42];
}
}
}
@interface Anno {}

View File

@@ -1,6 +1,7 @@
// "Replace explicit type with 'var'" "true"
class Main {
void m(String[] args) {
for (<caret>String arg : args) ;
for (@Anno <caret>String arg : args) ;
}
}
}
@interface Anno {}

View File

@@ -1,6 +1,7 @@
// "Replace explicit type with 'var'" "true"
class Main {
{
<caret>String[] args = new String[42];
@Anno <caret>String[] args = new String[42];
}
}
}
@interface Anno {}