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

@ -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;
@ -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,
)

View file

@ -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};
@ -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),
))
}

View file

@ -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;
@ -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(),
)

View file

@ -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;
@ -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();

View file

@ -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;
@ -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(),
)