diff --git a/Cargo.lock b/Cargo.lock index 62ddd69515..b2eb7daa03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2105,7 +2105,7 @@ dependencies = [ [[package]] name = "ruff_text_size" version = "0.0.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=8d74eee75031b68d2204219963fae54a3f31a394#8d74eee75031b68d2204219963fae54a3f31a394" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=ed3b4eb72b6e497bbdb4d19dec6621074d724130#ed3b4eb72b6e497bbdb4d19dec6621074d724130" dependencies = [ "schemars", "serde", @@ -2183,7 +2183,7 @@ dependencies = [ [[package]] name = "rustpython-ast" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=8d74eee75031b68d2204219963fae54a3f31a394#8d74eee75031b68d2204219963fae54a3f31a394" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=ed3b4eb72b6e497bbdb4d19dec6621074d724130#ed3b4eb72b6e497bbdb4d19dec6621074d724130" dependencies = [ "is-macro", "num-bigint", @@ -2194,7 +2194,7 @@ dependencies = [ [[package]] name = "rustpython-format" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=8d74eee75031b68d2204219963fae54a3f31a394#8d74eee75031b68d2204219963fae54a3f31a394" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=ed3b4eb72b6e497bbdb4d19dec6621074d724130#ed3b4eb72b6e497bbdb4d19dec6621074d724130" dependencies = [ "bitflags 2.3.1", "itertools", @@ -2206,7 +2206,7 @@ dependencies = [ [[package]] name = "rustpython-literal" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=8d74eee75031b68d2204219963fae54a3f31a394#8d74eee75031b68d2204219963fae54a3f31a394" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=ed3b4eb72b6e497bbdb4d19dec6621074d724130#ed3b4eb72b6e497bbdb4d19dec6621074d724130" dependencies = [ "hexf-parse", "is-macro", @@ -2218,7 +2218,7 @@ dependencies = [ [[package]] name = "rustpython-parser" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=8d74eee75031b68d2204219963fae54a3f31a394#8d74eee75031b68d2204219963fae54a3f31a394" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=ed3b4eb72b6e497bbdb4d19dec6621074d724130#ed3b4eb72b6e497bbdb4d19dec6621074d724130" dependencies = [ "anyhow", "is-macro", @@ -2241,7 +2241,7 @@ dependencies = [ [[package]] name = "rustpython-parser-core" version = "0.2.0" -source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=8d74eee75031b68d2204219963fae54a3f31a394#8d74eee75031b68d2204219963fae54a3f31a394" +source = "git+https://github.com/astral-sh/RustPython-Parser.git?rev=ed3b4eb72b6e497bbdb4d19dec6621074d724130#ed3b4eb72b6e497bbdb4d19dec6621074d724130" dependencies = [ "is-macro", "memchr", diff --git a/Cargo.toml b/Cargo.toml index 23414c705f..7a9e8d929b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ ignore = { version = "0.4.20" } insta = { version = "1.28.0" } is-macro = { version = "0.2.2" } itertools = { version = "0.10.5" } -libcst = { git = "https://github.com/charliermarsh/LibCST", rev = "80e4c1399f95e5beb532fdd1e209ad2dbb470438" } log = { version = "0.4.17" } memchr = "2.5.0" nohash-hasher = { version = "0.2.0" } @@ -36,11 +35,6 @@ proc-macro2 = { version = "1.0.51" } quote = { version = "1.0.23" } regex = { version = "1.7.1" } rustc-hash = { version = "1.1.0" } -ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394" } -rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394" , default-features = false, features = ["all-nodes-with-ranges", "num-bigint"]} -rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394", default-features = false, features = ["num-bigint"] } -rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394" } -rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394" , default-features = false, features = ["full-lexer", "all-nodes-with-ranges", "num-bigint"] } schemars = { version = "0.8.12" } serde = { version = "1.0.152", features = ["derive"] } serde_json = { version = "1.0.93" } @@ -53,6 +47,19 @@ syn = { version = "2.0.15" } test-case = { version = "3.0.0" } toml = { version = "0.7.2" } +# v0.0.1 +libcst = { git = "https://github.com/charliermarsh/LibCST", rev = "80e4c1399f95e5beb532fdd1e209ad2dbb470438" } +# v0.0.3 +ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "ed3b4eb72b6e497bbdb4d19dec6621074d724130" } +# v0.0.3 +rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "ed3b4eb72b6e497bbdb4d19dec6621074d724130" , default-features = false, features = ["all-nodes-with-ranges", "num-bigint"]} +# v0.0.3 +rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "ed3b4eb72b6e497bbdb4d19dec6621074d724130", default-features = false, features = ["num-bigint"] } +# v0.0.3 +rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "ed3b4eb72b6e497bbdb4d19dec6621074d724130", default-features = false } +# v0.0.3 +rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "ed3b4eb72b6e497bbdb4d19dec6621074d724130" , default-features = false, features = ["full-lexer", "all-nodes-with-ranges", "num-bigint"] } + [profile.release] lto = "fat" codegen-units = 1 diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 155d2f8c1f..54948a23cd 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -825,7 +825,7 @@ where if alias .asname .as_ref() - .map_or(false, |asname| asname == &alias.name) + .map_or(false, |asname| asname.as_str() == alias.name.as_str()) { flags |= BindingFlags::EXPLICIT_EXPORT; } @@ -1110,7 +1110,7 @@ where if alias .asname .as_ref() - .map_or(false, |asname| asname == &alias.name) + .map_or(false, |asname| asname.as_str() == alias.name.as_str()) { flags |= BindingFlags::EXPLICIT_EXPORT; } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/getattr_with_constant.rs b/crates/ruff/src/rules/flake8_bugbear/rules/getattr_with_constant.rs index ff57ce600f..29a667eb15 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/getattr_with_constant.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/getattr_with_constant.rs @@ -1,5 +1,5 @@ use ruff_text_size::TextRange; -use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Ranged}; +use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Identifier, Ranged}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; @@ -27,7 +27,7 @@ impl AlwaysAutofixableViolation for GetAttrWithConstant { fn attribute(value: &Expr, attr: &str) -> Expr { ast::ExprAttribute { value: Box::new(value.clone()), - attr: attr.into(), + attr: Identifier::new(attr.to_string(), TextRange::default()), ctx: ExprContext::Load, range: TextRange::default(), } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/setattr_with_constant.rs b/crates/ruff/src/rules/flake8_bugbear/rules/setattr_with_constant.rs index 4866a9ae7c..0708af6b87 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/setattr_with_constant.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/setattr_with_constant.rs @@ -1,5 +1,5 @@ use ruff_text_size::TextRange; -use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Ranged, Stmt}; +use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Identifier, Ranged, Stmt}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; @@ -30,7 +30,7 @@ fn assignment(obj: &Expr, name: &str, value: &Expr, generator: Generator) -> Str let stmt = Stmt::Assign(ast::StmtAssign { targets: vec![Expr::Attribute(ast::ExprAttribute { value: Box::new(obj.clone()), - attr: name.into(), + attr: Identifier::new(name.to_string(), TextRange::default()), ctx: ExprContext::Store, range: TextRange::default(), })], diff --git a/crates/ruff/src/rules/flake8_gettext/rules/is_gettext_func_call.rs b/crates/ruff/src/rules/flake8_gettext/rules/is_gettext_func_call.rs index 0e539ead9a..e2f78c4476 100644 --- a/crates/ruff/src/rules/flake8_gettext/rules/is_gettext_func_call.rs +++ b/crates/ruff/src/rules/flake8_gettext/rules/is_gettext_func_call.rs @@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Expr}; /// Returns true if the [`Expr`] is an internationalization function call. pub(crate) fn is_gettext_func_call(func: &Expr, functions_names: &[String]) -> bool { if let Expr::Name(ast::ExprName { id, .. }) = func { - functions_names.contains(id.as_ref()) + functions_names.contains(id) } else { false } diff --git a/crates/ruff/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs b/crates/ruff/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs index 708df4e241..2905bf8d34 100644 --- a/crates/ruff/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs +++ b/crates/ruff/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs @@ -5,7 +5,7 @@ use itertools::Either::{Left, Right}; use ruff_text_size::TextRange; -use rustpython_parser::ast::{self, BoolOp, Expr, ExprContext, Ranged}; +use rustpython_parser::ast::{self, BoolOp, Expr, ExprContext, Identifier, Ranged}; use ruff_diagnostics::AlwaysAutofixableViolation; use ruff_diagnostics::{Diagnostic, Edit, Fix}; @@ -140,7 +140,7 @@ pub(crate) fn multiple_starts_ends_with(checker: &mut Checker, expr: &Expr) { }); let node2 = Expr::Attribute(ast::ExprAttribute { value: Box::new(node1), - attr: attr_name.into(), + attr: Identifier::new(attr_name.to_string(), TextRange::default()), ctx: ExprContext::Load, range: TextRange::default(), }); diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs index 78296a99e4..74e13767a7 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs @@ -1,4 +1,4 @@ -use rustpython_parser::ast::{self, Expr, Identifier, Keyword, Ranged, Stmt, WithItem}; +use rustpython_parser::ast::{self, Expr, Keyword, Ranged, Stmt, WithItem}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -74,9 +74,12 @@ pub(crate) fn raises_call(checker: &mut Checker, func: &Expr, args: &[Expr], key } if checker.enabled(Rule::PytestRaisesTooBroad) { - let match_keyword = keywords - .iter() - .find(|kw| kw.arg == Some(Identifier::new("match"))); + let match_keyword = keywords.iter().find(|keyword| { + keyword + .arg + .as_ref() + .map_or(false, |arg| arg.as_str() == "match") + }); if let Some(exception) = args.first() { if let Some(match_keyword) = match_keyword { diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/unittest_assert.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/unittest_assert.rs index 0c032ce84c..106bf9ec51 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/unittest_assert.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/unittest_assert.rs @@ -3,7 +3,9 @@ use std::hash::BuildHasherDefault; use anyhow::{anyhow, bail, Result}; use ruff_text_size::TextRange; use rustc_hash::FxHashMap; -use rustpython_parser::ast::{self, CmpOp, Constant, Expr, ExprContext, Keyword, Stmt, UnaryOp}; +use rustpython_parser::ast::{ + self, CmpOp, Constant, Expr, ExprContext, Identifier, Keyword, Stmt, UnaryOp, +}; /// An enum to represent the different types of assertions present in the /// `unittest` module. Note: any variants that can't be replaced with plain @@ -388,7 +390,7 @@ impl UnittestAssert { }; let node1 = ast::ExprAttribute { value: Box::new(node.into()), - attr: "search".into(), + attr: Identifier::new("search".to_string(), TextRange::default()), ctx: ExprContext::Load, range: TextRange::default(), }; diff --git a/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs b/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs index 3b5d4a4bb9..8a3105919d 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs @@ -1,7 +1,7 @@ use log::error; use ruff_text_size::TextRange; use rustc_hash::FxHashSet; -use rustpython_parser::ast::{self, CmpOp, Constant, Expr, ExprContext, Ranged, Stmt}; +use rustpython_parser::ast::{self, CmpOp, Constant, Expr, ExprContext, Identifier, Ranged, Stmt}; use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -941,7 +941,7 @@ pub(crate) fn use_dict_get_with_default( let node1 = *test_key.clone(); let node2 = ast::ExprAttribute { value: expected_subscript.clone(), - attr: "get".into(), + attr: Identifier::new("get".to_string(), TextRange::default()), ctx: ExprContext::Load, range: TextRange::default(), }; diff --git a/crates/ruff/src/rules/flake8_tidy_imports/rules/relative_imports.rs b/crates/ruff/src/rules/flake8_tidy_imports/rules/relative_imports.rs index 6b8227be28..5012d69950 100644 --- a/crates/ruff/src/rules/flake8_tidy_imports/rules/relative_imports.rs +++ b/crates/ruff/src/rules/flake8_tidy_imports/rules/relative_imports.rs @@ -1,5 +1,5 @@ use ruff_text_size::TextRange; -use rustpython_parser::ast::{self, Int, Ranged, Stmt}; +use rustpython_parser::ast::{self, Identifier, Int, Ranged, Stmt}; use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -94,7 +94,10 @@ fn fix_banned_relative_import( panic!("Expected Stmt::ImportFrom"); }; let node = ast::StmtImportFrom { - module: Some(module_path.to_string().into()), + module: Some(Identifier::new( + module_path.to_string(), + TextRange::default(), + )), names: names.clone(), level: Some(Int::new(0)), range: TextRange::default(), diff --git a/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs b/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs index 9d65aa6692..8cc70a26c9 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs @@ -1,5 +1,7 @@ use ruff_text_size::TextRange; -use rustpython_parser::ast::{self, Arg, ArgWithDefault, Arguments, Constant, Expr, Ranged, Stmt}; +use rustpython_parser::ast::{ + self, Arg, ArgWithDefault, Arguments, Constant, Expr, Identifier, Ranged, Stmt, +}; use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -201,7 +203,7 @@ fn function( }) .collect::>(); let func = Stmt::FunctionDef(ast::StmtFunctionDef { - name: name.into(), + name: Identifier::new(name.to_string(), TextRange::default()), args: Box::new(Arguments { posonlyargs: new_posonlyargs, args: new_args, @@ -217,7 +219,7 @@ fn function( } } let func = Stmt::FunctionDef(ast::StmtFunctionDef { - name: name.into(), + name: Identifier::new(name.to_string(), TextRange::default()), args: Box::new(args.clone()), body: vec![body], decorator_list: vec![], diff --git a/crates/ruff/src/rules/pylint/rules/duplicate_bases.rs b/crates/ruff/src/rules/pylint/rules/duplicate_bases.rs index bd5a6b5c25..860e393565 100644 --- a/crates/ruff/src/rules/pylint/rules/duplicate_bases.rs +++ b/crates/ruff/src/rules/pylint/rules/duplicate_bases.rs @@ -1,7 +1,7 @@ use std::hash::BuildHasherDefault; use rustc_hash::FxHashSet; -use rustpython_parser::ast::{self, Expr, Identifier, Ranged}; +use rustpython_parser::ast::{self, Expr, Ranged}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -52,7 +52,7 @@ impl Violation for DuplicateBases { /// PLE0241 pub(crate) fn duplicate_bases(checker: &mut Checker, name: &str, bases: &[Expr]) { - let mut seen: FxHashSet<&Identifier> = + let mut seen: FxHashSet<&str> = FxHashSet::with_capacity_and_hasher(bases.len(), BuildHasherDefault::default()); for base in bases { if let Expr::Name(ast::ExprName { id, .. }) = base { diff --git a/crates/ruff/src/rules/pylint/rules/manual_import_from.rs b/crates/ruff/src/rules/pylint/rules/manual_import_from.rs index bac9cf746b..0343cd1eb7 100644 --- a/crates/ruff/src/rules/pylint/rules/manual_import_from.rs +++ b/crates/ruff/src/rules/pylint/rules/manual_import_from.rs @@ -1,5 +1,5 @@ use ruff_text_size::TextRange; -use rustpython_parser::ast::{self, Alias, Int, Ranged, Stmt}; +use rustpython_parser::ast::{self, Alias, Identifier, Int, Ranged, Stmt}; use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -74,7 +74,7 @@ pub(crate) fn manual_from_import( if checker.patch(diagnostic.kind.rule()) { if names.len() == 1 { let node = ast::StmtImportFrom { - module: Some(module.into()), + module: Some(Identifier::new(module.to_string(), TextRange::default())), names: vec![Alias { name: asname.clone(), asname: None, diff --git a/crates/ruff/src/rules/pylint/rules/useless_import_alias.rs b/crates/ruff/src/rules/pylint/rules/useless_import_alias.rs index 7c657a5840..708de5926b 100644 --- a/crates/ruff/src/rules/pylint/rules/useless_import_alias.rs +++ b/crates/ruff/src/rules/pylint/rules/useless_import_alias.rs @@ -43,7 +43,7 @@ pub(crate) fn useless_import_alias(checker: &mut Checker, alias: &Alias) { if alias.name.contains('.') { return; } - if &alias.name != asname { + if alias.name.as_str() != asname.as_str() { return; } diff --git a/crates/ruff/src/rules/pyupgrade/rules/convert_named_tuple_functional_to_class.rs b/crates/ruff/src/rules/pyupgrade/rules/convert_named_tuple_functional_to_class.rs index 9e83d864cc..284342608f 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/convert_named_tuple_functional_to_class.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/convert_named_tuple_functional_to_class.rs @@ -1,7 +1,9 @@ use anyhow::{bail, Result}; use log::debug; use ruff_text_size::TextRange; -use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Keyword, Ranged, Stmt}; +use rustpython_parser::ast::{ + self, Constant, Expr, ExprContext, Identifier, Keyword, Ranged, Stmt, +}; use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -185,7 +187,7 @@ fn create_properties_from_args(args: &[Expr], defaults: &[Expr]) -> Result, base_class: &Expr) -> Stmt { ast::StmtClassDef { - name: typename.into(), + name: Identifier::new(typename.to_string(), TextRange::default()), bases: vec![base_class.clone()], keywords: vec![], body, diff --git a/crates/ruff/src/rules/pyupgrade/rules/convert_typed_dict_functional_to_class.rs b/crates/ruff/src/rules/pyupgrade/rules/convert_typed_dict_functional_to_class.rs index c1d2f5c58d..2c7f2827af 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/convert_typed_dict_functional_to_class.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/convert_typed_dict_functional_to_class.rs @@ -1,7 +1,9 @@ use anyhow::{bail, Result}; use log::debug; use ruff_text_size::TextRange; -use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Keyword, Ranged, Stmt}; +use rustpython_parser::ast::{ + self, Constant, Expr, ExprContext, Identifier, Keyword, Ranged, Stmt, +}; use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -120,7 +122,7 @@ fn create_class_def_stmt( None => vec![], }; ast::StmtClassDef { - name: class_name.into(), + name: Identifier::new(class_name.to_string(), TextRange::default()), bases: vec![base_class.clone()], keywords, body, diff --git a/crates/ruff/src/rules/pyupgrade/rules/super_call_with_parameters.rs b/crates/ruff/src/rules/pyupgrade/rules/super_call_with_parameters.rs index cdde59a7e0..fab3d45868 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/super_call_with_parameters.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/super_call_with_parameters.rs @@ -129,7 +129,7 @@ pub(crate) fn super_call_with_parameters( return; }; - if !(first_arg_id == parent_name && second_arg_id == parent_arg) { + if !(first_arg_id == parent_name.as_str() && second_arg_id == parent_arg.as_str()) { return; } diff --git a/crates/ruff/src/rules/ruff/rules/implicit_optional.rs b/crates/ruff/src/rules/ruff/rules/implicit_optional.rs index 105d076112..2cc6f1dbc0 100644 --- a/crates/ruff/src/rules/ruff/rules/implicit_optional.rs +++ b/crates/ruff/src/rules/ruff/rules/implicit_optional.rs @@ -289,7 +289,7 @@ fn generate_fix(checker: &Checker, conversion_type: ConversionType, expr: &Expr) let new_expr = Expr::Subscript(ast::ExprSubscript { range: TextRange::default(), value: Box::new(Expr::Name(ast::ExprName { - id: binding.into(), + id: binding, ctx: ast::ExprContext::Store, range: TextRange::default(), })), diff --git a/crates/ruff/src/rules/tryceratops/rules/useless_try_except.rs b/crates/ruff/src/rules/tryceratops/rules/useless_try_except.rs index c2620b9515..db26e7588f 100644 --- a/crates/ruff/src/rules/tryceratops/rules/useless_try_except.rs +++ b/crates/ruff/src/rules/tryceratops/rules/useless_try_except.rs @@ -50,7 +50,7 @@ pub(crate) fn useless_try_except(checker: &mut Checker, handlers: &[ExceptHandle if let Some(expr) = exc { // E.g., `except ... as e: raise e` if let Expr::Name(ast::ExprName { id, .. }) = expr.as_ref() { - if Some(id) == name.as_ref() { + if name.as_ref().map_or(false, |name| name.as_str() == id) { return Some(Diagnostic::new(UselessTryExcept, handler.range())); } } diff --git a/crates/ruff/src/rules/tryceratops/rules/verbose_log_message.rs b/crates/ruff/src/rules/tryceratops/rules/verbose_log_message.rs index 4852bac33f..da2652d9ed 100644 --- a/crates/ruff/src/rules/tryceratops/rules/verbose_log_message.rs +++ b/crates/ruff/src/rules/tryceratops/rules/verbose_log_message.rs @@ -86,7 +86,7 @@ pub(crate) fn verbose_log_message(checker: &mut Checker, handlers: &[ExceptHandl names }; for expr in names { - if expr.id == *target { + if expr.id == target.as_str() { checker .diagnostics .push(Diagnostic::new(VerboseLogMessage, expr.range())); diff --git a/crates/ruff/src/rules/tryceratops/rules/verbose_raise.rs b/crates/ruff/src/rules/tryceratops/rules/verbose_raise.rs index 56123b43ad..c2829ca707 100644 --- a/crates/ruff/src/rules/tryceratops/rules/verbose_raise.rs +++ b/crates/ruff/src/rules/tryceratops/rules/verbose_raise.rs @@ -95,7 +95,7 @@ pub(crate) fn verbose_raise(checker: &mut Checker, handlers: &[ExceptHandler]) { if let Some(exc) = exc { // ...and the raised object is bound to the same name... if let Expr::Name(ast::ExprName { id, .. }) = exc { - if id == exception_name { + if id == exception_name.as_str() { checker .diagnostics .push(Diagnostic::new(VerboseRaise, exc.range())); diff --git a/crates/ruff_python_ast/src/source_code/generator.rs b/crates/ruff_python_ast/src/source_code/generator.rs index 08d552f8ec..0b3ec2f5d5 100644 --- a/crates/ruff_python_ast/src/source_code/generator.rs +++ b/crates/ruff_python_ast/src/source_code/generator.rs @@ -1191,7 +1191,7 @@ impl<'a> Generator<'a> { self.p("*"); self.unparse_expr(value, precedence::MAX); } - Expr::Name(ast::ExprName { id, .. }) => self.p_id(id), + Expr::Name(ast::ExprName { id, .. }) => self.p(id.as_str()), Expr::List(ast::ExprList { elts, .. }) => { self.p("["); let mut first = true; diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 3c6742508f..eb845933af 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -1,13 +1,16 @@ -use crate::comments::visitor::{CommentPlacement, DecoratedComment}; -use crate::comments::CommentTextPosition; -use crate::trivia::{SimpleTokenizer, Token, TokenKind}; +use std::cmp::Ordering; + +use ruff_text_size::{TextRange, TextSize}; +use rustpython_parser::ast::Ranged; + use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::source_code::Locator; use ruff_python_ast::whitespace; use ruff_python_whitespace::{PythonWhitespace, UniversalNewlines}; -use ruff_text_size::{TextRange, TextSize}; -use rustpython_parser::ast::Ranged; -use std::cmp::Ordering; + +use crate::comments::visitor::{CommentPlacement, DecoratedComment}; +use crate::comments::CommentTextPosition; +use crate::trivia::{SimpleTokenizer, Token, TokenKind}; /// Implements the custom comment placement logic. pub(super) fn place_comment<'a>( @@ -623,8 +626,13 @@ fn handle_positional_only_arguments_separator_comment<'a>( return CommentPlacement::Default(comment); }; - let is_last_positional_argument = are_same_optional(last_argument_or_default, arguments.posonlyargs.last()) - // If the preceding node is the default for the last positional argument + let is_last_positional_argument = + // If the preceding node is the identifier for the last positional argument (`a`). + // ```python + // def test(a, /, b): pass + // ``` + are_same_optional(last_argument_or_default, arguments.posonlyargs.last().map(|arg| &arg.def)) + // If the preceding node is the default for the last positional argument (`10`). // ```python // def test(a=10, /, b): pass // ```