-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check that iteration variable in for-each loop not used outside (#…
…708)
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
jadx-core/src/test/java/jadx/tests/integration/loops/TestLoopDetection5.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jadx.tests.integration.loops; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import jadx.core.dex.nodes.ClassNode; | ||
import jadx.tests.api.IntegrationTest; | ||
|
||
import static jadx.tests.api.utils.JadxMatchers.containsOne; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
public class TestLoopDetection5 extends IntegrationTest { | ||
|
||
public static class TestCls { | ||
|
||
public String test(String str) { | ||
Iterator<String> it = getStrings().iterator(); | ||
String otherStr = null; | ||
while (it.hasNext()) { | ||
otherStr = it.next(); | ||
if (otherStr.equalsIgnoreCase(str)) { | ||
break; | ||
} | ||
} | ||
return otherStr; | ||
} | ||
|
||
private List<String> getStrings() { | ||
return Arrays.asList("str", "otherStr", "STR", "OTHERSTR"); | ||
} | ||
|
||
public void check() { | ||
assertThat(test("OTHERSTR"), is("otherStr")); | ||
} | ||
} | ||
|
||
@Test | ||
public void test() { | ||
noDebugInfo(); | ||
ClassNode cls = getClassNode(TestCls.class); | ||
String code = cls.getCode().toString(); | ||
|
||
assertThat(code, not(containsString("for ("))); | ||
assertThat(code, containsOne("it.next();")); | ||
} | ||
} |