Java inspection: Highlight the whole return statement in "Move return to computation", removed duplicate test cases, changed inspection's level to INFO (IDEA-121153)

This commit is contained in:
Pavel Dolgov
2016-09-07 17:15:45 +03:00
parent 7883184f5a
commit 0b10403b20
38 changed files with 105 additions and 489 deletions
@@ -563,8 +563,7 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
@NotNull PsiReturnStatement returnStatement,
@NotNull PsiVariable variable) {
String name = variable.getName();
PsiElement returnElement = returnStatement.getFirstChild();
holder.registerProblem(returnElement instanceof PsiKeyword ? returnElement : returnStatement,
holder.registerProblem(returnStatement,
InspectionsBundle.message("inspection.return.separated.from.computation.descriptor", name),
new VariableFix(name));
}
@@ -592,7 +591,7 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = getNearestEnclosingStatement(descriptor.getPsiElement());
PsiElement element = descriptor.getPsiElement();
if (element instanceof PsiReturnStatement) {
doApply(((PsiReturnStatement)element));
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'raw'" "true"
import java.util.*;
class T {
List<String> f(boolean b) {
List raw = null;
if (b) {
return g();
}
return raw;
}
List<String> g() {
return Collections.singletonList("");
}
}
@@ -0,0 +1,15 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] == 0) {
return j;
}
}
}
return n;
}
}
@@ -0,0 +1,17 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
if (a[i].length == 0) {
return -i - 1;
}
for(int j = 0; j < a[i].length; j++) {
n = j;
if (a[i][j] == 0) return n;
}
}
return n;
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'raw'" "true"
import java.util.*;
class T {
List<String> f(boolean b) {
List raw = null;
if (b) {
raw = g();
}
re<caret>turn raw;
}
List<String> g() {
return Collections.singletonList("");
}
}
@@ -1,3 +1,4 @@
// "Move 'return' closer to computation of the value of 'raw'" "false"
import java.util.*;
class T {
@@ -6,7 +7,7 @@ class T {
if (b) {
raw = g();
}
return raw;
re<caret>turn raw;
}
List<String> g() {
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] == 0) {
n = j;
break myLabel;
}
}
}
re<caret>turn n;
}
}
@@ -0,0 +1,18 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
if (a[i].length == 0) {
n = -i - 1;
break myLabel;
}
for(int j = 0; j < a[i].length; j++) {
n = j;
if (a[i][j] == 0) break myLabel;
}
}
re<caret>turn n;
}
}
@@ -1,9 +1,10 @@
// "Move 'return' closer to computation of the value of 'r'" "false"
class T {
int[] f(boolean b) {
int[] r = new int[]{-1};
if (b) {
r[0] = 1;
}
return r;
re<caret>turn r;
}
}
@@ -1,7 +0,0 @@
class T {
int f(int a) {
int n = a;
assert n != 0;
return n;
}
}
@@ -1,14 +0,0 @@
class T {
int x;
int y;
int f(int a) {
int n = -1;
if (a != 0) {
n = a;
n = 31 * x + n;
n = 31 * y + n;
}
return n;
}
}
@@ -1,19 +0,0 @@
import java.io.*;
class T {
private static String getString() throws IOException {
String s;
try (BufferedReader reader = open()) {
while (true) {
s = reader.readLine();
if (s == null || s.startsWith("$")) {
break;
}
}
}
<warning descr="Return separated from computation of value of 's'">return</warning> s;
}
private static BufferedReader open() throws FileNotFoundException {
return null;
}
}
@@ -1,22 +0,0 @@
class T {
String f() {
String r = "";
do {
if (!hasNext()) break;
String s = next();
if (s != null) {
r = s;
break;
}
} while (true);
<warning descr="Return separated from computation of value of 'r'">return</warning> r;
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -1,12 +0,0 @@
class T {
int f() {
int n = -1;
for(int i=0;; i++) {
if (i % 127 == 0 && i % 129 == 0) {
n = i + 1;
break;
}
}
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,15 +0,0 @@
import java.util.*;
class T {
List<String> f(boolean b) {
List raw = null;
if (b) {
raw = g();
}
<warning descr="Return separated from computation of value of 'raw'">return</warning> raw;
}
List<String> g() {
return Collections.singletonList("");
}
}
@@ -1,12 +0,0 @@
class T {
int size;
int width;
int height;
public int hashCode() {
int result = size;
result = 31 * result + width;
result = 31 * result + height;
return result;
}
}
@@ -1,8 +0,0 @@
class T {
int f(boolean b) {
int n = 0;
if (b) System.out.println("yes");
else System.out.println("no");
return n;
}
}
@@ -1,8 +0,0 @@
class T {
int f(boolean b) {
int n = 0;
if (b) n = 1;
else n = 2;
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,8 +0,0 @@
class T {
int f(boolean b) {
int n = 0;
if (b) System.out.println("yes");
else n = 2;
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,8 +0,0 @@
class T {
int f(boolean b) {
int n = 0;
if (b) n = 1;
else System.out.println("no");
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,12 +0,0 @@
class T {
int f(boolean b) {
int n;
myLabel:
{
n = 1;
if (b) break myLabel;
n = 2;
}
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,13 +0,0 @@
class T {
int f(int[] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
if (a[0] == 0) {
n = i;
break myLabel;
}
}
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,11 +0,0 @@
class T {
int f(int[] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
n = i;
if (a[0] == 0) break myLabel;
}
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,9 +0,0 @@
class T {
int f(boolean b) {
int n = 0;
myLabel:
if (b) n = 1;
else break myLabel;
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,9 +0,0 @@
class T {
int f() {
int n;
{
n = 1;
}
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,10 +0,0 @@
class T {
int f() {
int n;
{
n = 1;
System.out.println();
}
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,11 +0,0 @@
class T {
int f(boolean a, boolean b) {
int n = -1;
if (a) {
if (b) {
n = 1;
}
}
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,10 +0,0 @@
class T {
int f(boolean a, boolean b) {
int n = -1;
if (a) {
if (b) n = 1;
else n = 2;
}
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,10 +0,0 @@
class T {
int f(boolean a, boolean b) {
int n = -1;
if (a) {
if (b) n = 1;
}
else n = 2;
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,15 +0,0 @@
import java.io.*;
class T {
private static String getString() throws IOException {
String s;
try (BufferedReader r = open()) {
s = r.readLine();
}
<warning descr="Return separated from computation of value of 's'">return</warning> s;
}
private static BufferedReader open() throws FileNotFoundException {
return null;
}
}
@@ -1,25 +0,0 @@
class T {
String f(String a) {
String r = "";
int i = 0;
do {
int j = a.indexOf(",", i);
String s = j > i ? a.substring(i, j) : a.substring(i);
if (s.startsWith("@")) {
r = s;
break;
}
i = j + 1;
}
while (i >= 0);
<warning descr="Return separated from computation of value of 'r'">return</warning> r;
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -1,12 +0,0 @@
class T {
int f(int[] a, int b) {
int n = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == b) {
n = i;
break;
}
}
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,12 +0,0 @@
class T {
String f(String[] a) {
String r = "";
for (String s : a) {
if (s != null && s.contains("@")) {
r = s + ":" + s.length();
break;
}
}
<warning descr="Return separated from computation of value of 'r'">return</warning> r;
}
}
@@ -1,7 +0,0 @@
class T {
int f(boolean b) {
int n = 0;
if (b) n = 1;
<warning descr="Return separated from computation of value of 'n'">return</warning> n;
}
}
@@ -1,21 +0,0 @@
class T {
String f() {
String r = "";
while (hasNext()) {
String s = next();
if (s != null) {
r = s;
break;
}
}
<warning descr="Return separated from computation of value of 'r'">return</warning> r;
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -1,15 +0,0 @@
class T {
long f() {
long r;
long s = System.currentTimeMillis();
long t = s;
while (true) {
t = System.currentTimeMillis();
if (t - s > 100) {
r = t;
break;
}
}
<warning descr="Return separated from computation of value of 'r'">return</warning> r;
}
}
@@ -1,148 +0,0 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.intermediaryVariable.ReturnSeparatedFromComputationInspection;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
/**
* @author Pavel.Dolgov
*/
public class ReturnSeparatedFromComputationTest extends LightCodeInsightFixtureTestCase {
@Override
protected String getBasePath() {
return JavaTestUtil.getRelativeJavaTestDataPath() + "/inspection/returnSeparatedFromComputation";
}
public void testReturnOutsideTryWithResources() {
doTest();
}
public void testBreakFromLoopInTryWithResources() {
doTest();
}
public void testSimpleIf() {
doTest();
}
public void testSimpleFor() {
doTest();
}
public void testIfElseWriteInBoth() {
doTest();
}
public void testIfElseWriteInIf() {
doTest();
}
public void testIfElseWriteInElse() {
doTest();
}
public void testIfElseNoWrite() {
doTest();
}
public void testNestedIf() {
doTest();
}
public void testNestedIfInnerElse() {
doTest();
}
public void testNestedIfOuterElse() {
doTest();
}
public void testNestedBlock() {
doTest();
}
public void testNestedBlockSideEffect() {
doTest();
}
public void testAssert() {
doTest();
}
public void testLabeledBlock() {
doTest();
}
public void testLabeledFor() {
doTest();
}
public void testLabeledFor2() {
doTest();
}
public void testLabeledIf() {
doTest();
}
public void testWhileTrue() {
doTest();
}
public void testSimpleWhile() {
doTest();
}
public void testForWithoutCondition() {
doTest();
}
public void testSimpleForeach() {
doTest();
}
public void testDoWhileTrue() {
doTest();
}
public void testSimpleDoWhile() {
doTest();
}
public void testSideEffectInIf() {
doTest();
}
public void testHashCode() {
doTest();
}
public void testGenericTypeCompatible() {
doTest();
}
public void testGenericTypeIncompatible() {
doTest();
}
private void doTest() {
myFixture.enableInspections(new ReturnSeparatedFromComputationInspection());
myFixture.testHighlighting(getTestName(false) + ".java");
}
}
+1 -1
View File
@@ -649,7 +649,7 @@
implementationClass="com.intellij.codeInspection.localCanBeFinal.LocalCanBeFinal"/>
<localInspection groupPath="Java" language="JAVA" shortName="ReturnSeparatedFromComputation" bundle="messages.InspectionsBundle"
key="inspection.return.separated.from.computation.name" groupKey="group.names.code.style.issues"
enabledByDefault="true" level="WARNING"
enabledByDefault="true" level="INFORMATION"
implementationClass="com.intellij.codeInspection.intermediaryVariable.ReturnSeparatedFromComputationInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="JavaDoc" bundle="messages.InspectionsBundle" key="inspection.javadoc.display.name"