MismatchedArrayReadWrite: do not consider compound assignments/increments as reads (IDEA-CR-41955)

This commit is contained in:
Tagir Valeev
2019-01-14 10:04:23 +07:00
parent 8b9b61b804
commit 815dda57fb
2 changed files with 10 additions and 2 deletions
@@ -256,8 +256,9 @@ public class MismatchedArrayReadWriteInspection extends BaseInspection {
}
if (PsiUtil.isAccessedForWriting(arrayAccessExpression)) {
myWritten = true;
// compound writes like += are not considered as reads, because result result goes to the same array as well
}
if (PsiUtil.isAccessedForReading(arrayAccessExpression) && !goesToSameArray(arrayAccessExpression)) {
else if (PsiUtil.isAccessedForReading(arrayAccessExpression) && !goesToSameArray(arrayAccessExpression)) {
myRead = true;
}
}
@@ -109,9 +109,10 @@ public class MismatchedArrayReadWrite {
}
void reuseSelf() {
int[] arr = new int[10];
int[] <warning descr="Contents of array 'arr' are written to, but never read">arr</warning> = new int[10];
for(int i=0; i<arr.length; i++) arr[i] = i;
for(int i=1; i<arr.length; i++) arr[i] += arr[i-1];
for(int i=1; i<arr.length; i++) arr[i] = arr[i-1] + 1;
}
void objectType(Object x) {
@@ -135,8 +136,14 @@ class Test{
}
private void testStuff() {
int[][] <warning descr="Contents of array 'array' are written to, but never read">array</warning> = new int[2][2];
array[0][1]++;
}
private void testStuff2() { // IDEA-47651
int[][] array = new int[2][2];
array[0][1]++;
System.out.println(array[0][1]);
}
void foo1() {