[java-inspections] Java8CollectionRemoveIf: highlight only 'for'/'while' keyword instead of the whole statement

GitOrigin-RevId: b2b312efab7a974dab6a2021a788274bc6cfc1b2
This commit is contained in:
Andrey.Cherkasov
2022-01-12 15:15:06 +00:00
committed by intellij-monorepo-bot
parent eb254acc5c
commit 205cf1eb2b
9 changed files with 11 additions and 12 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection.java18api;
import com.intellij.codeInsight.daemon.QuickFixBundle;
@@ -8,7 +8,6 @@ import com.intellij.codeInspection.util.IterableTraversal;
import com.intellij.codeInspection.util.IteratorDeclaration;
import com.intellij.codeInspection.util.LambdaGenerationUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.pom.java.JavaFeature;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
@@ -75,7 +74,7 @@ public class Java8CollectionRemoveIfInspection extends AbstractBaseJavaLocalInsp
}
private void registerProblem(PsiLoopStatement statement, PsiJavaToken endToken) {
holder.registerProblem(statement, new TextRange(0, endToken.getTextOffset() - statement.getTextOffset() + 1),
holder.registerProblem(statement.getFirstChild(),
QuickFixBundle.message("java.8.collection.removeif.inspection.description"),
new ReplaceWithRemoveIfQuickFix());
}
@@ -134,7 +133,7 @@ public class Java8CollectionRemoveIfInspection extends AbstractBaseJavaLocalInsp
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getStartElement();
PsiElement element = descriptor.getStartElement().getParent();
if(!(element instanceof PsiLoopStatement)) return;
PsiLoopStatement loop = (PsiLoopStatement)element;
PsiStatement[] statements = ControlFlowUtils.unwrapBlock(loop.getBody());
@@ -4,7 +4,7 @@ import java.util.*;
public class Main {
public void removeEmpty(List<String> list) throws Exception {
// Copy to avoid CME
for(<caret>String item : new ArrayList<>(list)) {
for<caret>(String item : new ArrayList<>(list)) {
if(item.isEmpty()) list.remove(item);
}
}
@@ -3,7 +3,7 @@ import java.util.*;
public class Main {
public void removeEmpty(List<String> list) throws Exception {
for(<caret>String item : list) {
for<caret>(String item : list) {
// Presumably CopyOnWriteArrayList
if(item.isEmpty()) list.remove(item);
}
@@ -4,7 +4,7 @@ import java.util.*;
public class Main {
public void removeEmpty(List<String> list) throws Exception {
// Copy to avoid CME
for(<caret>String item : list.toArray(new String[list.size()])) {
for<caret>(String item : list.toArray(new String[list.size()])) {
if(item.isEmpty()) list.remove(item);
}
}
@@ -4,7 +4,7 @@ import java.util.List;
public class Main {
public void testIterator(List<List<String>> data, boolean b) {
for(Ite<caret>rator<List<String>> iter = (data.iterator()); iter.hasNext();) {
for<caret>(Iterator<List<String>> iter = (data.iterator()); iter.hasNext();) {
if(iter.next().isEmpty() && /* also check the flag */ b) {
iter.remove();
}
@@ -4,7 +4,7 @@ import java.util.List;
public class Main {
public void testIterator(List<List<String>> data, boolean b) {
for(Ite<caret>rator<List<String>> iter = (data.iterator()); iter.hasNext();)
for<caret>(Iterator<List<String>> iter = (data.iterator()); iter.hasNext();)
if(iter.next().isEmpty() && /* also check the flag */ b)
iter.remove();
}
@@ -4,7 +4,7 @@ import java.util.List;
public class Main {
public void testIterator(List<List<String>> data, boolean b) {
for(Ite<caret>rator<List<String>> iter = data.iterator(); iter.hasNext();) {
for<caret>(Iterator<List<String>> iter = data.iterator(); iter.hasNext();) {
if(b && iter.next().isEmpty()) {
iter.remove();
}
@@ -4,7 +4,7 @@ import java.util.List;
public class Main {
public void testIterator(List<List<String>> data, boolean b) {
for(Ite<caret>rator<List<String>> iter = data.iterator(); iter.hasNext();) {
for<caret>(Iterator<List<String>> iter = data.iterator(); iter.hasNext();) {
if(iter.next().isEmpty() && iter.next().isEmpty()) {
iter.remove();
}
@@ -4,7 +4,7 @@ import java.util.*;
public class Test {
void test(List<String> list) {
ListIterator<String> iterator = list.listIterator();
while(iterator<caret>.hasNext()) {
while<caret>(iterator.hasNext()) {
if(iterator.next().isEmpty()) {
iterator.remove();
}