Use name directly in RUF006 (#9979)

This commit is contained in:
Charlie Marsh 2024-02-13 19:00:47 -05:00 committed by GitHub
parent 3e9d761b13
commit f40e012b4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,7 +3,6 @@ use std::fmt;
use ast::Stmt; use ast::Stmt;
use ruff_diagnostics::{Diagnostic, Violation}; use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::compose_call_path;
use ruff_python_ast::{self as ast, Expr}; use ruff_python_ast::{self as ast, Expr};
use ruff_python_semantic::{analyze::typing, Scope, SemanticModel}; use ruff_python_semantic::{analyze::typing, Scope, SemanticModel};
use ruff_text_size::Ranged; use ruff_text_size::Ranged;
@ -93,22 +92,24 @@ pub(crate) fn asyncio_dangling_task(expr: &Expr, semantic: &SemanticModel) -> Op
// Ex) `loop = ...; loop.create_task(...)` // Ex) `loop = ...; loop.create_task(...)`
if let Expr::Attribute(ast::ExprAttribute { attr, value, .. }) = func.as_ref() { if let Expr::Attribute(ast::ExprAttribute { attr, value, .. }) = func.as_ref() {
if attr == "create_task" { if attr == "create_task" {
if typing::resolve_assignment(value, semantic).is_some_and(|call_path| { if let Expr::Name(name) = value.as_ref() {
matches!( if typing::resolve_assignment(value, semantic).is_some_and(|call_path| {
call_path.as_slice(), matches!(
[ call_path.as_slice(),
"asyncio", [
"get_event_loop" | "get_running_loop" | "new_event_loop" "asyncio",
] "get_event_loop" | "get_running_loop" | "new_event_loop"
) ]
}) { )
return Some(Diagnostic::new( }) {
AsyncioDanglingTask { return Some(Diagnostic::new(
expr: compose_call_path(value).unwrap_or_else(|| "asyncio".to_string()), AsyncioDanglingTask {
method: Method::CreateTask, expr: name.id.to_string(),
}, method: Method::CreateTask,
expr.range(), },
)); expr.range(),
));
}
} }
} }
} }