Skip to content

Commit b4d4339

Browse files
committed
Cleanup string index and byte type mismatch inspection
- adjust description - rename inspection and quickfix - reuse ContainerUtil.findInstance and remove getFirstElementOfType - move isSingleCharLiteral to GoPsiImplUtil - simplify text retrieving in quickfix - check whether element is valid in quickfix - rename test method
1 parent a99228e commit b4d4339

File tree

8 files changed

+79
-92
lines changed

8 files changed

+79
-92
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@
368368
implementationClass="com.goide.inspections.GoMixedNamedUnnamedParametersInspection"/>
369369
<localInspection language="go" displayName="Mismatched types: byte and string" groupPath="Go"
370370
groupName="General" enabledByDefault="true" level="ERROR"
371-
implementationClass="com.goide.inspections.GoStringIndexIsByteInspection"/>
371+
implementationClass="com.goide.inspections.GoStringAndByteTypeMismatchInspection"/>
372372
<!-- /general -->
373373

374374
<!-- color schemes -->

resources/inspectionDescriptions/GoStringIndexIsByte.html renamed to resources/inspectionDescriptions/GoStringAndByteTypeMismatch.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616

1717
<html>
1818
<body>
19-
Checks comparison of string index with a single-byte string instead of byte
19+
Reports comparisons of string index with a single-byte string instead of byte.
2020
</body>
2121
</html>

src/com/goide/inspections/GoStringIndexIsByteInspection.java renamed to src/com/goide/inspections/GoStringAndByteTypeMismatchInspection.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,23 @@
1818

1919
import com.goide.GoTypes;
2020
import com.goide.psi.*;
21+
import com.goide.psi.impl.GoPsiImplUtil;
2122
import com.goide.psi.impl.GoTypeUtil;
22-
import com.goide.quickfix.GoStringIndexIsByteQuickFix;
23+
import com.goide.quickfix.GoConvertStringToByteQuickFix;
2324
import com.intellij.codeInspection.LocalInspectionToolSession;
2425
import com.intellij.codeInspection.LocalQuickFix;
2526
import com.intellij.codeInspection.ProblemHighlightType;
2627
import com.intellij.codeInspection.ProblemsHolder;
2728
import com.intellij.openapi.util.Trinity;
2829
import com.intellij.psi.tree.TokenSet;
30+
import com.intellij.util.containers.ContainerUtil;
2931
import org.jetbrains.annotations.NotNull;
3032

