Refactor range from Attributed to Nodes (#4422)

This commit is contained in:
Micha Reiser 2023-05-16 08:36:32 +02:00 committed by GitHub
parent 140e0acf54
commit fa26860296
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
330 changed files with 4816 additions and 3946 deletions

View file

@ -1,6 +1,6 @@
use std::cmp::Ordering;
use rustpython_parser::ast::{self, ExcepthandlerKind, Stmt, StmtKind};
use rustpython_parser::ast::{self, Excepthandler, Stmt};
use crate::node::{NodeId, Nodes};
@ -41,15 +41,15 @@ fn common_ancestor(
/// Return the alternative branches for a given node.
fn alternatives(stmt: &Stmt) -> Vec<Vec<&Stmt>> {
match &stmt.node {
StmtKind::If(ast::StmtIf { body, .. }) => vec![body.iter().collect()],
StmtKind::Try(ast::StmtTry {
match stmt {
Stmt::If(ast::StmtIf { body, .. }) => vec![body.iter().collect()],
Stmt::Try(ast::StmtTry {
body,
handlers,
orelse,
..
})
| StmtKind::TryStar(ast::StmtTryStar {
| Stmt::TryStar(ast::StmtTryStar {
body,
handlers,
orelse,
@ -57,13 +57,12 @@ fn alternatives(stmt: &Stmt) -> Vec<Vec<&Stmt>> {
}) => vec![body.iter().chain(orelse.iter()).collect()]
.into_iter()
.chain(handlers.iter().map(|handler| {
let ExcepthandlerKind::ExceptHandler(ast::ExcepthandlerExceptHandler {
body, ..
}) = &handler.node;
let Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler { body, .. }) =
handler;
body.iter().collect()
}))
.collect(),
StmtKind::Match(ast::StmtMatch { cases, .. }) => cases
Stmt::Match(ast::StmtMatch { cases, .. }) => cases
.iter()
.map(|case| case.body.iter().collect())
.collect(),

View file

@ -1,4 +1,4 @@
use rustpython_parser::ast::{self, Expr, ExprKind};
use rustpython_parser::ast::{self, Expr};
use ruff_python_ast::call_path::collect_call_path;
@ -17,7 +17,7 @@ use crate::context::Context;
/// bar.error()
/// ```
pub fn is_logger_candidate(context: &Context, func: &Expr) -> bool {
if let ExprKind::Attribute(ast::ExprAttribute { value, .. }) = &func.node {
if let Expr::Attribute(ast::ExprAttribute { value, .. }) = func {
let Some(call_path) = (if let Some(call_path) = context.resolve_call_path(value) {
if call_path.first().map_or(false, |module| *module == "logging") || call_path.as_slice() == ["flask", "current_app", "logger"] {
Some(call_path)

View file

@ -1,4 +1,4 @@
use rustpython_parser::ast::{self, Constant, Expr, ExprKind, Operator};
use rustpython_parser::ast::{self, Constant, Expr, Operator};
use ruff_python_ast::call_path::{from_unqualified_name, CallPath};
use ruff_python_stdlib::typing::{
@ -29,7 +29,7 @@ pub fn match_annotated_subscript<'a>(
context: &Context,
typing_modules: impl Iterator<Item = &'a str>,
) -> Option<SubscriptKind> {
if !matches!(expr.node, ExprKind::Name(_) | ExprKind::Attribute(_)) {
if !matches!(expr, Expr::Name(_) | Expr::Attribute(_)) {
return None;
}
@ -113,12 +113,12 @@ pub enum Pep604Operator {
pub fn to_pep604_operator(value: &Expr, slice: &Expr, context: &Context) -> Option<Pep604Operator> {
/// Returns `true` if any argument in the slice is a string.
fn any_arg_is_str(slice: &Expr) -> bool {
match &slice.node {
ExprKind::Constant(ast::ExprConstant {
match slice {
Expr::Constant(ast::ExprConstant {
value: Constant::Str(_),
..
}) => true,
ExprKind::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().any(any_arg_is_str),
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().any(any_arg_is_str),
_ => false,
}
}
@ -146,8 +146,8 @@ pub fn to_pep604_operator(value: &Expr, slice: &Expr, context: &Context) -> Opti
/// Return `true` if `Expr` represents a reference to a type annotation that resolves to an
/// immutable type.
pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
match &expr.node {
ExprKind::Name(_) | ExprKind::Attribute(_) => {
match expr {
Expr::Name(_) | Expr::Attribute(_) => {
context.resolve_call_path(expr).map_or(false, |call_path| {
IMMUTABLE_TYPES
.iter()
@ -155,7 +155,7 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
.any(|target| call_path.as_slice() == *target)
})
}
ExprKind::Subscript(ast::ExprSubscript { value, slice, .. }) => {
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
context.resolve_call_path(value).map_or(false, |call_path| {
if IMMUTABLE_GENERIC_TYPES
.iter()
@ -163,7 +163,7 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
{
true
} else if call_path.as_slice() == ["typing", "Union"] {
if let ExprKind::Tuple(ast::ExprTuple { elts, .. }) = &slice.node {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
elts.iter().all(|elt| is_immutable_annotation(context, elt))
} else {
false
@ -171,7 +171,7 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
} else if call_path.as_slice() == ["typing", "Optional"] {
is_immutable_annotation(context, slice)
} else if call_path.as_slice() == ["typing", "Annotated"] {
if let ExprKind::Tuple(ast::ExprTuple { elts, .. }) = &slice.node {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
elts.first()
.map_or(false, |elt| is_immutable_annotation(context, elt))
} else {
@ -182,12 +182,13 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
}
})
}
ExprKind::BinOp(ast::ExprBinOp {
Expr::BinOp(ast::ExprBinOp {
left,
op: Operator::BitOr,
right,
range: _range,
}) => is_immutable_annotation(context, left) && is_immutable_annotation(context, right),
ExprKind::Constant(ast::ExprConstant {
Expr::Constant(ast::ExprConstant {
value: Constant::None,
..
}) => true,

View file

@ -1,6 +1,6 @@
use std::path::Path;
use rustpython_parser::ast::{self, Expr, Stmt, StmtKind};
use rustpython_parser::ast::{self, Expr, Stmt};
use ruff_python_ast::call_path::{collect_call_path, CallPath};
use ruff_python_ast::helpers::map_callable;
@ -164,9 +164,9 @@ impl ModuleSource<'_> {
}
pub(crate) fn function_visibility(stmt: &Stmt) -> Visibility {
match &stmt.node {
StmtKind::FunctionDef(ast::StmtFunctionDef { name, .. })
| StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => {
match stmt {
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => {
if name.starts_with('_') {
Visibility::Private
} else {
@ -178,13 +178,13 @@ pub(crate) fn function_visibility(stmt: &Stmt) -> Visibility {
}
pub(crate) fn method_visibility(stmt: &Stmt) -> Visibility {
match &stmt.node {
StmtKind::FunctionDef(ast::StmtFunctionDef {
match stmt {
Stmt::FunctionDef(ast::StmtFunctionDef {
name,
decorator_list,
..
})
| StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
name,
decorator_list,
..
@ -216,8 +216,8 @@ pub(crate) fn method_visibility(stmt: &Stmt) -> Visibility {
}
pub(crate) fn class_visibility(stmt: &Stmt) -> Visibility {
match &stmt.node {
StmtKind::ClassDef(ast::StmtClassDef { name, .. }) => {
match stmt {
Stmt::ClassDef(ast::StmtClassDef { name, .. }) => {
if name.starts_with('_') {
Visibility::Private
} else {

View file

@ -5,7 +5,7 @@ use std::fmt::Debug;
use std::num::TryFromIntError;
use std::ops::{Deref, Index};
use rustpython_parser::ast::{self, Stmt, StmtKind};
use rustpython_parser::ast::{self, Stmt};
use crate::analyze::visibility::{
class_visibility, function_visibility, method_visibility, ModuleSource, Visibility,
@ -87,10 +87,10 @@ pub struct Member<'a> {
impl<'a> Member<'a> {
fn name(&self) -> &'a str {
match &self.stmt.node {
StmtKind::FunctionDef(ast::StmtFunctionDef { name, .. }) => name,
StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => name,
StmtKind::ClassDef(ast::StmtClassDef { name, .. }) => name,
match &self.stmt {
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => name,
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => name,
Stmt::ClassDef(ast::StmtClassDef { name, .. }) => name,
_ => unreachable!("Unexpected member kind: {:?}", self.kind),
}
}

View file

@ -115,7 +115,7 @@ pub enum ScopeKind<'a> {
#[derive(Debug)]
pub struct FunctionDef<'a> {
// Properties derived from StmtKind::FunctionDef.
// Properties derived from Stmt::FunctionDef.
pub name: &'a str,
pub args: &'a Arguments,
pub body: &'a [Stmt],
@ -130,7 +130,7 @@ pub struct FunctionDef<'a> {
#[derive(Debug)]
pub struct ClassDef<'a> {
// Properties derived from StmtKind::ClassDef.
// Properties derived from Stmt::ClassDef.
pub name: &'a str,
pub bases: &'a [Expr],
pub keywords: &'a [Keyword],