Skip to content

Commit 5d98f7f

Browse files
walacglemco
authored andcommitted
rv/rvgen: fix unbound loop variable warning
Pyright static analysis reports a "possibly unbound variable" warning for the loop variable `i` in the `abbreviate_atoms` function. The variable is accessed after the inner loop terminates to slice the atom string. While the loop logic currently ensures execution, the analyzer flags the reliance on the loop variable persisting outside its scope. Refactor the prefix length calculation into a nested `find_share_length` helper function. This encapsulates the search logic and uses explicit return statements, ensuring the length value is strictly defined. This satisfies the type checker and improves code readability without altering the runtime behavior. Signed-off-by: Wander Lairson Costa <wander@redhat.com> Reviewed-by: Gabriele Monaco <gmonaco@redhat.com> Link: https://lore.kernel.org/r/20260223162407.147003-19-wander@redhat.com Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
1 parent 957dcbf commit 5d98f7f

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

tools/verification/rvgen/rvgen/ltl2k.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ def shorten(s: str) -> str:
4444
skip = ["is", "by", "or", "and"]
4545
return '_'.join([x[:2] for x in s.lower().split('_') if x not in skip])
4646

47-
abbrs = []
48-
for atom in atoms:
47+
def find_share_length(atom: str) -> int:
4948
for i in range(len(atom), -1, -1):
5049
if sum(a.startswith(atom[:i]) for a in atoms) > 1:
51-
break
52-
share = atom[:i]
53-
unique = atom[i:]
50+
return i
51+
return 0
52+
53+
abbrs = []
54+
for atom in atoms:
55+
share_len = find_share_length(atom)
56+
share = atom[:share_len]
57+
unique = atom[share_len:]
5458
abbrs.append((shorten(share) + shorten(unique)))
5559
return abbrs
5660

0 commit comments

Comments
 (0)