diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF006.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF006.py index f11ecd5400293e..754503cd1d6867 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF006.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF006.py @@ -152,3 +152,13 @@ async def f(x: bool): t = asyncio.create_task(asyncio.sleep(1)) else: t = None + + +# OK +async def f(x: bool): + global T + + if x: + T = asyncio.create_task(asyncio.sleep(1)) + else: + T = None diff --git a/crates/ruff_linter/src/rules/ruff/rules/asyncio_dangling_task.rs b/crates/ruff_linter/src/rules/ruff/rules/asyncio_dangling_task.rs index 15188b588048c4..3dcb52fe54da07 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/asyncio_dangling_task.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/asyncio_dangling_task.rs @@ -112,11 +112,15 @@ pub(crate) fn asyncio_dangling_binding( for binding_id in scope.binding_ids() { // If the binding itself is used, or it's not an assignment, skip it. let binding = semantic.binding(binding_id); - if binding.is_used() || !binding.kind.is_assignment() { + if binding.is_used() + || binding.is_global() + || binding.is_nonlocal() + || !binding.kind.is_assignment() + { continue; } - // Otherwise, any dangling tasks, including those that are shadowed, as in: + // Otherwise, flag any dangling tasks, including those that are shadowed, as in: // ```python // if x > 0: // task = asyncio.create_task(make_request()) @@ -127,10 +131,16 @@ pub(crate) fn asyncio_dangling_binding( std::iter::successors(Some(binding_id), |id| semantic.shadowed_binding(*id)) { let binding = semantic.binding(binding_id); - if binding.is_used() || !binding.kind.is_assignment() { + if binding.is_used() + || binding.is_global() + || binding.is_nonlocal() + || !binding.kind.is_assignment() + { continue; } + println!("binding: {:?}", binding); + let Some(source) = binding.source else { continue; };