diff --git a/plugins/groovy/resources/inspectionDescriptions/GroovyResultOfArrayAllocationIgnored.html b/plugins/groovy/resources/inspectionDescriptions/GroovyResultOfArrayAllocationIgnored.html deleted file mode 100644 index e7c96f8a8870..000000000000 --- a/plugins/groovy/resources/inspectionDescriptions/GroovyResultOfArrayAllocationIgnored.html +++ /dev/null @@ -1,8 +0,0 @@ - -
| - -This inspection reports any instances of Groovy array allocation where the array allocated ignored. -Such allocation expressions are legal Groovy, but are usually either inadvertant, or -evidence of a very odd object initialization strategy. - |
| Powered by InspectorGroovy |
#ref is ignored #loc";
- } else {
- return "Result of new #ref[] is ignored #loc";
- }
- }
-
- public boolean isEnabledByDefault() {
- return true;
- }
-
- public BaseInspectionVisitor buildVisitor() {
- return new Visitor();
- }
-
- private static class Visitor extends BaseInspectionVisitor {
-
- public void visitNewExpression(GrNewExpression newExpression) {
- super.visitNewExpression(newExpression);
- final PsiElement parent = newExpression.getParent();
- if (!(parent instanceof GrCodeBlock)) {
- return;
- }
- if (newExpression.getArrayCount() == 0) {
- return;
- }
- if (parent instanceof GrOpenBlock) {
- final GrOpenBlock openBlock = (GrOpenBlock) parent;
- if (ControlFlowUtils.openBlockCompletesWithStatement(openBlock, newExpression)) {
- return;
- }
- }
-
- final GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
- if (referenceElement != null) {
- registerError(referenceElement, Boolean.FALSE);
- } else {
- registerError(newExpression, Boolean.TRUE);
- }
- }
- }
-}
\ No newline at end of file
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/bugs/GroovyResultOfObjectAllocationIgnoredInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/bugs/GroovyResultOfObjectAllocationIgnoredInspection.java
index 1b1f50d6a96e..9f958a4d2ce3 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/bugs/GroovyResultOfObjectAllocationIgnoredInspection.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/bugs/GroovyResultOfObjectAllocationIgnoredInspection.java
@@ -22,12 +22,11 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.codeInspection.BaseInspection;
import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor;
import org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils;
+import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
-import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression;
-import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
-import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression;
public class GroovyResultOfObjectAllocationIgnoredInspection extends BaseInspection {
@@ -45,7 +44,7 @@ public class GroovyResultOfObjectAllocationIgnoredInspection extends BaseInspect
@Nullable
protected String buildErrorString(Object... args) {
- return "Result of new #ref() is ignored #loc";
+ return "Result of new #ref" + (args[0].equals(new Integer(0)) ? "()" : "[]") + " is ignored #loc";
}
@@ -62,29 +61,19 @@ public class GroovyResultOfObjectAllocationIgnoredInspection extends BaseInspect
public void visitNewExpression(GrNewExpression newExpression) {
super.visitNewExpression(newExpression);
final PsiElement parent = newExpression.getParent();
- if (!(parent instanceof GrCodeBlock)) {
+ if (parent instanceof GrClosableBlock) {
return;
}
- if (parent instanceof GrOpenBlock) {
- final GrOpenBlock openBlock = (GrOpenBlock) parent;
- if (ControlFlowUtils.openBlockCompletesWithStatement(openBlock, newExpression)) {
- return;
- }
- } else if (parent instanceof GrClosableBlock) {
- final PsiElement grandParent = parent.getParent();
- if (grandParent instanceof GrMethodCallExpression) {
- return;
- } else if (grandParent instanceof GrReferenceExpression) {
- final PsiElement greatGrandParent = grandParent.getParent();
- if (greatGrandParent instanceof GrMethodCallExpression) {
+
+ if (parent instanceof GrCodeBlock || parent instanceof GroovyFile) {
+ if (parent instanceof GrOpenBlock) {
+ final GrOpenBlock openBlock = (GrOpenBlock)parent;
+ if (ControlFlowUtils.openBlockCompletesWithStatement(openBlock, newExpression)) {
return;
}
}
+ registerError(newExpression.getReferenceElement(), newExpression.getArrayCount());
}
- if (newExpression.getArrayCount() != 0) {
- return;
- }
- registerError(newExpression.getReferenceElement());
}
}
}
\ No newline at end of file
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/noReturnMethod/MissingReturnInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/noReturnMethod/MissingReturnInspection.java
index 305c5a12cdb1..684dbfe780ac 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/noReturnMethod/MissingReturnInspection.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/noReturnMethod/MissingReturnInspection.java
@@ -99,7 +99,9 @@ public class MissingReturnInspection extends GroovySuppressableInspectionTool {
final PsiElement element = instruction.getElement();
if (element instanceof GrReturnStatement) {
sometimes.set(true);
- hasExplicitReturn.set(true);
+ if (((GrReturnStatement)element).getReturnValue() != null) {
+ hasExplicitReturn.set(true);
+ }
}
else if (element instanceof GrThrowStatement || element instanceof GrAssertStatement) {
sometimes.set(true);
diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java
index 77b588826275..8b44f59bd3ee 100644
--- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java
+++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java
@@ -18,10 +18,10 @@ import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection;
import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyUncheckedAssignmentOfMemberOfRawTypeInspection;
+import org.jetbrains.plugins.groovy.codeInspection.bugs.GroovyResultOfObjectAllocationIgnoredInspection;
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialConditionalInspection;
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialIfInspection;
import org.jetbrains.plugins.groovy.codeInspection.metrics.GroovyOverlyLongMethodInspection;
-import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection;
import org.jetbrains.plugins.groovy.codeInspection.unassignedVariable.UnassignedVariableAccessInspection;
import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GroovyUnresolvedAccessInspection;
import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GroovyUntypedAccessInspection;
@@ -34,6 +34,17 @@ import java.io.IOException;
* @author peter
*/
public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
+ public static final DefaultLightProjectDescriptor GROOVY_17_PROJECT_DESCRIPTOR = new DefaultLightProjectDescriptor() {
+ @Override
+ public void configureModule(Module module, ModifiableRootModel model, ContentEntry contentEntry) {
+ final Library.ModifiableModel modifiableModel = model.getModuleLibraryTable().createLibrary("GROOVY").getModifiableModel();
+ final VirtualFile groovyJar =
+ JarFileSystem.getInstance().refreshAndFindFileByPath(TestUtils.getMockGroovy1_7LibraryName()+"!/");
+ modifiableModel.addRoot(groovyJar, OrderRootType.CLASSES);
+ modifiableModel.commit();
+ }
+ };
+
@Override
protected String getBasePath() {
return TestUtils.getTestDataPath() + "highlighting/";
@@ -42,16 +53,7 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
- return new DefaultLightProjectDescriptor() {
- @Override
- public void configureModule(Module module, ModifiableRootModel model, ContentEntry contentEntry) {
- final Library.ModifiableModel modifiableModel = model.getModuleLibraryTable().createLibrary("GROOVY").getModifiableModel();
- final VirtualFile groovyJar =
- JarFileSystem.getInstance().refreshAndFindFileByPath(TestUtils.getMockGroovy1_7LibraryName()+"!/");
- modifiableModel.addRoot(groovyJar, OrderRootType.CLASSES);
- modifiableModel.commit();
- }
- };
+ return GROOVY_17_PROJECT_DESCRIPTOR;
}
public void testDuplicateClosurePrivateVariable() throws Throwable {
@@ -139,18 +141,9 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
public void testDefaultMapConstructorNamedArgsError() throws Throwable {doTest();}
public void testDefaultMapConstructorWhenDefConstructorExists() throws Throwable {doTest();}
- public void testUnresolvedLhsAssignment() throws Throwable { doTest(new GroovyUnresolvedAccessInspection()); }
+ public void testSingleAllocationInClosure() throws Throwable {doTest(new GroovyResultOfObjectAllocationIgnoredInspection());}
- public void testMissingReturnWithLastLoop() throws Throwable { doTest(new MissingReturnInspection()); }
- public void testMissingReturnWithUnknownCall() throws Throwable { doTest(new MissingReturnInspection()); }
- public void testMissingReturnWithIf() throws Throwable { doTest(new MissingReturnInspection()); }
- public void testMissingReturnWithAssertion() throws Throwable { doTest(new MissingReturnInspection()); }
- public void testMissingReturnThrowException() throws Throwable { doTest(new MissingReturnInspection()); }
- public void testMissingReturnTryCatch() throws Throwable { doTest(new MissingReturnInspection()); }
- public void testMissingReturnLastNull() throws Throwable { doTest(new MissingReturnInspection()); }
- public void testMissingReturnImplicitReturns() throws Throwable {doTest(new MissingReturnInspection());}
- public void testMissingReturnOvertReturnType() throws Throwable {doTest(new MissingReturnInspection());}
- public void testMissingReturnFromClosure() throws Throwable {doTest(new MissingReturnInspection());}
+ public void testUnresolvedLhsAssignment() throws Throwable { doTest(new GroovyUnresolvedAccessInspection()); }
public void testUnresolvedMethodCallWithTwoDeclarations() throws Throwable{
doTest();
diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/MissingReturnTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/MissingReturnTest.java
new file mode 100644
index 000000000000..62e41e0863e9
--- /dev/null
+++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/MissingReturnTest.java
@@ -0,0 +1,42 @@
+package org.jetbrains.plugins.groovy.lang;
+
+import com.intellij.testFramework.LightProjectDescriptor;
+import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection;
+import org.jetbrains.plugins.groovy.util.TestUtils;
+
+/**
+ * @author peter
+ */
+public class MissingReturnTest extends LightCodeInsightFixtureTestCase {
+
+ @Override
+ protected String getBasePath() {
+ return TestUtils.getTestDataPath() + "highlighting/missingReturn";
+ }
+
+ @NotNull
+ @Override
+ protected LightProjectDescriptor getProjectDescriptor() {
+ return GroovyHighlightingTest.GROOVY_17_PROJECT_DESCRIPTOR;
+ }
+
+ public void testMissingReturnWithLastLoop() throws Throwable { doTest(); }
+ public void testMissingReturnWithUnknownCall() throws Throwable { doTest(); }
+ public void testMissingReturnWithIf() throws Throwable { doTest(); }
+ public void testMissingReturnWithAssertion() throws Throwable { doTest(); }
+ public void testMissingReturnThrowException() throws Throwable { doTest(); }
+ public void testMissingReturnTryCatch() throws Throwable { doTest(); }
+ public void testMissingReturnLastNull() throws Throwable { doTest(); }
+ public void testMissingReturnImplicitReturns() throws Throwable {doTest();}
+ public void testMissingReturnOvertReturnType() throws Throwable {doTest();}
+ public void testMissingReturnFromClosure() throws Throwable {doTest();}
+ public void testReturnsWithoutValue() throws Throwable {doTest();}
+
+ private void doTest() throws Exception {
+ myFixture.enableInspections(new MissingReturnInspection());
+ myFixture.testHighlighting(true, false, false, getTestName(false) + ".groovy");
+ }
+
+}
diff --git a/plugins/groovy/testdata/highlighting/SingleAllocationInClosure.groovy b/plugins/groovy/testdata/highlighting/SingleAllocationInClosure.groovy
new file mode 100644
index 000000000000..af443dcb0267
--- /dev/null
+++ b/plugins/groovy/testdata/highlighting/SingleAllocationInClosure.groovy
@@ -0,0 +1,3 @@
+def c = { new String() }
+def d = { new String[0] }
+new