Skip to content

Commit 1c58437

Browse files
SONARJAVA-5304 Improve issue message in case of array type cast in S6201 (#5019)
1 parent 376de24 commit 1c58437

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

java-checks-test-sources/default/src/main/java/checks/InstanceOfPatternMatching.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
public abstract class InstanceOfPatternMatching {
66

7+
private static String booleanArray(Object o){
8+
if(o instanceof boolean[]){ // Noncompliant {{Replace this instanceof check and cast with 'instanceof boolean[] booleanArray'}}
9+
return Boolean.toString(((boolean[])o)[0]);
10+
}
11+
return "not a boolean array";
12+
}
13+
14+
private static String booleanArray2(Object o){
15+
if(o instanceof boolean[][]){ // Noncompliant {{Replace this instanceof check and cast with 'instanceof boolean[][] booleanArrayArray'}}
16+
return Boolean.toString(((boolean[][])o)[0][0]);
17+
}
18+
return "not a boolean array";
19+
}
20+
721
int if1(Object o) {
822
if (o instanceof String) { // Noncompliant {{Replace this instanceof check and cast with 'instanceof String str'}}
923
// ^^^^^^^^^^^^^^^^^^^

java-checks/src/main/java/org/sonar/java/checks/InstanceOfPatternMatchingCheck.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ public void visitTypeCast(TypeCastTree tree) {
171171
Type type = tree.symbolType();
172172
if (!type.isUnknown() && type.equals(instanceOf.type().symbolType())
173173
&& SyntacticEquivalence.areEquivalentIncludingSameVariables(tree.expression(), instanceOf.expression())) {
174-
report(instanceOf, tree, tree.type().symbolType().name().toLowerCase(Locale.ROOT));
174+
Type symbolType = tree.type().symbolType();
175+
report(instanceOf, tree, makeVariableNameForType(symbolType));
175176
}
176177
}
177178

@@ -182,4 +183,8 @@ private void report(InstanceOfTree instanceOf, ExpressionTree cast, String name)
182183
reportIssue(instanceOf, message, Collections.singletonList(secondary), null);
183184
}
184185
}
186+
187+
private static String makeVariableNameForType(Type type) {
188+
return type.name().toLowerCase(Locale.ROOT).replace("[]", "Array");
189+
}
185190
}

0 commit comments

Comments
 (0)