From 383d5a0aab704ec6e6e856066de1c16a2bbead4e Mon Sep 17 00:00:00 2001 From: Nikita Eshkeev Date: Wed, 28 Jul 2021 21:13:22 +0300 Subject: [PATCH] [java][highlight] IDEA-274531 errors shown incorrectly when a method name is the same as a nested class name IDEA added a fix to remove the "new" keyword when a callsite looks like a call to a method or a field. It caused the mentioned regression, because sometimes the callsite might simply instantiate a class that has the same name as a method which was mistakenly highlighted as an error. This patch simply adds a check if the resolved element is of a PsiClass and if so then the mentioned highlighting doesn't get added to such callsites. GitOrigin-RevId: a34e6e1715d93ac21a2ba93cf5f57c54436bc11b --- .../codeInsight/daemon/impl/analysis/HighlightUtil.java | 8 +++++++- .../highlight/remove_new/beforeClassMethodSameName.java | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/highlight/remove_new/beforeClassMethodSameName.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java index 31bce6828de3..9b8a3e908bf1 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java @@ -2958,7 +2958,7 @@ public final class HighlightUtil { PsiElement refParent = ref.getParent(); - if (isCallToStaticMember(refParent)) { + if (!(resolved instanceof PsiClass) && isCallToStaticMember(refParent)) { final String text = JavaErrorBundle.message("cannot.resolve.symbol", refName.getText()); final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(text).create(); QuickFixAction.registerQuickFixAction(info, new RemoveNewKeywordFix(refParent)); @@ -3000,6 +3000,12 @@ public final class HighlightUtil { } else { description = JavaErrorBundle.message("cannot.resolve.symbol", refName.getText()); + if (isCallToStaticMember(refParent)) { + final String text = JavaErrorBundle.message("cannot.resolve.symbol", refName.getText()); + final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(text).create(); + QuickFixAction.registerQuickFixAction(info, new RemoveNewKeywordFix(refParent)); + return info; + } } HighlightInfo info = diff --git a/java/java-tests/testData/codeInsight/highlight/remove_new/beforeClassMethodSameName.java b/java/java-tests/testData/codeInsight/highlight/remove_new/beforeClassMethodSameName.java new file mode 100644 index 000000000000..aa22aeb7cef5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/highlight/remove_new/beforeClassMethodSameName.java @@ -0,0 +1,8 @@ +// "Remove 'new'" "false" + +class A { + class B {} + B B() { + return new A.B(); + } +}