31-
import static com.goide.psi.impl.GoPsiImplUtil.getFirstElementOfType;
32-
33-
public class GoStringIndexIsByteInspection extends GoInspectionBase {
33+
import java.util.Arrays;
3434

35+
public class GoStringAndByteTypeMismatchInspection extends GoInspectionBase {
3536
private static final String TEXT_HINT = "Mismatched types: byte and string";
36-
private static final GoStringIndexIsByteQuickFix STRING_INDEX_IS_BYTE_QUICK_FIX = new GoStringIndexIsByteQuickFix();
37+
private static final GoConvertStringToByteQuickFix STRING_INDEX_IS_BYTE_QUICK_FIX = new GoConvertStringToByteQuickFix();
3738

3839
@NotNull
3940
@Override
@@ -45,16 +46,16 @@ public void visitConditionalExpr(@NotNull GoConditionalExpr o) {
4546
GoExpression left = o.getLeft();
4647
GoExpression right = o.getRight();
4748

48-
GoIndexOrSliceExpr indexExpr = getFirstElementOfType(GoIndexOrSliceExpr.class, left, right);
49-
GoStringLiteral stringLiteral = getFirstElementOfType(GoStringLiteral.class, left, right);
49+
GoIndexOrSliceExpr indexExpr = ContainerUtil.findInstance(Arrays.asList(left, right), GoIndexOrSliceExpr.class);
50+
GoStringLiteral stringLiteral = ContainerUtil.findInstance(Arrays.asList(left, right), GoStringLiteral.class);
5051

5152
if (indexExpr == null || stringLiteral == null) {
5253
return;
5354
}
5455

5556
if (isStringIndexExpression(indexExpr)) {
56-
LocalQuickFix[] fixes =
57-
isSingleCharLiteral(stringLiteral) ? new LocalQuickFix[]{STRING_INDEX_IS_BYTE_QUICK_FIX} : LocalQuickFix.EMPTY_ARRAY;
57+
LocalQuickFix[] fixes = GoPsiImplUtil.isSingleCharLiteral(stringLiteral) ? new LocalQuickFix[]{STRING_INDEX_IS_BYTE_QUICK_FIX}
58+
: LocalQuickFix.EMPTY_ARRAY;
5859
holder.registerProblem(o, TEXT_HINT, ProblemHighlightType.GENERIC_ERROR, fixes);
5960
}
6061
}
@@ -63,9 +64,7 @@ public void visitConditionalExpr(@NotNull GoConditionalExpr o) {
6364

6465
private static boolean isStringIndexExpression(@NotNull GoIndexOrSliceExpr expr) {
6566
GoExpression expression = expr.getExpression();
66-
GoType type = expression != null ? expression.getGoType(null) : null;
67-
68-
if (!GoTypeUtil.isString(type)) {
67+
if (expression == null || !GoTypeUtil.isString(expression.getGoType(null))) {
6968
return false;
7069
}
7170

@@ -74,8 +73,4 @@ private static boolean isStringIndexExpression(@NotNull GoIndexOrSliceExpr expr)
7473
&& indices.getThird() == null
7574
&& expr.getNode().getChildren(TokenSet.create(GoTypes.COLON)).length == 0;
7675
}
77-
78-
public static boolean isSingleCharLiteral(@NotNull GoStringLiteral literal) {
79-
return literal.getDecodedText().length() == 1;
80-
}
8176
}

src/com/goide/psi/impl/GoPsiImplUtil.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,14 +1676,7 @@ private static PsiElement getNotNullElement(@Nullable PsiElement... elements) {
16761676
return null;
16771677
}
16781678

1679-
@Nullable
1680-
public static <T extends PsiElement> T getFirstElementOfType(@NotNull Class<T> clazz, @Nullable PsiElement... elements) {
1681-
if (elements == null) return null;
1682-
for (PsiElement e : elements) {
1683-
if (clazz.isInstance(e)) {
1684-
return clazz.cast(e);
1685-
}
1686-
}
1687-
return null;
1679+
public static boolean isSingleCharLiteral(@NotNull GoStringLiteral literal) {
1680+
return literal.getDecodedText().length() == 1;
16881681
}
16891682
}

src/com/goide/quickfix/GoStringIndexIsByteQuickFix.java renamed to src/com/goide/quickfix/GoConvertStringToByteQuickFix.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,41 @@
2121
import com.intellij.codeInspection.LocalQuickFixBase;
2222
import com.intellij.codeInspection.ProblemDescriptor;
2323
import com.intellij.openapi.project.Project;
24+
import com.intellij.psi.ElementManipulators;
2425
import com.intellij.psi.PsiElement;
26+
import com.intellij.util.containers.ContainerUtil;
2527
import org.jetbrains.annotations.NotNull;
2628

27-
import static com.goide.inspections.GoStringIndexIsByteInspection.isSingleCharLiteral;
29+
import java.util.Arrays;
30+
2831
import static com.goide.psi.impl.GoElementFactory.createExpression;
29-
import static com.goide.psi.impl.GoPsiImplUtil.getFirstElementOfType;
30-
import static com.intellij.psi.ElementManipulators.getValueTextRange;
32+
import static com.goide.psi.impl.GoPsiImplUtil.isSingleCharLiteral;
3133
import static java.lang.String.format;
3234

33-
public class GoStringIndexIsByteQuickFix extends LocalQuickFixBase {
34-
35+
public class GoConvertStringToByteQuickFix extends LocalQuickFixBase {
3536
public static final String NAME = "Convert string to byte";
3637

37-
public GoStringIndexIsByteQuickFix() {
38+
public GoConvertStringToByteQuickFix() {
3839
super(NAME);
3940
}
4041

4142
@Override
4243
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
4344
PsiElement element = descriptor.getPsiElement();
44-
if (!(element instanceof GoConditionalExpr)) {
45+
if (!(element instanceof GoConditionalExpr) || !element.isValid()) {
4546
return;
4647
}
4748

4849
GoConditionalExpr expr = (GoConditionalExpr)element;
49-
GoStringLiteral literal = getFirstElementOfType(GoStringLiteral.class, expr.getLeft(), expr.getRight());
50+
GoStringLiteral literal = ContainerUtil.findInstance(Arrays.asList(expr.getLeft(), expr.getRight()), GoStringLiteral.class);
5051
if (literal == null || !isSingleCharLiteral(literal)) {
5152
return;
5253
}
53-
5454
literal.replace(createExpression(project, extractSingleCharFromText(literal)));
5555
}
5656

5757
@NotNull
5858
private static String extractSingleCharFromText(@NotNull GoStringLiteral element) {
59-
return format("'%s'", getValueTextRange(element).substring(element.getText()));
59+
return format("'%s'", ElementManipulators.getValueText(element));
6060
}
6161
}

tests/com/goide/inspections/GoHighlightingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void setUp() throws Exception {
7676
GoInvalidStringOrCharInspection.class,
7777
GoMixedNamedUnnamedParametersInspection.class,
7878
GoAnonymousFieldDefinitionTypeInspection.class,
79-
GoStringIndexIsByteInspection.class
79+
GoStringAndByteTypeMismatchInspection.class
8080
);
8181
}
8282

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.goide.quickfix;
18+
19+
import com.goide.SdkAware;
20+
import com.goide.inspections.GoStringAndByteTypeMismatchInspection;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
@SdkAware
24+
public class GoConvertStringToByteQuickFixTest extends GoQuickFixTestBase {
25+
@Override
26+
protected void setUp() throws Exception {
27+
super.setUp();
28+
myFixture.enableInspections(GoStringAndByteTypeMismatchInspection.class);
29+
}
30+
31+
@NotNull
32+
@Override
33+
protected String getBasePath() {
34+
return "quickfixes/string-index-is-byte";
35+
}
36+
37+
private void doTest() { doTest(GoConvertStringToByteQuickFix.NAME); }
38+
private void doTestNoFix(boolean checkHighlighting) { doTestNoFix(GoConvertStringToByteQuickFix.NAME, checkHighlighting); }
39+
40+
public void testEqualsCondition() { doTest(); }
41+
public void testNotEqualsCondition() { doTest(); }
42+
public void testGreaterCondition() { doTest(); }
43+
public void testGreaterOrEqualsCondition() { doTest(); }
44+
public void testLessCondition() { doTest(); }
45+
public void testLessOrEqualsCondition() { doTest(); }
46+
public void testReverse() { doTest(); }
47+
public void testLiterals() { doTest(); }
48+
public void testLongLiteral() { doTest(); }
49+
public void testSliceFromLeft() { doTestNoFix(false); }
50+
public void testSliceFromRight() { doTestNoFix(false); }
51+
public void testSliceUnbound() { doTestNoFix(false); }
52+
public void testMoreThanOneCharInString() { doTestNoFix(true); }
53+
}

tests/com/goide/quickfix/GoStringIndexIsByteQuickFixTest.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)