ForLoopThatDoesntUseLoopVariable inspection removed (superceded by LoopConditionNotUpdatedInsideLoop)

Fixes IDEA-184207
LoopConditionNotUpdatedInsideLoop is on by default now
//noinspection ForLoopThatDoesntUseLoopVariable marks removed and other trivial warnings fixed
This commit is contained in:
Tagir Valeev
2018-01-22 12:24:43 +07:00
parent b2518ed418
commit c95dca14e9
15 changed files with 13 additions and 265 deletions
@@ -186,7 +186,6 @@ public abstract class LwComponent implements IComponent{
public final LwIntrospectedProperty[] getAssignedIntrospectedProperties() {
final LwIntrospectedProperty[] properties = new LwIntrospectedProperty[myIntrospectedProperty2Value.size()];
final Iterator iterator = myIntrospectedProperty2Value.keySet().iterator();
//noinspection ForLoopThatDoesntUseLoopVariable
for (int i=0; iterator.hasNext(); i++) {
properties[i] = (LwIntrospectedProperty)iterator.next();
}
@@ -221,13 +220,12 @@ public abstract class LwComponent implements IComponent{
final HashMap name2property = provider.getLwProperties(getComponentClassName());
if (name2property == null) {
myErrorComponentProperties = (Element)propertiesElement.clone();
myErrorComponentProperties = propertiesElement.clone();
return;
}
final List propertyElements = propertiesElement.getChildren();
for (int i = 0; i < propertyElements.size(); i++) {
final Element t = (Element)propertyElements.get(i);
final List<Element> propertyElements = propertiesElement.getChildren();
for (Element t : propertyElements) {
final String name = t.getName();
final LwIntrospectedProperty property = (LwIntrospectedProperty)name2property.get(name);
if (property == null){
@@ -248,9 +246,8 @@ public abstract class LwComponent implements IComponent{
private void readClientProperties(final Element element) {
Element propertiesElement = LwXmlReader.getChild(element, UIFormXmlConstants.ELEMENT_CLIENT_PROPERTIES);
if (propertiesElement == null) return;
final List clientPropertyList = propertiesElement.getChildren();
for(int i=0; i<clientPropertyList.size(); i++) {
final Element prop = (Element) clientPropertyList.get(i);
final List<Element> clientPropertyList = propertiesElement.getChildren();
for (Element prop : clientPropertyList) {
final String propName = prop.getName();
final String className = LwXmlReader.getRequiredString(prop, UIFormXmlConstants.ATTRIBUTE_CLASS);
@@ -171,13 +171,12 @@ public class AnonymousToInnerHandler implements RefactoringActionHandler {
myTargetClass.add(aClass);
PsiNewExpression newExpr = (PsiNewExpression) myAnonClass.getParent();
@NonNls StringBuffer buf = new StringBuffer();
@NonNls StringBuilder buf = new StringBuilder();
buf.append("new ");
buf.append(aClass.getName());
if (!myTypeParametersToCreate.isEmpty()) {
buf.append("<");
int idx = 0;
//noinspection ForLoopThatDoesntUseLoopVariable
for (Iterator<PsiTypeParameter> it = myTypeParametersToCreate.iterator(); it.hasNext(); idx++) {
if (idx > 0) buf.append(", ");
String typeParamName = it.next().getName();
@@ -401,7 +400,7 @@ public class AnonymousToInnerHandler implements RefactoringActionHandler {
}
}
Collections.sort(toAdd, (e1, e2) -> e1.getTextRange().getStartOffset() - e2.getTextRange().getStartOffset());
toAdd.sort(Comparator.comparingInt(e -> e.getTextRange().getStartOffset()));
for (PsiElement element : toAdd) {
if (element instanceof PsiClassInitializer) {
@@ -3497,7 +3497,6 @@ public final class EditorImpl extends UserDataHolderBase implements EditorEx, Hi
// committed text insertion
if (commitCount > 0) {
//noinspection ForLoopThatDoesntUseLoopVariable
for (char c = text.current(); commitCount > 0; c = text.next(), commitCount--) {
if (c >= 0x20 && c != 0x7F) { // Hack just like in javax.swing.text.DefaultEditorKit.DefaultKeyTypedAction
processKeyTyped(c);
@@ -24,12 +24,7 @@ import com.intellij.openapi.util.Condition;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
@@ -65,7 +60,6 @@ final class KeymapSchemeManager extends AbstractSchemeActions<KeymapScheme> impl
if (scheme.isMutable()) return scheme.getMutable();
String name = message("new.keymap.name", keymap.getPresentableName());
//noinspection ForLoopThatDoesntUseLoopVariable
for (int i = 1; containsScheme(name, false); i++) {
name = message("new.indexed.keymap.name", keymap.getPresentableName(), i);
}
@@ -21,7 +21,6 @@ import io.netty.channel.ChannelHandlerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
@@ -37,7 +36,7 @@ public abstract class MessageDecoder extends Decoder {
}
@Nullable
protected final CharSequence readChars(@NotNull ByteBuf input) throws IOException {
protected final CharSequence readChars(@NotNull ByteBuf input) {
int readableBytes = input.readableBytes();
if (readableBytes == 0) {
input.release();
@@ -85,7 +84,6 @@ public abstract class MessageDecoder extends Decoder {
public static boolean readUntil(char what, @NotNull ByteBuf buffer, @NotNull StringBuilder builder) {
int i = buffer.readerIndex();
//noinspection ForLoopThatDoesntUseLoopVariable
for (int n = buffer.writerIndex(); i < n; i++) {
char c = (char)buffer.getByte(i);
if (c == what) {
@@ -79,10 +79,6 @@ non.final.field.in.equals.display.name=Non-final field referenced in 'equals()'
non.final.field.in.equals.problem.descriptor=Non-final field <code>#ref</code> accessed in 'equals()' #loc
equals.doesnt.check.class.parameter.display.name='equals()' method which does not check class of parameter
equals.doesnt.check.class.parameter.problem.descriptor=<code>#ref()</code> should check the class of its parameter #loc
for.loop.not.use.loop.variable.display.name='for' loop where update or condition does not use loop variable
for.loop.not.use.loop.variable.problem.descriptor.condition=<code>#ref</code> statement has condition which does not use the for loop variable #loc
for.loop.not.use.loop.variable.problem.descriptor.update=<code>#ref</code> statement has update which does not use the for loop variable #loc
for.loop.not.use.loop.variable.problem.descriptor.both.condition.and.update=<code>#ref</code> statement has condition and update which do not use the for loop variable #loc
non.final.field.in.hashcode.display.name=Non-final field referenced in 'hashCode()'
non.final.field.in.hashcode.problem.descriptor=Non-final field <code>#ref</code> accessed in 'hashCode()' #loc
result.of.method.call.ignored.display.name=Result of method call ignored
@@ -1,113 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* 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.siyeh.ig.bugs;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ComparisonUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.VariableAccessUtils;
import org.jetbrains.annotations.NotNull;
import static com.intellij.util.ObjectUtils.tryCast;
public class ForLoopThatDoesntUseLoopVariableInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message("for.loop.not.use.loop.variable.display.name");
}
@Override
@NotNull
public String buildErrorString(Object... infos) {
final boolean condition = ((Boolean)infos[0]).booleanValue();
final boolean update = ((Boolean)infos[1]).booleanValue();
if (condition && update) {
return InspectionGadgetsBundle.message(
"for.loop.not.use.loop.variable.problem.descriptor.both.condition.and.update");
}
if (condition) {
return InspectionGadgetsBundle.message(
"for.loop.not.use.loop.variable.problem.descriptor.condition");
}
return InspectionGadgetsBundle.message(
"for.loop.not.use.loop.variable.problem.descriptor.update");
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new ForLoopThatDoesntUseLoopVariableVisitor();
}
private static class ForLoopThatDoesntUseLoopVariableVisitor extends BaseInspectionVisitor {
@Override
public void visitForStatement(@NotNull PsiForStatement statement) {
super.visitForStatement(statement);
PsiLocalVariable variable = extractInitializerVariable(statement);
if (variable == null) return;
boolean notUsedInCondition = !conditionUsesVariable(statement, variable);
boolean notUsedInUpdate = !updateUsesVariable(statement, variable);
if (notUsedInCondition || notUsedInUpdate) {
if (!notUsedInCondition && isDeclarationUsedAsBound(statement, variable)) return;
registerStatementError(statement, notUsedInCondition, notUsedInUpdate);
}
}
private static boolean isDeclarationUsedAsBound(PsiForStatement statement, PsiLocalVariable boundVar) {
PsiBinaryExpression condition = tryCast(PsiUtil.skipParenthesizedExprDown(statement.getCondition()), PsiBinaryExpression.class);
if (condition == null || !ComparisonUtils.isComparisonOperation(condition.getOperationTokenType())) return false;
PsiExpression otherOperand = null;
if (ExpressionUtils.isReferenceTo(condition.getLOperand(), boundVar)) {
otherOperand = condition.getROperand();
} else if (ExpressionUtils.isReferenceTo(condition.getROperand(), boundVar)) {
otherOperand = condition.getLOperand();
}
if (otherOperand == null) return false;
PsiReferenceExpression ref = tryCast(PsiUtil.skipParenthesizedExprDown(otherOperand), PsiReferenceExpression.class);
if (ref == null) return false;
PsiVariable indexVar = tryCast(ref.resolve(), PsiVariable.class);
if (indexVar == null) return false;
PsiStatement update = statement.getUpdate();
return VariableAccessUtils.variableIsIncremented(indexVar, update) || VariableAccessUtils.variableIsDecremented(indexVar, update);
}
private static PsiLocalVariable extractInitializerVariable(PsiForStatement statement) {
final PsiDeclarationStatement declaration = tryCast(statement.getInitialization(), PsiDeclarationStatement.class);
if (declaration == null) return null;
final PsiElement[] declaredElements = declaration.getDeclaredElements();
if (declaredElements.length != 1) return null;
return tryCast(declaredElements[0], PsiLocalVariable.class);
}
private static boolean conditionUsesVariable(PsiForStatement statement, PsiLocalVariable variable) {
final PsiExpression condition = statement.getCondition();
return condition == null || VariableAccessUtils.variableIsUsed(variable, condition);
}
private static boolean updateUsesVariable(PsiForStatement statement, PsiLocalVariable variable) {
final PsiStatement update = statement.getUpdate();
return update == null || VariableAccessUtils.variableIsUsed(variable, update);
}
}
}
@@ -238,10 +238,6 @@
key="equals.doesnt.check.class.parameter.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.probable.bugs" enabledByDefault="true" level="WARNING"
implementationClass="com.siyeh.ig.bugs.EqualsWhichDoesntCheckParameterClassInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="ForLoopThatDoesntUseLoopVariable" bundle="com.siyeh.InspectionGadgetsBundle"
key="for.loop.not.use.loop.variable.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.probable.bugs" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.bugs.ForLoopThatDoesntUseLoopVariableInspection"/>
<localInspection groupPath="Java" language="JAVA" suppressId="NonFinalFieldReferencedInHashCode" shortName="HashCodeUsesNonFinalVariable"
bundle="com.siyeh.InspectionGadgetsBundle" key="non.final.field.in.hashcode.display.name"
groupBundle="messages.InspectionsBundle" groupKey="group.names.probable.bugs" enabledByDefault="false" level="WARNING"
@@ -681,7 +677,7 @@
level="WARNING" implementationClass="com.siyeh.ig.controlflow.LabeledStatementInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="LoopConditionNotUpdatedInsideLoop" bundle="com.siyeh.InspectionGadgetsBundle"
key="loop.condition.not.updated.inside.loop.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.control.flow.issues" enabledByDefault="false" level="WARNING"
groupKey="group.names.control.flow.issues" enabledByDefault="true" level="WARNING"
implementationClass="com.siyeh.ig.controlflow.LoopConditionNotUpdatedInsideLoopInspection"/>
<localInspection groupPath="Java" language="JAVA" suppressId="LoopStatementThatDoesntLoop" shortName="LoopStatementsThatDontLoop"
bundle="com.siyeh.InspectionGadgetsBundle" key="loop.statements.that.dont.loop.display.name"
@@ -1,9 +0,0 @@
<html>
<body>
Reports <b>for</b> loops where the condition or
update does not use the <b>for</b> loop variable.
<!-- tooltip end -->
<p>
</body>
</html>
@@ -1,55 +0,0 @@
/*
* Copyright 2000-2017 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.
*/
import java.util.List;
public class ForLoopThatDoesntUseLoopVariable {
boolean test(int i) {
return i < 10;
}
void bug() {
for (int i = 0; test(i); i++) {
<warning descr="'for' statement has update which does not use the for loop variable">for</warning> (int j = 0; test(j); i++) {
System.out.println(i + ":" + j);
}
}
}
void bug2() {
for (int i = 0; test(i); i++) {
<warning descr="'for' statement has condition which does not use the for loop variable">for</warning> (int j = 0; test(i); j++) {
System.out.println(i + ":" + j);
}
}
}
// IDEA-166869
void test(List<String> lines) {
int i = 0;
for (int size = lines.size(); i < size; i++) {
if (lines.get(i).isEmpty()) break;
}
System.out.println(i);
}
void test2(List<String> lines) {
int i = 0, j = 0;
<warning descr="'for' statement has update which does not use the for loop variable">for</warning> (int size = lines.size(); i < size; j++) {
if (lines.get(i).isEmpty()) break;
}
System.out.println(j);
}
}
@@ -1,44 +0,0 @@
/*
* Copyright 2000-2017 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.siyeh.ig.bugs;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.intellij.testFramework.LightProjectDescriptor;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.NotNull;
public class ForLoopThatDoesntUseLoopVariableInspectionTest extends LightInspectionTestCase {
public void testForLoopThatDoesntUseLoopVariable() {
doTest();
}
@Override
protected InspectionProfileEntry getInspection() {
return new ForLoopThatDoesntUseLoopVariableInspection();
}
@Override
protected String getBasePath() {
return "/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/for_does_not_use_var";
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_8;
}
}
@@ -172,7 +172,6 @@ public class MethodParameterInjection extends BaseInjection {
public static String fixSignature(final String signature, final boolean parameterNames) {
@NonNls final StringBuilder sb = new StringBuilder();
final StringTokenizer st = new StringTokenizer(signature, "(,)");
//noinspection ForLoopThatDoesntUseLoopVariable
for (int i = 0; st.hasMoreTokens(); i++) {
final String token = st.nextToken().trim();
if (i > 1) sb.append(", ");
@@ -215,7 +214,6 @@ public class MethodParameterInjection extends BaseInjection {
if (type instanceof PsiPrimitiveType) return false;
if (project.isDefault()) {
@NonNls final String text = type.getPresentableText();
if (text == null) return false;
return text.equals("java.lang.String") || text.equals("java.lang.String...") || text.equals("java.lang.String[]");
}
else {
@@ -239,7 +237,6 @@ public class MethodParameterInjection extends BaseInjection {
public static String getParameterTypesString(final String signature) {
@NonNls final StringBuilder sb = new StringBuilder();
final StringTokenizer st = new StringTokenizer(signature, "(,)");
//noinspection ForLoopThatDoesntUseLoopVariable
for (int i = 0; st.hasMoreTokens(); i++) {
final String token = st.nextToken().trim();
if (i > 1) sb.append(", ");
@@ -236,7 +236,6 @@ public class OutputParser{
final int columnNum) {
StringTokenizer tokenizer = new StringTokenizer(message, "\n", false);
final String[] strings = new String[tokenizer.countTokens()];
//noinspection ForLoopThatDoesntUseLoopVariable
for (int idx = 0; tokenizer.hasMoreTokens(); idx++) {
strings[idx] = tokenizer.nextToken();
}
@@ -171,7 +171,6 @@ public final class InsertComponentProcessor extends EventProcessor {
public static String getUniqueBinding(RadRootContainer root, final String baseName) {
// Generate member name based on current code style
//noinspection ForLoopThatDoesntUseLoopVariable
for (int i = 0; true; i++) {
final String nameCandidate = baseName + (i + 1);
final String binding = JavaCodeStyleManager.getInstance(root.getProject()).propertyNameToVariableName(
@@ -27,7 +27,6 @@ import com.intellij.openapi.fileTypes.impl.FileTypeRenderer;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdkType;
@@ -362,16 +361,14 @@ class XsltRunSettingsEditor extends SettingsEditor<XsltRunConfiguration> {
if (fileType instanceof LanguageFileType) {
final SyntaxHighlighter sh =
SyntaxHighlighterFactory.getSyntaxHighlighter(((LanguageFileType)fileType).getLanguage(), project, null);
if (sh != null) {
v.add(fileType);
}
v.add(fileType);
}
}
catch (Throwable e) {
Logger.getInstance(XsltRunSettingsEditor.class.getName()).info("Encountered incompatible FileType: " + fileType.getName(), e);
}
}
Collections.sort(v, (o1, o2) -> o1.getDescription().compareTo(o2.getDescription()));
Collections.sort(v, Comparator.comparing(FileType::getDescription));
// off
v.insertElementAt(null, 0);
@@ -471,7 +468,6 @@ class XsltRunSettingsEditor extends SettingsEditor<XsltRunConfiguration> {
private static void setSelectedIndex(ButtonGroup group, int i) {
final Enumeration<AbstractButton> buttons = group.getElements();
//noinspection ForLoopThatDoesntUseLoopVariable
for (int j = 0; buttons.hasMoreElements(); j++) {
group.setSelected(buttons.nextElement().getModel(), i == j);
}
@@ -481,7 +477,6 @@ class XsltRunSettingsEditor extends SettingsEditor<XsltRunConfiguration> {
final ButtonModel selection = group.getSelection();
if (selection == null) return -1;
final Enumeration<AbstractButton> buttons = group.getElements();
//noinspection ForLoopThatDoesntUseLoopVariable
for (int i = 0; buttons.hasMoreElements(); i++) {
final AbstractButton button = buttons.nextElement();
if (group.isSelected(button.getModel())) return i;
@@ -598,7 +593,7 @@ class XsltRunSettingsEditor extends SettingsEditor<XsltRunConfiguration> {
myEditor.resetFrom(s);
}
protected void applyEditorTo(@NotNull XsltRunConfiguration s) throws ConfigurationException {
protected void applyEditorTo(@NotNull XsltRunConfiguration s) {
myEditor.applyTo(s);
}