[java-highlighting] Better call type mismatch reporting

GitOrigin-RevId: 1f7240ef2cdad17d78aa0f691b1b5ce3108293b3
This commit is contained in:
Tagir Valeev
2022-08-05 15:56:14 +02:00
committed by intellij-monorepo-bot
parent a0bc3ee404
commit b41375447f
7 changed files with 68 additions and 1 deletions

View File

@@ -12,6 +12,7 @@ import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.codeInsight.intention.impl.PriorityIntentionActionWrapper;
import com.intellij.codeInsight.quickfix.UnresolvedReferenceQuickFixProvider;
import com.intellij.codeInspection.LocalQuickFixOnPsiElementAsIntentionAdapter;
import com.intellij.core.JavaPsiBundle;
import com.intellij.java.analysis.JavaAnalysisBundle;
import com.intellij.lang.jvm.JvmModifier;
import com.intellij.lang.jvm.actions.JvmElementActionFactories;
@@ -573,6 +574,7 @@ public final class HighlightMethodUtil {
@NotNull PsiElement elementToHighlight) {
String errorMessage = resolveResult.getInferenceErrorMessage();
if (errorMessage == null) return null;
if (favorParentReport(methodCall, errorMessage)) return null;
PsiMethod method = resolveResult.getElement();
HighlightInfo highlightInfo;
PsiType expectedTypeByParent = InferenceSession.getTargetTypeByParent(methodCall);
@@ -598,6 +600,25 @@ public final class HighlightMethodUtil {
return highlightInfo;
}
private static boolean favorParentReport(@NotNull PsiCall methodCall, String errorMessage) {
if (errorMessage.equals(JavaPsiBundle.message("error.incompatible.type.failed.to.resolve.argument"))) {
PsiElement parent = PsiUtil.skipParenthesizedExprUp(methodCall.getParent());
if (parent instanceof PsiExpressionList) {
PsiElement grandParent = parent.getParent();
if (grandParent instanceof PsiCallExpression) {
JavaResolveResult parentResolveResult = ((PsiCallExpression)grandParent).resolveMethodGenerics();
if (parentResolveResult instanceof MethodCandidateInfo &&
((MethodCandidateInfo)parentResolveResult).getInferenceErrorMessage() != null) {
// Parent resolve failed as well, and it's likely more informative.
// Suppress this error to allow reporting from parent
return true;
}
}
}
}
return false;
}
private static void registerUsageFixes(@NotNull PsiMethodCallExpression methodCall,
@Nullable HighlightInfo highlightInfo,
@NotNull TextRange range) {

View File

@@ -1,9 +1,15 @@
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
Map<Path, Long> fileSizes(List<File> files) {
return <error descr="Incompatible types. Found: 'java.util.Map<java.io.File,java.lang.Long>', required: 'java.util.Map<java.nio.file.Path,java.lang.Long>'">files.stream().collect(Collectors.toMap(f -> f, f -> f.length()));</error>
}
void test() {
Map<Long, List<String>> collect = <error descr="Incompatible types. Found: 'java.util.Map<java.lang.Integer,java.util.List<java.lang.String>>', required: 'java.util.Map<java.lang.Long,java.util.List<java.lang.String>>'">Stream.of("xyz", "asfdasdfdasf", "dasfafasdfdf")
.collect(Collectors.groupingBy(s -> s.length()));</error>

View File

@@ -0,0 +1,11 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamFilter {
void test() {
Map<Long, List<String>> collect = Stream.of("xyz", "asfdasdfdasf", "dasfafasdfdf")
.collect(Collectors.groupingBy(s -> s.length()));
}
}

View File

@@ -0,0 +1,13 @@
// "Adapt lambda return using 'toPath()'" "true-preview"
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Demo {
Map<Path, Long> fileSizes(List<File> files) {
return files.stream().collect(Collectors.toMap(f -> f.toPath(), f -> f.length()));
}
}

View File

@@ -0,0 +1,13 @@
// "Adapt lambda return using 'toPath()'" "true-preview"
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Demo {
Map<Path, Long> fileSizes(List<File> files) {
return files.stream().<caret>collect(Collectors.toMap(f -> f, f -> f.length()));
}
}

View File

@@ -32,7 +32,6 @@ public class LightAdvHighlightingJdk8Test extends LightDaemonAnalyzerTestCase {
public void testUncheckedWarningForPolyConditional() { enableInspectionTool(new UncheckedWarningLocalInspection()); doTest(true, true); }
public void testUncheckedWarningOnQualifierWithTypeParameterType() { enableInspectionTool(new UncheckedWarningLocalInspection()); doTest(true, true); }
public void testLambdaExpressions() { doTest(false, true); }
public void testComplexStreamTypeMismatch() { doTest(false, false);}
public void testUnsupportedFeatures() { doTest(false, false); }
public void testModulesNotSupported() { doTest(false, false); }

View File

@@ -34,6 +34,10 @@ class LightJava11HighlightingTest : LightJavaCodeInsightFixtureTestCase() {
doTest()
}
fun testComplexStreamTypeMismatch() {
doTest()
}
fun testJavaShebang() {
val file = myFixture.configureByText("hello",
"""#!/path/to/java