Skip to content

Commit 7f453db

Browse files
kinto0meta-codesync[bot]
authored andcommitted
Add more test cases for unused variable false positives in loops
Summary: more tests to ensure the reports are fixed Reviewed By: yangdanny97 Differential Revision: D97020182 fbshipit-source-id: a468f0a29bbde694f5e4d13014cfa1d18a81fe64
1 parent 8e33ca2 commit 7f453db

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pyrefly/lib/test/lsp/diagnostic.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,39 @@ def func(count: int) -> int:
301301
assert_eq!(report, "No unused variables");
302302
}
303303

304+
// Local variable reassigned using its own value in a loop should not be
305+
// reported as unused.
306+
#[test]
307+
fn test_local_reassignment_in_loop() {
308+
let code = r#"
309+
def f():
310+
x = 0
311+
while x < 10:
312+
x = x + 1
313+
"#;
314+
let (handles, state) = mk_multi_file_state(&[("main", code)], Require::Exports, true);
315+
let handle = handles.get("main").unwrap();
316+
let report = get_unused_variable_diagnostics(&state, handle);
317+
assert_eq!(report, "No unused variables");
318+
}
319+
320+
// Reassigning a parameter using a slice of itself in a loop should not be
321+
// reported as unused.
322+
#[test]
323+
fn test_parameter_slice_reassignment_in_loop() {
324+
let code = r#"
325+
from os import write as _write
326+
327+
def _write_all(fd: int, data: bytes):
328+
while (n := _write(fd, data)) < len(data):
329+
data = data[n:]
330+
"#;
331+
let (handles, state) = mk_multi_file_state(&[("main", code)], Require::Exports, true);
332+
let handle = handles.get("main").unwrap();
333+
let report = get_unused_variable_diagnostics(&state, handle);
334+
assert_eq!(report, "No unused variables");
335+
}
336+
304337
// Test for issue #1961: `import a as a` and `from x import a as a` are explicit re-exports
305338
// per the Python typing spec and should NOT be reported as unused imports.
306339
// See: https://typing.python.org/en/latest/spec/distributing.html#import-conventions

0 commit comments

Comments
 (0)