mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-07 21:25:08 +00:00
Battling allocator
This commit is contained in:
parent
d7412af996
commit
d8bb0632c5
3 changed files with 89 additions and 173 deletions
|
@ -1,22 +1,11 @@
|
|||
use std::borrow::Borrow;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use bat::PrettyPrinter;
|
||||
use bumpalo::Bump;
|
||||
use libcst_native::{
|
||||
AnnAssign, Annotation, Arg, AsName, Assert, Assign, AssignEqual, AssignTarget,
|
||||
AssignTargetExpression, Asynchronous, Attribute, AugAssign, Await, BinaryOp, BinaryOperation,
|
||||
BooleanOp, BooleanOperation, Break, Call, ClassDef, Codegen, CompFor, CompIf, CompOp,
|
||||
Comparison, ComparisonTarget, CompoundStatement, ConcatenatedString, Continue, Decorator, Del,
|
||||
DelTargetExpression, Dict, DictComp, DictElement, Element, Ellipsis, Else, ExceptHandler,
|
||||
ExceptStarHandler, Expr, Expression, Finally, Float, For, FormattedString,
|
||||
FormattedStringContent, FormattedStringExpression, FormattedStringText, FunctionDef,
|
||||
GeneratorExp, Global, If, IfExp, Imaginary, Import, ImportAlias, ImportFrom, ImportStar,
|
||||
IndentedBlock, Index, Integer, Lambda, List, ListComp, Match, Module, Name, NameItem,
|
||||
NamedExpr, Nonlocal, OrElse, Param, ParamStar, Parameters, Pass, Raise, Return, Set, SetComp,
|
||||
SimpleStatementLine, SimpleStatementSuite, SimpleString, Slice, SmallStatement,
|
||||
StarredDictElement, StarredElement, Statement, Subscript, SubscriptElement, Try, TryStar,
|
||||
Tuple, UnaryOp, UnaryOperation, While, With, WithItem, Yield, YieldValue,
|
||||
Arg, ClassDef, Codegen, Expression, FormattedStringContent, If, Module, SimpleString,
|
||||
};
|
||||
use rustpython_parser::ast::Location;
|
||||
|
||||
|
@ -56,44 +45,32 @@ struct Binding {
|
|||
}
|
||||
|
||||
struct Checker<'a> {
|
||||
bump: Bump,
|
||||
settings: &'a Settings,
|
||||
checks: Vec<Check>,
|
||||
arena: Vec<String>,
|
||||
}
|
||||
|
||||
impl Checker<'_> {
|
||||
pub fn new(settings: &Settings) -> Checker {
|
||||
Checker {
|
||||
bump: Bump::new(),
|
||||
settings,
|
||||
checks: vec![],
|
||||
arena: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CSTVisitor for Checker<'_> {
|
||||
fn visit_If(&mut self, node: &If) {
|
||||
if let Expression::Tuple { .. } = node.test {
|
||||
self.checks.push(Check {
|
||||
kind: CheckKind::IfTuple,
|
||||
location: Default::default(),
|
||||
});
|
||||
}
|
||||
cst_visitor::walk_If(self, node);
|
||||
}
|
||||
const QUOTE: &str = "\"";
|
||||
|
||||
fn visit_Expression<'a, 'b>(&'b mut self, node: &'a Expression<'a>) -> Expression<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
impl<'b> CSTVisitor for Checker<'_> {
|
||||
fn visit_Expression<'a>(&mut self, node: &'a Expression<'a>) -> Expression<'a> {
|
||||
match node {
|
||||
Expression::FormattedString(node) => match &node.parts[..] {
|
||||
[node] => match node {
|
||||
FormattedStringContent::Text(node) => {
|
||||
let x = node.value.to_string();
|
||||
println!("Found: {:?}", node);
|
||||
self.arena.push(format!("\"{}\"", node.value));
|
||||
return Expression::SimpleString(Box::new(SimpleString {
|
||||
value: self.bump.alloc(format!("\"{}\"", x)),
|
||||
value: node.value,
|
||||
lpar: vec![],
|
||||
rpar: vec![],
|
||||
}));
|
||||
|
@ -133,6 +110,16 @@ impl CSTVisitor for Checker<'_> {
|
|||
transformed.bases = bases;
|
||||
transformed
|
||||
}
|
||||
|
||||
fn visit_If(&mut self, node: &If) {
|
||||
if let Expression::Tuple { .. } = node.test {
|
||||
self.checks.push(Check {
|
||||
kind: CheckKind::IfTuple,
|
||||
location: Default::default(),
|
||||
});
|
||||
}
|
||||
cst_visitor::walk_If(self, node);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_cst<'a>(python_cst: &'a Module<'a>, settings: &Settings) -> Vec<Check> {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#![allow(non_snake_case)]
|
||||
|
||||
use bumpalo::Bump;
|
||||
use libcst_native::{
|
||||
AnnAssign, Annotation, Arg, AsName, Assert, Assign, AssignEqual, AssignTarget,
|
||||
AssignTargetExpression, Asynchronous, Attribute, AugAssign, Await, BaseSlice, BinaryOp,
|
||||
|
@ -18,55 +19,31 @@ use libcst_native::{
|
|||
};
|
||||
|
||||
pub trait CSTVisitor {
|
||||
fn visit_Module<'a, 'b>(&'b mut self, node: &'a Module<'a>) -> Module<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
fn visit_Module<'a>(&mut self, node: &'a Module<'a>) -> Module<'a> {
|
||||
walk_Module(self, node)
|
||||
}
|
||||
fn visit_Statement<'a, 'b>(&'b mut self, node: &'a Statement<'a>) -> Option<Statement<'a>>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
fn visit_Statement<'a>(&mut self, node: &'a Statement<'a>) -> Option<Statement<'a>> {
|
||||
walk_Statement(self, node)
|
||||
}
|
||||
fn visit_SimpleStatementLine<'a, 'b>(
|
||||
&'b mut self,
|
||||
fn visit_SimpleStatementLine<'a>(
|
||||
&mut self,
|
||||
node: &'a SimpleStatementLine<'a>,
|
||||
) -> Option<SimpleStatementLine<'a>>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
) -> Option<SimpleStatementLine<'a>> {
|
||||
walk_SimpleStatementLine(self, node)
|
||||
}
|
||||
fn visit_CompoundStatement<'a, 'b>(
|
||||
&'b mut self,
|
||||
fn visit_CompoundStatement<'a>(
|
||||
&mut self,
|
||||
node: &'a CompoundStatement<'a>,
|
||||
) -> Option<CompoundStatement<'a>>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
) -> Option<CompoundStatement<'a>> {
|
||||
walk_CompoundStatement(self, node)
|
||||
}
|
||||
fn visit_SmallStatement<'a, 'b>(
|
||||
&'b mut self,
|
||||
node: &'a SmallStatement<'a>,
|
||||
) -> SmallStatement<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
fn visit_SmallStatement<'a>(&mut self, node: &'a SmallStatement<'a>) -> SmallStatement<'a> {
|
||||
walk_SmallStatement(self, node)
|
||||
}
|
||||
fn visit_Expression<'a, 'b>(&'b mut self, node: &'a Expression<'a>) -> Expression<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
fn visit_Expression<'a>(&mut self, node: &'a Expression<'a>) -> Expression<'a> {
|
||||
walk_Expression(self, node)
|
||||
}
|
||||
fn visit_AnnAssign<'a, 'b>(&'b mut self, node: &'a AnnAssign<'a>) -> AnnAssign<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
fn visit_AnnAssign<'a>(&mut self, node: &'a AnnAssign<'a>) -> AnnAssign<'a> {
|
||||
walk_AnnAssign(self, node)
|
||||
}
|
||||
fn visit_Annotation(&mut self, node: &Annotation) {
|
||||
|
@ -105,20 +82,11 @@ pub trait CSTVisitor {
|
|||
fn visit_Await(&mut self, node: &Await) {
|
||||
walk_Await(self, node);
|
||||
}
|
||||
fn visit_BinaryOperation<'a, 'b>(
|
||||
&'b mut self,
|
||||
node: &'a BinaryOperation<'a>,
|
||||
) -> BinaryOperation<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
fn visit_BinaryOperation<'a>(&mut self, node: &'a BinaryOperation<'a>) -> BinaryOperation<'a> {
|
||||
walk_BinaryOperation(self, node)
|
||||
}
|
||||
fn visit_BinaryOp<'a, 'b>(&'b mut self, node: &'a BinaryOp<'a>) -> BinaryOp<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
walk_BinaryOp(self, node)
|
||||
fn visit_BinaryOp(&mut self, node: &BinaryOp) {
|
||||
walk_BinaryOp(self, node);
|
||||
}
|
||||
fn visit_BooleanOperation(&mut self, node: &BooleanOperation) {
|
||||
walk_BooleanOperation(self, node);
|
||||
|
@ -374,14 +342,11 @@ pub trait CSTVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_Module<'a, 'b, V: CSTVisitor + ?Sized>(
|
||||
visitor: &'b mut V,
|
||||
pub fn walk_Module<'a, V: CSTVisitor + ?Sized>(
|
||||
visitor: &mut V,
|
||||
node: &'a Module<'a>,
|
||||
) -> Module<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
let mut body: Vec<Statement<'a>> = vec![];
|
||||
) -> Module<'a> {
|
||||
let mut body: Vec<Statement> = vec![];
|
||||
for node in &node.body {
|
||||
if let Some(node) = visitor.visit_Statement(node) {
|
||||
body.push(node)
|
||||
|
@ -392,13 +357,10 @@ where
|
|||
transformed.body = body;
|
||||
transformed
|
||||
}
|
||||
pub fn walk_Statement<'a, 'b, V: CSTVisitor + ?Sized>(
|
||||
visitor: &'b mut V,
|
||||
pub fn walk_Statement<'a, V: CSTVisitor + ?Sized>(
|
||||
visitor: &mut V,
|
||||
node: &'a Statement<'a>,
|
||||
) -> Option<Statement<'a>>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
) -> Option<Statement<'a>> {
|
||||
match node {
|
||||
Statement::Simple(node) => visitor
|
||||
.visit_SimpleStatementLine(node)
|
||||
|
@ -408,14 +370,11 @@ where
|
|||
.map(Statement::Compound),
|
||||
}
|
||||
}
|
||||
pub fn walk_SimpleStatementLine<'a, 'b, V: CSTVisitor + ?Sized>(
|
||||
visitor: &'b mut V,
|
||||
pub fn walk_SimpleStatementLine<'a, V: CSTVisitor + ?Sized>(
|
||||
visitor: &mut V,
|
||||
node: &'a SimpleStatementLine<'a>,
|
||||
) -> Option<SimpleStatementLine<'a>>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
println!("{:?}", node);
|
||||
) -> Option<SimpleStatementLine<'a>> {
|
||||
// println!("{:?}", node);
|
||||
let mut body: Vec<SmallStatement> = vec![];
|
||||
for node in &node.body {
|
||||
body.push(visitor.visit_SmallStatement(node).clone());
|
||||
|
@ -424,13 +383,10 @@ where
|
|||
transformed.body = body;
|
||||
Some(transformed)
|
||||
}
|
||||
pub fn walk_CompoundStatement<'a, 'b, V: CSTVisitor + ?Sized>(
|
||||
visitor: &'b mut V,
|
||||
pub fn walk_CompoundStatement<'a, V: CSTVisitor + ?Sized>(
|
||||
visitor: &mut V,
|
||||
node: &'a CompoundStatement<'a>,
|
||||
) -> Option<CompoundStatement<'a>>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
) -> Option<CompoundStatement<'a>> {
|
||||
match node {
|
||||
CompoundStatement::If(node) => {
|
||||
visitor.visit_If(node);
|
||||
|
@ -450,13 +406,7 @@ where
|
|||
|
||||
Some(node.clone())
|
||||
}
|
||||
pub fn walk_SmallStatement<'a, 'b, V: CSTVisitor + ?Sized>(
|
||||
visitor: &'b mut V,
|
||||
node: &'a SmallStatement<'a>,
|
||||
) -> SmallStatement<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
pub fn walk_SmallStatement<'a, V: CSTVisitor + ?Sized>(visitor: &mut V, node: &'a SmallStatement<'a>) -> SmallStatement<'a> {
|
||||
match node {
|
||||
SmallStatement::Pass(node) => visitor.visit_Pass(node),
|
||||
SmallStatement::Break(node) => visitor.visit_Break(node),
|
||||
|
@ -467,9 +417,7 @@ where
|
|||
SmallStatement::Import(node) => visitor.visit_Import(node),
|
||||
SmallStatement::ImportFrom(node) => visitor.visit_ImportFrom(node),
|
||||
SmallStatement::Assign(node) => visitor.visit_Assign(node),
|
||||
SmallStatement::AnnAssign(node) => {
|
||||
return SmallStatement::AnnAssign(visitor.visit_AnnAssign(node));
|
||||
}
|
||||
SmallStatement::AnnAssign(node) => { return SmallStatement::AnnAssign(visitor.visit_AnnAssign(node)); },
|
||||
SmallStatement::Raise(node) => visitor.visit_Raise(node),
|
||||
SmallStatement::Global(node) => visitor.visit_Global(node),
|
||||
SmallStatement::Nonlocal(node) => visitor.visit_Nonlocal(node),
|
||||
|
@ -479,8 +427,8 @@ where
|
|||
|
||||
node.clone()
|
||||
}
|
||||
pub fn walk_Expression<'a, 'b, V: CSTVisitor + ?Sized>(
|
||||
visitor: &'b mut V,
|
||||
pub fn walk_Expression<'a, V: CSTVisitor + ?Sized>(
|
||||
visitor: &mut V,
|
||||
node: &'a Expression<'a>,
|
||||
) -> Expression<'a> {
|
||||
match node {
|
||||
|
@ -491,9 +439,7 @@ pub fn walk_Expression<'a, 'b, V: CSTVisitor + ?Sized>(
|
|||
Expression::Imaginary(node) => visitor.visit_Imaginary(node),
|
||||
Expression::Comparison(node) => visitor.visit_Comparison(node),
|
||||
Expression::UnaryOperation(node) => visitor.visit_UnaryOperation(node),
|
||||
Expression::BinaryOperation(node) => {
|
||||
return Expression::BinaryOperation(Box::new(visitor.visit_BinaryOperation(node)))
|
||||
}
|
||||
Expression::BinaryOperation(node) => return Expression::BinaryOperation(Box::new(visitor.visit_BinaryOperation(node))),
|
||||
Expression::BooleanOperation(node) => visitor.visit_BooleanOperation(node),
|
||||
Expression::Attribute(node) => visitor.visit_Attribute(node),
|
||||
Expression::Tuple(node) => visitor.visit_Tuple(node),
|
||||
|
@ -538,20 +484,14 @@ pub fn walk_AssignTargetExpression<V: CSTVisitor + ?Sized>(
|
|||
AssignTargetExpression::Subscript(node) => visitor.visit_Subscript(node),
|
||||
}
|
||||
}
|
||||
pub fn walk_AnnAssign<'a, 'b, V: CSTVisitor + ?Sized>(
|
||||
visitor: &'b mut V,
|
||||
node: &'a AnnAssign<'a>,
|
||||
) -> AnnAssign<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
println!("walk_AnnAssign");
|
||||
pub fn walk_AnnAssign<'a, V: CSTVisitor + ?Sized>(visitor: &mut V, node: &'a AnnAssign<'a>) -> AnnAssign<'a> {
|
||||
// println!("walk_AnnAssign");
|
||||
let mut transformed: AnnAssign<'a> = node.clone();
|
||||
visitor.visit_AssignTargetExpression(&node.target);
|
||||
if let Some(node) = &node.value {
|
||||
println!("Before: {:?}", node);
|
||||
// println!("Before: {:?}", node);
|
||||
transformed.value = Some(visitor.visit_Expression(node));
|
||||
println!("After: {:?}", transformed.value);
|
||||
// println!("After: {:?}", transformed.value);
|
||||
}
|
||||
transformed
|
||||
}
|
||||
|
@ -595,13 +535,7 @@ pub fn walk_AugAssign<V: CSTVisitor + ?Sized>(visitor: &mut V, node: &AugAssign)
|
|||
pub fn walk_Await<V: CSTVisitor + ?Sized>(visitor: &mut V, node: &Await) {
|
||||
visitor.visit_Expression(&node.expression);
|
||||
}
|
||||
pub fn walk_BinaryOperation<'a, 'b, V: CSTVisitor + ?Sized>(
|
||||
visitor: &'b mut V,
|
||||
node: &'a BinaryOperation<'a>,
|
||||
) -> BinaryOperation<'a>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
pub fn walk_BinaryOperation<'a, V: CSTVisitor + ?Sized>(visitor: &mut V, node: &'a BinaryOperation<'a>) -> BinaryOperation<'a> {
|
||||
let mut transformed: BinaryOperation = node.clone();
|
||||
|
||||
transformed.left = Box::new(visitor.visit_Expression(&node.left));
|
||||
|
@ -610,9 +544,8 @@ where
|
|||
|
||||
transformed
|
||||
}
|
||||
pub fn walk_BinaryOp<'a, 'b, V: CSTVisitor + ?Sized>(visitor: &'b mut V, node: &'a BinaryOp<'a>) -> BinaryOp<'a> {
|
||||
pub fn walk_BinaryOp<V: CSTVisitor + ?Sized>(visitor: &mut V, node: &BinaryOp) {
|
||||
// Nothing to do.
|
||||
node.clone()
|
||||
}
|
||||
pub fn walk_BooleanOperation<V: CSTVisitor + ?Sized>(visitor: &mut V, node: &BooleanOperation) {
|
||||
visitor.visit_Expression(&node.left);
|
||||
|
@ -726,12 +659,8 @@ pub fn walk_DictElement<V: CSTVisitor + ?Sized>(visitor: &mut V, node: &DictElem
|
|||
}
|
||||
pub fn walk_Element<V: CSTVisitor + ?Sized>(visitor: &mut V, node: &Element) {
|
||||
match node {
|
||||
Element::Simple { value: node, .. } => {
|
||||
visitor.visit_Expression(node);
|
||||
}
|
||||
Element::Starred(node) => {
|
||||
visitor.visit_StarredElement(node);
|
||||
}
|
||||
Element::Simple { value: node, .. } => { visitor.visit_Expression(node); },
|
||||
Element::Starred(node) => { visitor.visit_StarredElement(node); },
|
||||
};
|
||||
}
|
||||
pub fn walk_Ellipsis<V: CSTVisitor + ?Sized>(visitor: &mut V, node: &Ellipsis) {
|
||||
|
@ -1104,11 +1033,7 @@ pub fn walk_Yield<V: CSTVisitor + ?Sized>(visitor: &mut V, node: &Yield) {
|
|||
}
|
||||
pub fn walk_YieldValue<V: CSTVisitor + ?Sized>(visitor: &mut V, node: &YieldValue) {
|
||||
match node {
|
||||
YieldValue::Expression(node) => {
|
||||
visitor.visit_Expression(node);
|
||||
}
|
||||
YieldValue::From(node) => {
|
||||
visitor.visit_From(node);
|
||||
}
|
||||
YieldValue::Expression(node) => { visitor.visit_Expression(node); }
|
||||
YieldValue::From(node) => { visitor.visit_From(node); }
|
||||
};
|
||||
}
|
||||
|
|
|
@ -10,11 +10,11 @@ use crate::settings::Settings;
|
|||
use crate::{cache, fs};
|
||||
|
||||
pub fn check_path(path: &Path, settings: &Settings, mode: &cache::Mode) -> Result<Vec<Message>> {
|
||||
// Check the cache.
|
||||
if let Some(messages) = cache::get(path, settings, mode) {
|
||||
debug!("Cache hit for: {}", path.to_string_lossy());
|
||||
return Ok(messages);
|
||||
}
|
||||
// // Check the cache.
|
||||
// if let Some(messages) = cache::get(path, settings, mode) {
|
||||
// debug!("Cache hit for: {}", path.to_string_lossy());
|
||||
// return Ok(messages);
|
||||
// }
|
||||
|
||||
// Read the file from disk.
|
||||
let contents = fs::read_file(path)?;
|
||||
|
@ -23,11 +23,15 @@ pub fn check_path(path: &Path, settings: &Settings, mode: &cache::Mode) -> Resul
|
|||
let mut checks: Vec<Check> = vec![];
|
||||
|
||||
// Run the CST-based checks.
|
||||
let python_cst = match libcst_native::parse_module(&contents, None) {
|
||||
let _ = match libcst_native::parse_module(&contents, None) {
|
||||
Ok(m) => m,
|
||||
Err(e) => panic!("Failed to parse CST."),
|
||||
Err(e) => {
|
||||
return Err(anyhow::anyhow!("Failed to parse"));
|
||||
}
|
||||
};
|
||||
checks.extend(check_cst(&python_cst, settings));
|
||||
|
||||
Ok(vec![])
|
||||
// checks.extend(check_cst(&python_cst, settings));
|
||||
|
||||
// // Run the AST-based checks.
|
||||
// if settings
|
||||
|
@ -42,20 +46,20 @@ pub fn check_path(path: &Path, settings: &Settings, mode: &cache::Mode) -> Resul
|
|||
//
|
||||
// // Run the lines-based checks.
|
||||
// check_lines(&mut checks, &contents, settings);
|
||||
|
||||
// Convert to messages.
|
||||
let messages: Vec<Message> = checks
|
||||
.into_iter()
|
||||
.map(|check| Message {
|
||||
kind: check.kind,
|
||||
location: check.location,
|
||||
filename: path.to_string_lossy().to_string(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
cache::set(path, settings, &messages, mode);
|
||||
|
||||
Ok(messages)
|
||||
//
|
||||
// // Convert to messages.
|
||||
// let messages: Vec<Message> = checks
|
||||
// .into_iter()
|
||||
// .map(|check| Message {
|
||||
// kind: check.kind,
|
||||
// location: check.location,
|
||||
// filename: path.to_string_lossy().to_string(),
|
||||
// })
|
||||
// .collect();
|
||||
//
|
||||
// cache::set(path, settings, &messages, mode);
|
||||
//
|
||||
// Ok(messages)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue