Remove AstNode and AnyNode (#15479)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

While looking into potential AST optimizations, I noticed the `AstNode`
trait and `AnyNode` type aren't used anywhere in Ruff or Red Knot. It
looks like they might be historical artifacts of previous ways of
consuming AST nodes?

- `AstNode::cast`, `AstNode::cast_ref`, and `AstNode::can_cast` are not
used anywhere.
- Since `cast_ref` isn't needed anymore, the `Ref` associated type isn't
either.

This is a pure refactoring, with no intended behavior changes.
This commit is contained in:
Douglas Creager 2025-01-17 17:11:00 -05:00 committed by GitHub
parent 8e3633f55a
commit 98ef564170
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 52 additions and 5349 deletions

View file

@ -1,5 +1,4 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::AstNode;
use ruff_python_ast::StmtGlobal;
use crate::comments::SourceComment;
@ -14,7 +13,7 @@ impl FormatNodeRule<StmtGlobal> for FormatStmtGlobal {
// Join the `global` names, breaking across continuation lines if necessary, unless the
// `global` statement has a trailing comment, in which case, breaking the names would
// move the comment "off" of the `global` statement.
if f.context().comments().has_trailing(item.as_any_node_ref()) {
if f.context().comments().has_trailing(item) {
let joined = format_with(|f| {
f.join_with(format_args![token(","), space()])
.entries(item.names.iter().formatted())

View file

@ -1,5 +1,4 @@
use ruff_formatter::write;
use ruff_python_ast::AstNode;
use ruff_python_ast::StmtImportFrom;
use ruff_text_size::Ranged;
@ -62,7 +61,7 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
// )
// ```
let comments = f.context().comments().clone();
let parenthesized_comments = comments.dangling(item.as_any_node_ref());
let parenthesized_comments = comments.dangling(item);
if parenthesized_comments.is_empty() {
parenthesize_if_expands(&names).fmt(f)

View file

@ -1,5 +1,4 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::AstNode;
use ruff_python_ast::StmtNonlocal;
use crate::comments::SourceComment;
@ -14,7 +13,7 @@ impl FormatNodeRule<StmtNonlocal> for FormatStmtNonlocal {
// Join the `nonlocal` names, breaking across continuation lines if necessary, unless the
// `nonlocal` statement has a trailing comment, in which case, breaking the names would
// move the comment "off" of the `nonlocal` statement.
if f.context().comments().has_trailing(item.as_any_node_ref()) {
if f.context().comments().has_trailing(item) {
let joined = format_with(|f| {
f.join_with(format_args![token(","), space()])
.entries(item.names.iter().formatted())

View file

@ -1,5 +1,4 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::AstNode;
use ruff_python_ast::{Stmt, StmtWhile};
use ruff_text_size::Ranged;
@ -22,7 +21,7 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
} = item;
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling(item.as_any_node_ref());
let dangling_comments = comments.dangling(item);
let body_start = body.first().map_or(test.end(), Stmt::start);
let or_else_comments_start =

View file

@ -1,6 +1,5 @@
use ruff_formatter::{format_args, write, FormatContext, FormatError};
use ruff_python_ast::StmtWith;
use ruff_python_ast::{AstNode, WithItem};
use ruff_python_ast::{StmtWith, WithItem};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{Ranged, TextRange};
@ -36,7 +35,7 @@ impl FormatNodeRule<StmtWith> for FormatStmtWith {
// ...
// ```
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling(with_stmt.as_any_node_ref());
let dangling_comments = comments.dangling(with_stmt);
let partition_point = dangling_comments.partition_point(|comment| {
with_stmt
.items