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 80ce625903ce..f2c5b5139498 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
@@ -1391,7 +1391,13 @@ public class HighlightUtil extends HighlightUtilBase {
@Nullable
static HighlightInfo checkNotAStatement(@NotNull PsiStatement statement) {
if (!PsiUtil.isStatement(statement) && !PsiUtilCore.hasErrorElementChild(statement)) {
- String description = JavaErrorMessages.message("not.a.statement");
+ boolean isDeclarationNotAllowed = false;
+ if (statement instanceof PsiDeclarationStatement) {
+ final PsiElement parent = statement.getParent();
+ isDeclarationNotAllowed = parent instanceof PsiIfStatement || parent instanceof PsiLoopStatement;
+ }
+
+ String description = JavaErrorMessages.message(isDeclarationNotAllowed ? "declaration.not.allowed" : "not.a.statement");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
}
return null;
diff --git a/java/java-psi-impl/src/messages/JavaErrorMessages.properties b/java/java-psi-impl/src/messages/JavaErrorMessages.properties
index d03c02b68c72..4befd0929037 100644
--- a/java/java-psi-impl/src/messages/JavaErrorMessages.properties
+++ b/java/java-psi-impl/src/messages/JavaErrorMessages.properties
@@ -401,3 +401,4 @@ feature.type.annotations=Type annotations
feature.type.receivers=Receiver parameters
feature.try.with.resources.refs=Resource references
insufficient.language.level={0} are not supported at this language level
+declaration.not.allowed=Declaration not allowed here
diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/InvalidExpressions.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/InvalidExpressions.java
index 79ab2bdd2748..abff05e7ed60 100644
--- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/InvalidExpressions.java
+++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/InvalidExpressions.java
@@ -16,9 +16,9 @@ public class a12 {
new int[]{1};
new String[]{new String()};
if (i==1)
- String s00 = "";
+ String s00 = "";
for (;;)
- String s01 = "";
+ String s01 = "";
for (1==2,i=3; i<3; !(1==2));