[flake8-return] Fix false-positive for variables used inside nested functions in RET504 (#18433)

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->
This PR is the same as #17656.

I accidentally deleted the branch of that PR, so I'm creating a new one.

Fixes #14052

## Test Plan

Add regression tests
<!-- How was it tested? -->
This commit is contained in:
Victor Hugo Gomes 2025-07-10 17:10:22 -03:00 committed by GitHub
parent 965f415212
commit f2ae12bab3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 142 additions and 50 deletions

View file

@ -2094,6 +2094,20 @@ impl<'a> SemanticModel<'a> {
None
})
}
/// Finds and returns the [`Scope`] corresponding to a given [`ast::StmtFunctionDef`].
///
/// This method searches all scopes created by a function definition, comparing the
/// [`TextRange`] of the provided `function_def` with the the range of the function
/// associated with the scope.
pub fn function_scope(&self, function_def: &ast::StmtFunctionDef) -> Option<&Scope> {
self.scopes.iter().find(|scope| {
let Some(function) = scope.kind.as_function() else {
return false;
};
function.range() == function_def.range()
})
}
}
pub struct ShadowedBinding {