mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-14 09:12:22 +07:00
[lombok] IDEA-255688 Get rid of LombokHighlightErrorFilter for "Variable initialized before usage Inspection"
Used in case of lombok lazy Getter GitOrigin-RevId: 37699b68fd2431efe92d05e86a3b09cb59e673b8
This commit is contained in:
committed by
intellij-monorepo-bot
parent
69eb85b4f8
commit
b2853a3590
@@ -35,6 +35,7 @@
|
||||
<extensionPoint qualifiedName="com.intellij.lang.jvm.annotationPackageSupport" interface="com.intellij.codeInsight.annoPackages.AnnotationPackageSupport" dynamic="true"/>
|
||||
<extensionPoint qualifiedName="com.intellij.lang.jvm.ignoreAnnotationParamSupport" interface="com.intellij.codeInspection.DefaultAnnotationParamInspection$IgnoreAnnotationParamSupport" dynamic="true"/>
|
||||
<extensionPoint qualifiedName="com.intellij.lang.jvm.ignoreVariableInitializerSupport" interface="com.intellij.codeInspection.defUse.DefUseInspection$IgnoreVariableInitializerSupport" dynamic="true"/>
|
||||
<extensionPoint qualifiedName="com.intellij.lang.jvm.ignoreVariableInitializedBeforeUsageSupport" interface="com.intellij.codeInsight.daemon.impl.analysis.VariableInitializedBeforeUsageSupport" dynamic="true"/>
|
||||
<extensionPoint qualifiedName="com.intellij.javaLanguageLevelPusherCustomizer" interface="com.intellij.openapi.roots.impl.JavaLanguageLevelPusherCustomizer" dynamic="true"/>
|
||||
<extensionPoint qualifiedName="com.intellij.propertyAccessorDetector" interface="com.intellij.psi.util.PropertyAccessorDetector" dynamic="true"/>
|
||||
<extensionPoint qualifiedName="com.intellij.virtualManifestProvider" interface="com.intellij.codeInsight.daemon.impl.analysis.VirtualManifestProvider" dynamic="true"/>
|
||||
|
||||
@@ -318,6 +318,10 @@ public final class HighlightControlFlowUtil {
|
||||
boolean ignoreFinality) {
|
||||
if (variable instanceof ImplicitVariable) return null;
|
||||
if (!PsiUtil.isAccessedForReading(expression)) return null;
|
||||
if (ContainerUtil.exists(VariableInitializedBeforeUsageSupport.EP_NAME.getExtensionList(),
|
||||
ext -> ext.ignoreVariableExpression(expression, variable))) {
|
||||
return null;
|
||||
}
|
||||
int startOffset = expression.getTextRange().getStartOffset();
|
||||
PsiElement topBlock;
|
||||
if (variable.hasInitializer()) {
|
||||
@@ -830,7 +834,7 @@ public final class HighlightControlFlowUtil {
|
||||
@Override
|
||||
public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
|
||||
if (expression.isReferenceTo(variable) &&
|
||||
PsiUtil.isAccessedForWriting(expression) &&
|
||||
PsiUtil.isAccessedForWriting(expression) &&
|
||||
ControlFlowUtil.isVariableAssignedInLoop(expression, variable)) {
|
||||
stopWalking();
|
||||
stopped.set(true);
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInsight.daemon.impl.analysis;
|
||||
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.psi.PsiReferenceExpression;
|
||||
import com.intellij.psi.PsiVariable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Allows skipping 'Variable might not have been initialized' highlighting for specific PsiReference
|
||||
*/
|
||||
public interface VariableInitializedBeforeUsageSupport {
|
||||
ExtensionPointName<VariableInitializedBeforeUsageSupport> EP_NAME =
|
||||
ExtensionPointName.create("com.intellij.lang.jvm.ignoreVariableInitializedBeforeUsageSupport");
|
||||
/**
|
||||
* Checks if the given expression should be ignored for inspection.
|
||||
*
|
||||
* @param psiExpression the expression to be checked for ignoring the initializer
|
||||
* @param psiVariable the variable from the expression resolving
|
||||
* @return true if the inspection should be skipped for the {@code psiExpression},
|
||||
* otherwise false
|
||||
*/
|
||||
default boolean ignoreVariableExpression(@NotNull PsiReferenceExpression psiExpression, @NotNull PsiVariable psiVariable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -150,21 +150,6 @@ public class LombokHighlightErrorFilter implements HighlightInfoFilter {
|
||||
}
|
||||
|
||||
private enum LombokHighlightFilter {
|
||||
// ERROR HANDLERS
|
||||
|
||||
//see com.intellij.java.lomboktest.LombokHighlightingTest.testGetterLazyVariableNotInitialized
|
||||
VARIABLE_MIGHT_NOT_BEEN_INITIALIZED(HighlightSeverity.ERROR, CodeInsightColors.ERRORS_ATTRIBUTES) {
|
||||
@Override
|
||||
public boolean descriptionCheck(@Nullable String description, PsiElement highlightedElement) {
|
||||
return JavaErrorBundle.message("variable.not.initialized", highlightedElement.getText()).equals(description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(@NotNull PsiElement highlightedElement) {
|
||||
return !LazyGetterHandler.isLazyGetterHandled(highlightedElement);
|
||||
}
|
||||
},
|
||||
|
||||
// WARNINGS HANDLERS
|
||||
|
||||
// field should have lazy getter and should be initialized in constructors
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package de.plushnikov.intellij.plugin.provider;
|
||||
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.VariableInitializedBeforeUsageSupport;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import de.plushnikov.intellij.plugin.LombokClassNames;
|
||||
import de.plushnikov.intellij.plugin.util.PsiAnnotationUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
/**
|
||||
* A class that implements the VariableInitializedBeforeUsageSupport interface to provide support for Lombok annotated variables.
|
||||
* It checks if a variable expression should be ignored based on Lombok annotations.
|
||||
*/
|
||||
public class LombokVariableInitializedBeforeUsageSupport implements VariableInitializedBeforeUsageSupport {
|
||||
@Override
|
||||
public boolean ignoreVariableExpression(@NotNull PsiReferenceExpression psiExpression, @NotNull PsiVariable psiVariable) {
|
||||
final PsiField field = PsiTreeUtil.getParentOfType(psiExpression, PsiField.class);
|
||||
if (field == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final PsiAnnotation getterAnnotation = field.getAnnotation(LombokClassNames.GETTER);
|
||||
return null != getterAnnotation && PsiAnnotationUtil.getBooleanAnnotationValue(getterAnnotation, "lazy", false);
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,7 @@
|
||||
<lang.jvm.annotationPackageSupport implementation="de.plushnikov.intellij.plugin.provider.LombokAnnotationSupport"/>
|
||||
<lang.jvm.ignoreAnnotationParamSupport implementation="de.plushnikov.intellij.plugin.provider.LombokDefaultAnnotationParamSupport"/>
|
||||
<lang.jvm.ignoreVariableInitializerSupport implementation="de.plushnikov.intellij.plugin.provider.LombokVariableInitializerSupport"/>
|
||||
<lang.jvm.ignoreVariableInitializedBeforeUsageSupport implementation="de.plushnikov.intellij.plugin.provider.LombokVariableInitializedBeforeUsageSupport"/>
|
||||
<implicitUsageProvider implementation="de.plushnikov.intellij.plugin.provider.LombokImplicitUsageProvider"/>
|
||||
<projectConfigurable groupId="language"
|
||||
key="plugin.settings.title" bundle="messages.LombokBundle"
|
||||
|
||||
@@ -13,8 +13,7 @@ public class GetterLazyInvocationProduceNPE {
|
||||
}
|
||||
}
|
||||
|
||||
// no warning descr="Field 'bar' may be 'final'" any more?
|
||||
private Bar bar;
|
||||
private Bar <warning descr="Field 'bar' may be 'final'">bar</warning>;
|
||||
private Car car;
|
||||
|
||||
public GetterLazyInvocationProduceNPE(Bar bar, Car car) {
|
||||
|
||||
Reference in New Issue
Block a user