Skip to content

Commit

Permalink
only suggest subList for lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Apr 4, 2024
1 parent a14a755 commit 66df616
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private void checkSubList(CtFor ctFor) {
return;
}


this.addLocalProblem(
ctFor,
new LocalizedMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class ForToForEachLoop extends IntegratedCheck {
if (ctVariableAccess.getParent() instanceof CtInvocation<?> ctInvocation
// && SpoonUtil.isSignatureEqualTo(ctInvocation.getExecutable(), Object.class, "get", int.class)
&& ctInvocation.getExecutable().getSimpleName().equals("get")
&& ctInvocation.getTarget() instanceof CtVariableAccess<?> variableAccess) {
&& ctInvocation.getTarget() instanceof CtVariableAccess<?> variableAccess
&& SpoonUtil.isSubtypeOf(variableAccess.getType(), java.util.List.class)) {
return Optional.of(variableAccess);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,46 @@ public static void printList2(List<Integer> list, int start, int end) {
assertEqualsReimplementation(problems.next(), "for (int value : list.subList(start, end)) { ... }");
problems.assertExhausted();
}

@Test
void testEntireList() throws LinterException, IOException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString(
JavaVersion.JAVA_17,
"Test",
"""
import java.util.List;
public class Test {
public static void iter(List<String> storage) {
for (int i = 0; i < storage.size(); i++) {
System.out.println(storage.get(i));
}
}
}
"""
), PROBLEM_TYPES);

problems.assertExhausted();
}

@Test
void testMap() throws LinterException, IOException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString(
JavaVersion.JAVA_17,
"Test",
"""
import java.util.Map;
public class Test {
public static void iterMap(Map<Integer, String> storage) {
for (int i = 0; i < storage.size(); i++) {
System.out.println(storage.get(i));
}
}
}
"""
), PROBLEM_TYPES);

problems.assertExhausted();
}
}

0 comments on commit 66df616

Please sign in to comment.