mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 16:40:19 +00:00
Detect asyncio.get_running_loop
calls in RUF006 (#7562)
## Summary We can do a good enough job detecting this with our existing semantic model. Closes https://github.com/astral-sh/ruff/issues/3237.
This commit is contained in:
parent
b685ee4749
commit
87a0cd219f
6 changed files with 103 additions and 60 deletions
|
@ -521,3 +521,36 @@ fn find_parameter<'a>(
|
|||
.chain(parameters.kwonlyargs.iter())
|
||||
.find(|arg| arg.parameter.name.range() == binding.range())
|
||||
}
|
||||
|
||||
/// Return the [`CallPath`] of the value to which the given [`Expr`] is assigned, if any.
|
||||
///
|
||||
/// For example, given:
|
||||
/// ```python
|
||||
/// import asyncio
|
||||
///
|
||||
/// loop = asyncio.get_running_loop()
|
||||
/// loop.create_task(...)
|
||||
/// ```
|
||||
///
|
||||
/// This function will return `["asyncio", "get_running_loop"]` for the `loop` binding.
|
||||
pub fn resolve_assignment<'a>(
|
||||
expr: &'a Expr,
|
||||
semantic: &'a SemanticModel<'a>,
|
||||
) -> Option<CallPath<'a>> {
|
||||
let name = expr.as_name_expr()?;
|
||||
let binding_id = semantic.resolve_name(name)?;
|
||||
let statement = semantic.binding(binding_id).statement(semantic)?;
|
||||
match statement {
|
||||
Stmt::Assign(ast::StmtAssign { value, .. }) => {
|
||||
let ast::ExprCall { func, .. } = value.as_call_expr()?;
|
||||
semantic.resolve_call_path(func)
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
value: Some(value), ..
|
||||
}) => {
|
||||
let ast::ExprCall { func, .. } = value.as_call_expr()?;
|
||||
semantic.resolve_call_path(func)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue