[java-inspections] NullableStuffInspectionBase: fix record constructor support

GitOrigin-RevId: defcd306c9f5a32c027cbc2105ac3224878de452
This commit is contained in:
Tagir Valeev
2024-06-21 16:20:44 +02:00
committed by intellij-monorepo-bot
parent bf4cebe086
commit b038d3db65
3 changed files with 22 additions and 1 deletions

View File

@@ -942,7 +942,9 @@ public class NullableStuffInspectionBase extends AbstractBaseJavaLocalInspection
PsiParameter parameter) {
if (!REPORT_NULLS_PASSED_TO_NOT_NULL_PARAMETER || !holder.isOnTheFly()) return;
PsiVariable owner = Objects.requireNonNullElse(JavaPsiRecordUtil.getComponentForCanonicalConstructorParameter(parameter), parameter);
PsiVariable owner = parameter.isPhysical() ? parameter : JavaPsiRecordUtil.getComponentForCanonicalConstructorParameter(parameter);
if (owner == null) return;
PsiElement elementToHighlight = null;
NullabilityAnnotationInfo info = nullableManager.findOwnNullabilityInfo(owner);
if (info != null && !info.isInferred()) {

View File

@@ -2,6 +2,7 @@
package com.intellij.psi.util;
import com.intellij.psi.*;
import com.intellij.psi.impl.light.LightCompactConstructorParameter;
import com.intellij.psi.impl.light.LightRecordCanonicalConstructor;
import com.intellij.psi.impl.light.LightRecordField;
import com.intellij.psi.impl.source.DummyHolder;
@@ -74,6 +75,9 @@ public final class JavaPsiRecordUtil {
* @return record component that corresponds to the parameter
*/
public static @Nullable PsiRecordComponent getComponentForCanonicalConstructorParameter(@NotNull PsiParameter parameter) {
if (parameter instanceof LightCompactConstructorParameter) {
return ((LightCompactConstructorParameter)parameter).getRecordComponent();
}
PsiClass aClass = PsiTreeUtil.getParentOfType(parameter, PsiClass.class);
if (aClass == null) return null;
String parameterName = parameter.getName();

View File

@@ -22,11 +22,26 @@ class Main111 {
record MyRecord(<warning descr="Parameter annotated @NotNull should not receive 'null' as an argument">@NotNull</warning> Object o,
@NotNull Object o2) {}
record MyRecord2(<warning descr="Parameter annotated @NotNull should not receive 'null' as an argument">@NotNull</warning> Object o,
@NotNull Object o2) {
MyRecord2 {
}
}
record MyRecord3(@NotNull Object o, @NotNull Object o2) {
MyRecord3(<warning descr="Parameter annotated @NotNull should not receive 'null' as an argument">@NotNull</warning> Object o, @NotNull Object o2) {
this.o = o;
this.o2 = o2;
}
}
static void main() {
new Main111(null);
new Main111.SubClass(null);
new SubClass2(null);
new MyRecord(null, "");
new MyRecord2(null, "");
new MyRecord3(null, "");
new ParamerizedRunnable(null) {
@Override