Skip to content

Commit

Permalink
fix #548
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Jun 30, 2024
1 parent bdc5a7a commit ff2f1ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ private void checkAddAll(CtForEach ctFor) {
return;
}

// handle edge case where the variable is implicitly cast in the invocation (Character in List, but char in iterable)
List<CtTypeReference<?>> actualTypeArguments = ctInvocation.getTarget().getType().getActualTypeArguments();
if (!actualTypeArguments.isEmpty() && !ctFor.getVariable().getType().equals(actualTypeArguments.getFirst())) {
return;
}

String addAllArg = ctFor.getExpression().toString();
if (ctFor.getExpression().getType().isArray()) {
addAllArg = "Arrays.asList(%s)".formatted(addAllArg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void process(CtMethod<?> ctMethod) {
return;
}


if (!ctMethod.isStatic() && isEffectivelyStatic(ctMethod)) {
addLocalProblem(
ctMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,29 @@ public static <T, U> Collection<U> toCollection(Iterable<T> input) {

problems.assertExhausted();
}
}

@Test
void testAddAllImplicitCast() throws LinterException, IOException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString(
JavaVersion.JAVA_17,
"Main",
"""
import java.util.Collection;
import java.util.ArrayList;
public class Main {
public static Collection<Character> toCollection(char[] input) {
Collection<Character> result = new ArrayList<>();
for (char element : input) {
result.add(element);
}
return result;
}
}
"""
), PROBLEM_TYPES);

problems.assertExhausted();
}}

0 comments on commit ff2f1ef

Please sign in to comment.