Java: don't warn about replacing type with diamond on nested annotation (IDEA-288000)

GitOrigin-RevId: 16c913b812b81538d5587432e9bed3af5b586545
This commit is contained in:
Bas Leijdekkers
2025-01-16 14:03:55 +01:00
committed by intellij-monorepo-bot
parent e0831cbaa1
commit d85933d67d
3 changed files with 34 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection;
import com.intellij.codeInsight.intention.HighPriorityAction;
@@ -51,10 +51,8 @@ public final class ExplicitTypeCanBeDiamondInspection extends AbstractBaseJavaLo
LOG.assertTrue(classReference != null);
final PsiReferenceParameterList parameterList = classReference.getParameterList();
LOG.assertTrue(parameterList != null);
for (PsiTypeElement typeElement : parameterList.getTypeParameterElements()) {
if (typeElement.getAnnotations().length > 0) {
return;
}
if (PsiTreeUtil.findChildOfType(parameterList, PsiAnnotation.class) != null) {
return;
}
final PsiElement firstChild = parameterList.getFirstChild();
final PsiElement lastChild = parameterList.getLastChild();

View File

@@ -18,7 +18,7 @@ class XYZ {
public static <T> void genericConsumer(T item) {}
public static void main(String[] args) {
genericConsumer(new Wrapper<@Nullable<caret>s String>(getString()));
genericConsumer(new Wrapper<@Nullable<caret> String>(getString()));
// below one works great
Wrapper<@Nullable String> wrapper = new Wrapper<>(getString());
}

View File

@@ -0,0 +1,30 @@
// "Replace with <>" "false"
import java.util.List;
class XYZ {
@Target({ElementType.TYPE_USE, ElementType.METHOD})
public @interface Nullable {}
public final static class Wrapper<T> {
private final T value;
public Wrapper(T value) {
this.value = value;
}
}
public static List<@Nullable String> getStrings() {
String element = ThreadLocalRandom.current().nextBoolean() ? "hello" : null;
return Collections.singletonList(element);
}
public static <T> void genericConsumer(T item) {}
public static void main(String[] args) {
// suggests to replace the explicit type below with <>
genericConsumer(new Wrapper<L<caret>ist<@Nullable String>>(getStrings()));
// below one works great
Wrapper<List<@Nullable String>> wrapper = new Wrapper<>(getStrings());
}
}