Skip to content

Commit

Permalink
Remove AstNode and AnyNode (#15479)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dcreager authored Jan 17, 2025
1 parent 8e3633f commit 98ef564
Show file tree
Hide file tree
Showing 24 changed files with 1,201 additions and 6,498 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::AstNode;
use ruff_python_ast::{self as ast, Expr, ExprCall, ExprContext};
use ruff_python_codegen::Generator;
use ruff_python_trivia::CommentRanges;
Expand Down Expand Up @@ -324,7 +323,7 @@ fn get_parametrize_name_range(
) -> Option<TextRange> {
parenthesized_range(
expr.into(),
call.arguments.as_any_node_ref(),
(&call.arguments).into(),
comment_ranges,
source,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Range;
use ruff_diagnostics::{AlwaysFixableViolation, Applicability, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::{AstNode, Expr, ExprBinOp, ExprCall, Operator};
use ruff_python_ast::{Expr, ExprBinOp, ExprCall, Operator};
use ruff_python_semantic::SemanticModel;
use ruff_python_trivia::CommentRanges;
use ruff_text_size::{Ranged, TextRange};
Expand Down Expand Up @@ -134,12 +134,6 @@ fn parent_and_next_path_fragment_range(

Some((
parent.range(),
parenthesized_range(
right.into(),
parent.as_any_node_ref(),
comment_ranges,
source,
)
.unwrap_or(range),
parenthesized_range(right.into(), parent.into(), comment_ranges, source).unwrap_or(range),
))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::{
self as ast, AstNode, Expr, ExprEllipsisLiteral, ExprLambda, Identifier, Parameter,
self as ast, Expr, ExprEllipsisLiteral, ExprLambda, Identifier, Parameter,
ParameterWithDefault, Parameters, Stmt,
};
use ruff_python_semantic::SemanticModel;
Expand Down Expand Up @@ -263,7 +263,7 @@ fn replace_trailing_ellipsis_with_original_expr(
) -> String {
let original_expr_range = parenthesized_range(
(&lambda.body).into(),
lambda.as_any_node_ref(),
lambda.into(),
checker.comment_ranges(),
checker.source(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast as ast;
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::{AstNode, ExprBinOp, ExpressionRef, Operator};
use ruff_python_ast::{ExprBinOp, ExpressionRef, Operator};
use ruff_text_size::{Ranged, TextRange};

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -147,7 +147,7 @@ fn augmented_assignment(
let locator = checker.locator();

let right_operand_ref = ExpressionRef::from(right_operand);
let parent = original_expr.as_any_node_ref();
let parent = original_expr.into();
let comment_ranges = checker.comment_ranges();
let source = checker.source();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use itertools::Itertools;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::AstNode;
use ruff_python_ast::{self as ast, Arguments, Expr};
use ruff_python_semantic::SemanticModel;
use ruff_text_size::Ranged;
Expand Down Expand Up @@ -110,7 +109,7 @@ fn convert_to_reduce(iterable: &Expr, call: &ast::ExprCall, checker: &Checker) -
let iterable = checker.locator().slice(
parenthesized_range(
iterable.into(),
call.arguments.as_any_node_ref(),
(&call.arguments).into(),
checker.comment_ranges(),
checker.locator().contents(),
)
Expand Down
5 changes: 2 additions & 3 deletions crates/ruff_python_ast/ast.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
# is, `StmtIf` will become `if_stmt`.)
#
# anynode_is_label: foo_bar
# Controls the name of the AnyNode::foo_bar, AnyNode::is_foo_bar, and
# AnyNodeRef::is_foo_bar methods. The default is the group name in
# snake_case.
# Controls the name of the AnyNodeRef::is_foo_bar method. The default is the
# group name in snake_case.
#
# ref_enum_ty:
# The name of the reference enum that we create for this group. The default
Expand Down
Loading

0 comments on commit 98ef564

Please sign in to comment.