Disable field assignments in record compact constructors

Last piece of IDEA-239088

GitOrigin-RevId: 23c467c6c525d7908922bc2ebd8928205368dff7
This commit is contained in:
Tagir Valeev
2020-06-22 13:26:17 +03:00
committed by intellij-monorepo-bot
parent 3e101e4fcf
commit e027f98d6a
15 changed files with 96 additions and 7 deletions
@@ -240,12 +240,14 @@ public class HighlightControlFlowUtil {
if (identifier == null) return null;
PsiMethod canonicalConstructor = JavaPsiRecordUtil.findCanonicalConstructor(aClass);
if (canonicalConstructor == null || canonicalConstructor instanceof LightRecordCanonicalConstructor) return null;
boolean isCompact = JavaPsiRecordUtil.isCompactConstructor(canonicalConstructor);
if (isCompact && PsiUtil.getLanguageLevel(aClass) != LanguageLevel.JDK_14_PREVIEW) return null;
PsiCodeBlock body = canonicalConstructor.getBody();
if (body == null) return null;
PsiField field = JavaPsiRecordUtil.getFieldForComponent(component);
if (field == null) return null;
if (variableDefinitelyAssignedIn(field, body)) return null;
if (JavaPsiRecordUtil.isCompactConstructor(canonicalConstructor) && variableDefinitelyNotAssignedIn(field, body)) return null;
if (isCompact && variableDefinitelyNotAssignedIn(field, body)) return null;
String description = JavaErrorBundle.message("record.component.not.initialized", field.getName());
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(description).create();
}
@@ -646,7 +648,11 @@ public class HighlightControlFlowUtil {
PsiField field = (PsiField)variable;
if (innerClass != null && !containingFile.getManager().areElementsEquivalent(innerClass, field.getContainingClass())) return false;
final PsiMember enclosingCtrOrInitializer = PsiUtil.findEnclosingConstructorOrInitializer(expression);
return enclosingCtrOrInitializer != null && isSameField(enclosingCtrOrInitializer, field, reference, containingFile);
return enclosingCtrOrInitializer != null &&
!(enclosingCtrOrInitializer instanceof PsiMethod &&
JavaPsiRecordUtil.isCompactConstructor((PsiMethod)enclosingCtrOrInitializer) &&
PsiUtil.getLanguageLevel(enclosingCtrOrInitializer) != LanguageLevel.JDK_14_PREVIEW) &&
isSameField(enclosingCtrOrInitializer, field, reference, containingFile);
}
if (variable instanceof PsiLocalVariable) {
boolean isAccessedFromOtherClass = innerClass != null;
@@ -7,6 +7,7 @@ import com.intellij.codeInsight.daemon.impl.analysis.HighlightingFeature;
import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.project.Project;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.util.JavaPsiRecordUtil;
import com.intellij.psi.util.PsiTreeUtil;
@@ -65,6 +66,7 @@ public class RedundantRecordConstructorInspection extends AbstractBaseJavaLocalI
return;
}
if (PsiUtil.findReturnStatements(body).length > 0) return;
if (PsiUtil.getLanguageLevel(ctor) != LanguageLevel.JDK_14_PREVIEW && assignedCount != components.length) return;
holder.registerProblem(ctor.getParameterList(), JavaBundle.message("inspection.redundant.record.constructor.can.be.compact.message"),
ProblemHighlightType.LIKE_UNUSED_SYMBOL, new ConvertToCompactConstructorFix());
}
@@ -0,0 +1,8 @@
record WrittenFields(int x,
int y,
int z) {
public WrittenFields {
<error descr="Cannot assign a value to final variable 'x'">this.x</error> = 0;
if (Math.random() > 0.5) <error descr="Cannot assign a value to final variable 'y'">this.y</error> = 1;
}
}
@@ -1,4 +1,4 @@
// "Convert canonical constructor to compact form" "true"
// "Convert canonical constructor to compact form" "false"
record Rec(int x, int y) {
public Rec(int x<caret>, int y) {
this.x = y;
@@ -1,4 +1,4 @@
// "Convert canonical constructor to compact form" "true"
// "Convert canonical constructor to compact form" "false"
record Rec(int x, int y) {
public Rec(int x<caret>, int y) {
this.x = y;
@@ -1,4 +1,4 @@
// "Convert canonical constructor to compact form" "true"
// "Convert canonical constructor to compact form" "false"
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@@ -0,0 +1,9 @@
// "Convert canonical constructor to compact form" "true"
record Rec(int x, int y) {
public Rec(int x<caret>, int y) {
this.x = y;
// 1
this.y /*2*/= y; //3
//4
}
}
@@ -0,0 +1,7 @@
// "Convert canonical constructor to compact form" "true"
record Rec(int x, int y) {
public Rec(int x<caret>, int y) {
this.x = y;
this.y = x;
}
}
@@ -0,0 +1,13 @@
// "Convert canonical constructor to compact form" "true"
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
record Rec(@Anno int x, int y) {
public Rec(int x<caret>, int y) {
this.x = y;
this.y = y;
}
}
@Target(ElementType.FIELD)
@interface Anno {}
@@ -38,6 +38,9 @@ public class LightRecordsHighlightingTest extends LightJavaCodeInsightFixtureTes
public void testRecordCompactConstructors() {
doTest();
}
public void testRecordCompactConstructorsJava15() {
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_15_PREVIEW, this::doTest);
}
public void testLocalRecords() {
doTest();
}
@@ -0,0 +1,42 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.java.codeInspection;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.RedundantRecordConstructorInspection;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
// Remove this test when support of 14-preview is dropped
public class RedundantRecordConstructorInspection14Test extends LightQuickFixParameterizedTestCase {
@Override
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
return new LocalInspectionTool[]{new RedundantRecordConstructorInspection()};
}
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return LightJavaCodeInsightFixtureTestCase.JAVA_14;
}
@Override
protected String getBasePath() {
return "/inspection/redundantRecordConstructor/java14";
}
}
@@ -18,7 +18,6 @@ package com.intellij.java.codeInspection;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.RedundantRecordConstructorInspection;
import com.intellij.codeInspection.RedundantStreamOptionalCallInspection;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
@@ -32,7 +31,7 @@ public class RedundantRecordConstructorInspectionTest extends LightQuickFixParam
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return LightJavaCodeInsightFixtureTestCase.JAVA_14;
return LightJavaCodeInsightFixtureTestCase.JAVA_15;
}
@Override