From d6a20838a2147f7beaf8bb0810a44950534d2cfb Mon Sep 17 00:00:00 2001 From: LaBatata101 Date: Sun, 26 Nov 2023 16:56:53 -0300 Subject: [PATCH 1/6] Replace `String` with `SmolStr` for nodes `Identifier` and `ExprName` --- Cargo.lock | 11 +++++++ Cargo.toml | 1 + crates/ruff_linter/src/checkers/imports.rs | 2 +- .../rules/airflow/rules/task_variable_name.rs | 2 +- .../src/rules/flake8_gettext/mod.rs | 2 +- .../rules/unnecessary_literal_union.rs | 2 +- .../rules/unnecessary_type_union.rs | 2 +- .../rules/private_member_access.rs | 2 +- .../src/rules/pyflakes/rules/strings.rs | 2 +- .../rules/pylint/rules/no_method_decorator.rs | 2 +- .../pyupgrade/rules/use_pep695_type_alias.rs | 7 +++- .../ruff_linter/src/rules/refurb/helpers.rs | 4 +-- .../src/rules/refurb/rules/read_whole_file.rs | 2 +- .../refurb/rules/reimplemented_starmap.rs | 4 +-- .../refurb/rules/unnecessary_enumerate.rs | 6 ++-- .../src/rules/ruff/rules/implicit_optional.rs | 2 +- crates/ruff_python_ast/Cargo.toml | 1 + crates/ruff_python_ast/src/helpers.rs | 6 ++-- crates/ruff_python_ast/src/nodes.rs | 26 +++++++++------ crates/ruff_python_parser/Cargo.toml | 1 + crates/ruff_python_parser/src/lexer.rs | 3 +- crates/ruff_python_parser/src/python.lalrpop | 5 +-- crates/ruff_python_parser/src/python.rs | 32 ++++++++++--------- .../ruff_python_parser/src/soft_keywords.rs | 4 +-- crates/ruff_python_parser/src/token.rs | 3 +- 25 files changed, 82 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87a42ff98d0d9..ab4effd67026f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2310,6 +2310,7 @@ dependencies = [ "rustc-hash", "serde", "smallvec", + "smol_str", "static_assertions", ] @@ -2398,6 +2399,7 @@ dependencies = [ "ruff_python_ast", "ruff_text_size", "rustc-hash", + "smol_str", "static_assertions", "tiny-keccak", "unicode-ident", @@ -2811,6 +2813,15 @@ version = "1.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" +[[package]] +name = "smol_str" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74212e6bbe9a4352329b2f68ba3130c15a3f26fe88ff22dbdc6cdd58fa85e99c" +dependencies = [ + "serde", +] + [[package]] name = "spin" version = "0.5.2" diff --git a/Cargo.toml b/Cargo.toml index 0c57416eaea86..94b8f27684be3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ serde_json = { version = "1.0.108" } shellexpand = { version = "3.0.0" } similar = { version = "2.3.0", features = ["inline"] } smallvec = { version = "1.11.2" } +smol_str = "0.2.0" static_assertions = "1.1.0" strum = { version = "0.25.0", features = ["strum_macros"] } strum_macros = { version = "0.25.3" } diff --git a/crates/ruff_linter/src/checkers/imports.rs b/crates/ruff_linter/src/checkers/imports.rs index 5ecb477d85e29..21a1d5ad9b34b 100644 --- a/crates/ruff_linter/src/checkers/imports.rs +++ b/crates/ruff_linter/src/checkers/imports.rs @@ -46,7 +46,7 @@ fn extract_import_map(path: &Path, package: Option<&Path>, blocks: &[&Block]) -> }) => { let level = level.unwrap_or_default() as usize; let module = if let Some(module) = module { - let module: &String = module.as_ref(); + let module = module.as_str(); if level == 0 { Cow::Borrowed(module) } else { diff --git a/crates/ruff_linter/src/rules/airflow/rules/task_variable_name.rs b/crates/ruff_linter/src/rules/airflow/rules/task_variable_name.rs index 76ebb405a225d..df9ed63eadcae 100644 --- a/crates/ruff_linter/src/rules/airflow/rules/task_variable_name.rs +++ b/crates/ruff_linter/src/rules/airflow/rules/task_variable_name.rs @@ -81,7 +81,7 @@ pub(crate) fn variable_name_task_id( let ast::ExprStringLiteral { value: task_id, .. } = keyword.value.as_string_literal_expr()?; // If the target name is the same as the task_id, no violation. - if task_id == id { + if task_id == id.as_str() { return None; } diff --git a/crates/ruff_linter/src/rules/flake8_gettext/mod.rs b/crates/ruff_linter/src/rules/flake8_gettext/mod.rs index eec91be60eb18..9476be96e11f9 100644 --- a/crates/ruff_linter/src/rules/flake8_gettext/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_gettext/mod.rs @@ -7,7 +7,7 @@ pub mod settings; /// 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) + functions_names.contains(&id.to_string()) } else { false } diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs index 7b56371e837c3..727bcc84c2b4e 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs @@ -80,7 +80,7 @@ fn make_literal_expr(subscript: Option, exprs: Vec<&Expr>) -> Expr { subscript.unwrap().clone() } else { Expr::Name(ast::ExprName { - id: "Literal".to_string(), + id: "Literal".into(), range: TextRange::default(), ctx: ast::ExprContext::Load, }) diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs index 6d74fffd5b74e..821a65ec8b0a6 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs @@ -133,7 +133,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr) .into_iter() .map(|type_member| { Expr::Name(ast::ExprName { - id: type_member, + id: type_member.into(), ctx: ExprContext::Load, range: TextRange::default(), }) diff --git a/crates/ruff_linter/src/rules/flake8_self/rules/private_member_access.rs b/crates/ruff_linter/src/rules/flake8_self/rules/private_member_access.rs index fdbf3e8def12b..6e68021274a34 100644 --- a/crates/ruff_linter/src/rules/flake8_self/rules/private_member_access.rs +++ b/crates/ruff_linter/src/rules/flake8_self/rules/private_member_access.rs @@ -77,7 +77,7 @@ pub(crate) fn private_member_access(checker: &mut Checker, expr: &Expr) { .settings .flake8_self .ignore_names - .contains(attr.as_ref()) + .contains(&attr.to_string()) { return; } diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs b/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs index e4545e139b79f..68438e18f2f49 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs @@ -747,7 +747,7 @@ pub(crate) fn string_dot_format_extra_named_arguments( let missing: Vec<(usize, &str)> = keywords .enumerate() .filter_map(|(index, keyword)| { - if summary.keywords.contains(keyword.as_ref()) { + if summary.keywords.contains(&keyword.to_string()) { None } else { Some((index, keyword.as_str())) diff --git a/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs b/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs index 60439119fd703..559d05879c555 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs @@ -131,7 +131,7 @@ fn get_undecorated_methods( if let Expr::Name(ast::ExprName { id, .. }) = &arguments.args[0] { if target_name == *id { - explicit_decorator_calls.insert(id.clone(), stmt.range()); + explicit_decorator_calls.insert(id.to_string(), stmt.range()); } }; } diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs index 547e40f7d87f1..f02e27063e420 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs @@ -92,7 +92,12 @@ pub(crate) fn non_pep695_type_alias(checker: &mut Checker, stmt: &StmtAnnAssign) // TODO(zanie): We should check for generic type variables used in the value and define them // as type params instead - let mut diagnostic = Diagnostic::new(NonPEP695TypeAlias { name: name.clone() }, stmt.range()); + let mut diagnostic = Diagnostic::new( + NonPEP695TypeAlias { + name: name.to_string(), + }, + stmt.range(), + ); let mut visitor = TypeVarReferenceVisitor { vars: vec![], semantic: checker.semantic(), diff --git a/crates/ruff_linter/src/rules/refurb/helpers.rs b/crates/ruff_linter/src/rules/refurb/helpers.rs index 031a58c62a437..f59154a7bf408 100644 --- a/crates/ruff_linter/src/rules/refurb/helpers.rs +++ b/crates/ruff_linter/src/rules/refurb/helpers.rs @@ -6,7 +6,7 @@ use ruff_text_size::TextRange; pub(super) fn generate_method_call(name: &str, method: &str, generator: Generator) -> String { // Construct `name`. let var = ast::ExprName { - id: name.to_string(), + id: name.into(), ctx: ast::ExprContext::Load, range: TextRange::default(), }; @@ -43,7 +43,7 @@ pub(super) fn generate_none_identity_comparison( ) -> String { // Construct `name`. let var = ast::ExprName { - id: name.to_string(), + id: name.into(), ctx: ast::ExprContext::Load, range: TextRange::default(), }; diff --git a/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs b/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs index 8bf1e6b6bccfb..d198035401c8a 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs @@ -315,7 +315,7 @@ fn make_suggestion(open: &FileOpen<'_>, generator: Generator) -> SourceCodeSnipp ReadMode::Bytes => "read_bytes", }; let name = ast::ExprName { - id: method_name.to_string(), + id: method_name.into(), ctx: ast::ExprContext::Load, range: TextRange::default(), }; diff --git a/crates/ruff_linter/src/rules/refurb/rules/reimplemented_starmap.rs b/crates/ruff_linter/src/rules/refurb/rules/reimplemented_starmap.rs index 8f380a187b952..b53b0dcf077e4 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/reimplemented_starmap.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/reimplemented_starmap.rs @@ -297,7 +297,7 @@ fn try_construct_call( /// Construct the call to `itertools.starmap` for suggestion. fn construct_starmap_call(starmap_binding: String, iter: &Expr, func: &Expr) -> ast::ExprCall { let starmap = ast::ExprName { - id: starmap_binding, + id: starmap_binding.into(), ctx: ast::ExprContext::Load, range: TextRange::default(), }; @@ -315,7 +315,7 @@ fn construct_starmap_call(starmap_binding: String, iter: &Expr, func: &Expr) -> /// Wrap given function call with yet another call. fn wrap_with_call_to(call: ast::ExprCall, func_name: &str) -> ast::ExprCall { let name = ast::ExprName { - id: func_name.to_string(), + id: func_name.into(), ctx: ast::ExprContext::Load, range: TextRange::default(), }; diff --git a/crates/ruff_linter/src/rules/refurb/rules/unnecessary_enumerate.rs b/crates/ruff_linter/src/rules/refurb/rules/unnecessary_enumerate.rs index 5d693dfc43894..f1115a60b958d 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/unnecessary_enumerate.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/unnecessary_enumerate.rs @@ -236,7 +236,7 @@ impl fmt::Display for EnumerateSubset { fn generate_range_len_call(name: &str, generator: Generator) -> String { // Construct `name`. let var = ast::ExprName { - id: name.to_string(), + id: name.into(), ctx: ast::ExprContext::Load, range: TextRange::default(), }; @@ -244,7 +244,7 @@ fn generate_range_len_call(name: &str, generator: Generator) -> String { let len = ast::ExprCall { func: Box::new( ast::ExprName { - id: "len".to_string(), + id: "len".into(), ctx: ast::ExprContext::Load, range: TextRange::default(), } @@ -261,7 +261,7 @@ fn generate_range_len_call(name: &str, generator: Generator) -> String { let range = ast::ExprCall { func: Box::new( ast::ExprName { - id: "range".to_string(), + id: "range".into(), ctx: ast::ExprContext::Load, range: TextRange::default(), } diff --git a/crates/ruff_linter/src/rules/ruff/rules/implicit_optional.rs b/crates/ruff_linter/src/rules/ruff/rules/implicit_optional.rs index c9e3b06e0df04..3298f8be85eb0 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/implicit_optional.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/implicit_optional.rs @@ -145,7 +145,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, + id: binding.into(), ctx: ast::ExprContext::Store, range: TextRange::default(), })), diff --git a/crates/ruff_python_ast/Cargo.toml b/crates/ruff_python_ast/Cargo.toml index 460bdf48c9bb0..856537b0b634e 100644 --- a/crates/ruff_python_ast/Cargo.toml +++ b/crates/ruff_python_ast/Cargo.toml @@ -26,6 +26,7 @@ rustc-hash = { workspace = true } serde = { workspace = true, optional = true } smallvec = { workspace = true } static_assertions = "1.1.0" +smol_str = { workspace = true } [dev-dependencies] insta = { workspace = true } diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index fceba83d02a80..a6e8e3cbba39f 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -1482,7 +1482,7 @@ pub fn pep_604_union(elts: &[Expr]) -> Expr { pub fn typing_optional(elt: Expr, binding: String) -> Expr { Expr::Subscript(ast::ExprSubscript { value: Box::new(Expr::Name(ast::ExprName { - id: binding, + id: binding.into(), range: TextRange::default(), ctx: ExprContext::Load, })), @@ -1513,7 +1513,7 @@ pub fn typing_union(elts: &[Expr], binding: String) -> Expr { Expr::Subscript(ast::ExprSubscript { value: Box::new(Expr::Name(ast::ExprName { - id: binding, + id: binding.into(), range: TextRange::default(), ctx: ExprContext::Load, })), @@ -1579,7 +1579,7 @@ mod tests { fn any_over_stmt_type_alias() { let seen = RefCell::new(Vec::new()); let name = Expr::Name(ExprName { - id: "x".to_string(), + id: "x".into(), range: TextRange::default(), ctx: ExprContext::Load, }); diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index f24a8063e008e..29a593f4985e5 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -1,13 +1,14 @@ #![allow(clippy::derive_partial_eq_without_eq)] +use itertools::Itertools; +use smol_str::SmolStr; use std::cell::OnceCell; + use std::fmt; use std::fmt::Debug; use std::ops::Deref; use std::slice::{Iter, IterMut}; -use itertools::Itertools; - use ruff_text_size::{Ranged, TextRange, TextSize}; use crate::{int, LiteralExpressionRef}; @@ -1739,7 +1740,7 @@ impl From for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprName { pub range: TextRange, - pub id: String, + pub id: SmolStr, pub ctx: ExprContext, } @@ -3225,13 +3226,13 @@ impl IpyEscapeKind { #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Identifier { - id: String, - range: TextRange, + pub id: SmolStr, + pub range: TextRange, } impl Identifier { #[inline] - pub fn new(id: impl Into, range: TextRange) -> Self { + pub fn new(id: impl Into, range: TextRange) -> Self { Self { id: id.into(), range, @@ -3275,9 +3276,9 @@ impl AsRef for Identifier { } } -impl AsRef for Identifier { +impl AsRef for Identifier { #[inline] - fn as_ref(&self) -> &String { + fn as_ref(&self) -> &SmolStr { &self.id } } @@ -3291,6 +3292,13 @@ impl std::fmt::Display for Identifier { impl From for String { #[inline] fn from(identifier: Identifier) -> String { + identifier.id.to_string() + } +} + +impl From for SmolStr { + #[inline] + fn from(identifier: Identifier) -> Self { identifier.id } } @@ -3836,6 +3844,6 @@ mod size_assertions { assert_eq_size!(StmtClassDef, [u8; 104]); assert_eq_size!(StmtTry, [u8; 112]); assert_eq_size!(Expr, [u8; 80]); - assert_eq_size!(Pattern, [u8; 96]); + assert_eq_size!(Pattern, [u8; 88]); assert_eq_size!(Mod, [u8; 32]); } diff --git a/crates/ruff_python_parser/Cargo.toml b/crates/ruff_python_parser/Cargo.toml index 25afcf6a081cb..f1e18f57a57e3 100644 --- a/crates/ruff_python_parser/Cargo.toml +++ b/crates/ruff_python_parser/Cargo.toml @@ -23,6 +23,7 @@ is-macro = { workspace = true } itertools = { workspace = true } lalrpop-util = { version = "0.20.0", default-features = false } memchr = { workspace = true } +smol_str = { workspace = true } unicode-ident = { workspace = true } unicode_names2 = { workspace = true } rustc-hash = { workspace = true } diff --git a/crates/ruff_python_parser/src/lexer.rs b/crates/ruff_python_parser/src/lexer.rs index 3831bc28d0315..b9a968d42d962 100644 --- a/crates/ruff_python_parser/src/lexer.rs +++ b/crates/ruff_python_parser/src/lexer.rs @@ -31,6 +31,7 @@ use std::iter::FusedIterator; use std::{char, cmp::Ordering, str::FromStr}; +use smol_str::SmolStr; use unicode_ident::{is_xid_continue, is_xid_start}; use ruff_python_ast::{Int, IpyEscapeKind}; @@ -241,7 +242,7 @@ impl<'source> Lexer<'source> { "yield" => Tok::Yield, _ => { return Ok(Tok::Name { - name: text.to_string(), + name: SmolStr::new(text), }) } }; diff --git a/crates/ruff_python_parser/src/python.lalrpop b/crates/ruff_python_parser/src/python.lalrpop index aa87fcd72d4f0..c5610b77941e8 100644 --- a/crates/ruff_python_parser/src/python.lalrpop +++ b/crates/ruff_python_parser/src/python.lalrpop @@ -3,6 +3,7 @@ // See also: file:///usr/share/doc/python/html/reference/compound_stmts.html#function-definitions // See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#keyword +use smol_str::SmolStr; use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_python_ast::{self as ast, Int, IpyEscapeKind}; use crate::{ @@ -289,7 +290,7 @@ ImportAsAlias: ast::Alias = { DottedName: ast::Identifier = { => ast::Identifier::new(n, (location..end_location).into()), => { - let mut r = n; + let mut r = n.to_string(); for x in n2 { r.push('.'); r.push_str(x.1.as_str()); @@ -2069,7 +2070,7 @@ extern { value: , is_raw: }, - name => token::Tok::Name { name: }, + name => token::Tok::Name { name: }, ipy_escape_command => token::Tok::IpyEscapeCommand { kind: , value: diff --git a/crates/ruff_python_parser/src/python.rs b/crates/ruff_python_parser/src/python.rs index 5771d7099f737..35cf0f7fb3168 100644 --- a/crates/ruff_python_parser/src/python.rs +++ b/crates/ruff_python_parser/src/python.rs @@ -1,5 +1,6 @@ // auto-generated: "lalrpop 0.20.0" -// sha3: 031689e389556292d9dbd8a1b1ff8ca29bac76d83f1b345630481d620b89e1c2 +// sha3: d4ee7846b5dfacc84c7fcd86cd6afddd44e4ae968b0a0e48ae8970bb2a7b4d12 +use smol_str::SmolStr; use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_python_ast::{self as ast, Int, IpyEscapeKind}; use crate::{ @@ -24,6 +25,7 @@ extern crate alloc; #[allow(non_snake_case, non_camel_case_types, unused_mut, unused_variables, unused_imports, unused_parens, clippy::all)] mod __parse__Top { + use smol_str::SmolStr; use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_python_ast::{self as ast, Int, IpyEscapeKind}; use crate::{ @@ -53,7 +55,7 @@ mod __parse__Top { Variant3((String, bool)), Variant4(Int), Variant5((IpyEscapeKind, String)), - Variant6(String), + Variant6(SmolStr), Variant7((String, StringKind, bool)), Variant8(core::option::Option), Variant9(Option>), @@ -18526,7 +18528,7 @@ mod __parse__Top { fn __pop_Variant6< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, String, TextSize) + ) -> (TextSize, SmolStr, TextSize) { match __symbols.pop() { Some((__l, __Symbol::Variant6(__v), __r)) => (__l, __v, __r), @@ -33541,7 +33543,7 @@ fn __action69< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, n, _): (TextSize, String, TextSize), + (_, n, _): (TextSize, SmolStr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Identifier { @@ -33555,13 +33557,13 @@ fn __action70< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, n, _): (TextSize, String, TextSize), + (_, n, _): (TextSize, SmolStr, TextSize), (_, n2, _): (TextSize, alloc::vec::Vec<(token::Tok, ast::Identifier)>, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Identifier { { - let mut r = n; + let mut r = n.to_string(); for x in n2 { r.push('.'); r.push_str(x.1.as_str()); @@ -36514,7 +36516,7 @@ fn __action224< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, name_location, _): (TextSize, TextSize, TextSize), - (_, s, _): (TextSize, String, TextSize), + (_, s, _): (TextSize, SmolStr, TextSize), ) -> Result<(TextSize, ast::ConversionFlag),__lalrpop_util::ParseError> { { @@ -36899,7 +36901,7 @@ fn __action249< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, s, _): (TextSize, String, TextSize), + (_, s, _): (TextSize, SmolStr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Identifier { @@ -48027,7 +48029,7 @@ fn __action789< >( source_code: &str, mode: Mode, - __0: (TextSize, String, TextSize), + __0: (TextSize, SmolStr, TextSize), __1: (TextSize, TextSize, TextSize), ) -> ast::Identifier { @@ -48055,7 +48057,7 @@ fn __action790< >( source_code: &str, mode: Mode, - __0: (TextSize, String, TextSize), + __0: (TextSize, SmolStr, TextSize), __1: (TextSize, alloc::vec::Vec<(token::Tok, ast::Identifier)>, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::Identifier @@ -48408,7 +48410,7 @@ fn __action801< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, String, TextSize), + __1: (TextSize, SmolStr, TextSize), ) -> Result<(TextSize, ast::ConversionFlag),__lalrpop_util::ParseError> { let __start0 = __0.0; @@ -49209,7 +49211,7 @@ fn __action826< >( source_code: &str, mode: Mode, - __0: (TextSize, String, TextSize), + __0: (TextSize, SmolStr, TextSize), __1: (TextSize, TextSize, TextSize), ) -> ast::Identifier { @@ -64211,7 +64213,7 @@ fn __action1304< >( source_code: &str, mode: Mode, - __0: (TextSize, String, TextSize), + __0: (TextSize, SmolStr, TextSize), ) -> ast::Identifier { let __start0 = __0.2; @@ -64237,7 +64239,7 @@ fn __action1305< >( source_code: &str, mode: Mode, - __0: (TextSize, String, TextSize), + __0: (TextSize, SmolStr, TextSize), __1: (TextSize, alloc::vec::Vec<(token::Tok, ast::Identifier)>, TextSize), ) -> ast::Identifier { @@ -65035,7 +65037,7 @@ fn __action1333< >( source_code: &str, mode: Mode, - __0: (TextSize, String, TextSize), + __0: (TextSize, SmolStr, TextSize), ) -> ast::Identifier { let __start0 = __0.2; diff --git a/crates/ruff_python_parser/src/soft_keywords.rs b/crates/ruff_python_parser/src/soft_keywords.rs index 379ae1c08db38..764fb7fc3eab7 100644 --- a/crates/ruff_python_parser/src/soft_keywords.rs +++ b/crates/ruff_python_parser/src/soft_keywords.rs @@ -202,9 +202,7 @@ fn soft_to_name(tok: &Tok) -> Tok { Tok::Type => "type", _ => unreachable!("other tokens never reach here"), }; - Tok::Name { - name: name.to_owned(), - } + Tok::Name { name: name.into() } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/crates/ruff_python_parser/src/token.rs b/crates/ruff_python_parser/src/token.rs index ac441395fffc2..06c55b5caa747 100644 --- a/crates/ruff_python_parser/src/token.rs +++ b/crates/ruff_python_parser/src/token.rs @@ -8,6 +8,7 @@ use crate::Mode; use ruff_python_ast::{Int, IpyEscapeKind}; use ruff_text_size::TextSize; +use smol_str::SmolStr; use std::fmt; /// The set of tokens the Python source code can be tokenized in. @@ -16,7 +17,7 @@ pub enum Tok { /// Token value for a name, commonly known as an identifier. Name { /// The name value. - name: String, + name: SmolStr, }, /// Token value for an integer. Int { From bdd8f54d3fa392c621f798348d9fb88c8936691e Mon Sep 17 00:00:00 2001 From: LaBatata101 Date: Tue, 19 Dec 2023 17:21:43 -0300 Subject: [PATCH 2/6] Fix `clippy` lint --- crates/ruff_python_ast/src/nodes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 29a593f4985e5..c8266cacccbb6 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -3257,7 +3257,7 @@ impl PartialEq for Identifier { impl PartialEq for Identifier { #[inline] fn eq(&self, other: &String) -> bool { - &self.id == other + self.id == other } } From 687dc2ffdc2f15a67b0d35d6c02df9ed5006ad4f Mon Sep 17 00:00:00 2001 From: LaBatata101 Date: Tue, 19 Dec 2023 17:37:30 -0300 Subject: [PATCH 3/6] Avoid allocations --- crates/ruff_linter/src/rules/flake8_gettext/mod.rs | 4 +++- .../src/rules/flake8_self/rules/private_member_access.rs | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_gettext/mod.rs b/crates/ruff_linter/src/rules/flake8_gettext/mod.rs index 9476be96e11f9..37af247ebc6dc 100644 --- a/crates/ruff_linter/src/rules/flake8_gettext/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_gettext/mod.rs @@ -7,7 +7,9 @@ pub mod settings; /// 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.to_string()) + functions_names + .iter() + .any(|function_name| function_name == id) } else { false } diff --git a/crates/ruff_linter/src/rules/flake8_self/rules/private_member_access.rs b/crates/ruff_linter/src/rules/flake8_self/rules/private_member_access.rs index 6e68021274a34..385bdc7597461 100644 --- a/crates/ruff_linter/src/rules/flake8_self/rules/private_member_access.rs +++ b/crates/ruff_linter/src/rules/flake8_self/rules/private_member_access.rs @@ -77,7 +77,8 @@ pub(crate) fn private_member_access(checker: &mut Checker, expr: &Expr) { .settings .flake8_self .ignore_names - .contains(&attr.to_string()) + .iter() + .any(|name| name == attr.as_str()) { return; } From fdbb2015fbb40670d28ab1e3c1bbe253ed742b76 Mon Sep 17 00:00:00 2001 From: LaBatata101 Date: Tue, 19 Dec 2023 18:26:22 -0300 Subject: [PATCH 4/6] Avoid more allocations --- crates/ruff_linter/src/rules/pyflakes/rules/strings.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs b/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs index 68438e18f2f49..737d14c13db67 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs @@ -747,7 +747,11 @@ pub(crate) fn string_dot_format_extra_named_arguments( let missing: Vec<(usize, &str)> = keywords .enumerate() .filter_map(|(index, keyword)| { - if summary.keywords.contains(&keyword.to_string()) { + if summary + .keywords + .iter() + .any(|summary_keyword| summary_keyword == keyword.as_str()) + { None } else { Some((index, keyword.as_str())) From ee373e36a9aa6acc7390eea6c0b8b2e10e554c5b Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 20 Dec 2023 16:23:59 -0500 Subject: [PATCH 5/6] Fix type error --- .../src/rules/flake8_annotations/helpers.rs | 2 +- .../src/rules/pylint/rules/no_method_decorator.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs b/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs index 0ef1bcf262c0f..61967e736dafe 100644 --- a/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs @@ -136,7 +136,7 @@ impl AutoPythonType { ) .ok()?; let expr = Expr::Name(ast::ExprName { - id: binding, + id: binding.into(), range: TextRange::default(), ctx: ExprContext::Load, }); diff --git a/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs b/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs index 559d05879c555..f6d40c762cab3 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs @@ -116,21 +116,21 @@ fn get_undecorated_methods( { if let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() { if id == method_name && checker.semantic().is_builtin(method_name) { - if arguments.args.len() != 1 { + if targets.len() != 1 { continue; } - if targets.len() != 1 { + let [arg] = arguments.args.as_slice() else { continue; - } + }; let target_name = match targets.first() { - Some(Expr::Name(ast::ExprName { id, .. })) => id.to_string(), + Some(Expr::Name(ast::ExprName { id, .. })) => id, _ => continue, }; - if let Expr::Name(ast::ExprName { id, .. }) = &arguments.args[0] { - if target_name == *id { + if let Expr::Name(ast::ExprName { id, .. }) = &arg { + if target_name == id { explicit_decorator_calls.insert(id.to_string(), stmt.range()); } }; From 196901910d1c2ae2ed890fd34644a4f2681be35d Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 20 Dec 2023 16:44:42 -0500 Subject: [PATCH 6/6] Fix Clippy --- .../src/rules/pylint/rules/no_method_decorator.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs b/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs index f6d40c762cab3..1a1bad556d115 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/no_method_decorator.rs @@ -124,9 +124,11 @@ fn get_undecorated_methods( continue; }; - let target_name = match targets.first() { - Some(Expr::Name(ast::ExprName { id, .. })) => id, - _ => continue, + let Some(Expr::Name(ast::ExprName { + id: target_name, .. + })) = targets.first() + else { + continue; }; if let Expr::Name(ast::ExprName { id, .. }) = &arg {