From 2e75d580a60d9342a01641417d3561e1d8157acc Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 8 Feb 2024 16:25:48 -0500 Subject: [PATCH] Box other strings --- .../rules/hardcoded_bind_all_interfaces.rs | 4 +++- crates/ruff_linter/src/rules/flynt/helpers.rs | 4 ++-- crates/ruff_python_ast/src/comparable.rs | 2 +- crates/ruff_python_ast/src/nodes.rs | 8 ++++---- crates/ruff_python_parser/src/string.rs | 14 +++++++------- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs index 38295b71316a2c..0e4301ee44c071 100644 --- a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs +++ b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_bind_all_interfaces.rs @@ -40,7 +40,9 @@ impl Violation for HardcodedBindAllInterfaces { pub(crate) fn hardcoded_bind_all_interfaces(checker: &mut Checker, string: StringLike) { let is_bind_all_interface = match string { StringLike::StringLiteral(ast::ExprStringLiteral { value, .. }) => value == "0.0.0.0", - StringLike::FStringLiteral(ast::FStringLiteralElement { value, .. }) => value == "0.0.0.0", + StringLike::FStringLiteral(ast::FStringLiteralElement { value, .. }) => { + &**value == "0.0.0.0" + } StringLike::BytesLiteral(_) => return, }; diff --git a/crates/ruff_linter/src/rules/flynt/helpers.rs b/crates/ruff_linter/src/rules/flynt/helpers.rs index 7a6af204d13f90..640f922d6faa22 100644 --- a/crates/ruff_linter/src/rules/flynt/helpers.rs +++ b/crates/ruff_linter/src/rules/flynt/helpers.rs @@ -15,7 +15,7 @@ fn to_f_string_expression_element(inner: &Expr) -> ast::FStringElement { /// Convert a string to a [`ast::FStringElement::Literal`]. pub(super) fn to_f_string_literal_element(s: &str) -> ast::FStringElement { ast::FStringElement::Literal(ast::FStringLiteralElement { - value: s.to_owned(), + value: s.to_string().into_boxed_str(), range: TextRange::default(), }) } @@ -53,7 +53,7 @@ pub(super) fn to_f_string_element(expr: &Expr) -> Option { match expr { Expr::StringLiteral(ast::ExprStringLiteral { value, range }) => { Some(ast::FStringElement::Literal(ast::FStringLiteralElement { - value: value.to_string(), + value: value.to_string().into_boxed_str(), range: *range, })) } diff --git a/crates/ruff_python_ast/src/comparable.rs b/crates/ruff_python_ast/src/comparable.rs index bc6327f01dca05..344bb615ce95e6 100644 --- a/crates/ruff_python_ast/src/comparable.rs +++ b/crates/ruff_python_ast/src/comparable.rs @@ -644,7 +644,7 @@ pub struct ComparableBytesLiteral<'a> { impl<'a> From<&'a ast::BytesLiteral> for ComparableBytesLiteral<'a> { fn from(bytes_literal: &'a ast::BytesLiteral) -> Self { Self { - value: bytes_literal.value.as_slice(), + value: &bytes_literal.value, } } } diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 09f4bf8ddd410f..f82317aee10e49 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -949,7 +949,7 @@ impl Ranged for FStringExpressionElement { #[derive(Clone, Debug, PartialEq)] pub struct FStringLiteralElement { pub range: TextRange, - pub value: String, + pub value: Box, } impl Ranged for FStringLiteralElement { @@ -962,7 +962,7 @@ impl Deref for FStringLiteralElement { type Target = str; fn deref(&self) -> &Self::Target { - self.value.as_str() + &self.value } } @@ -1607,7 +1607,7 @@ impl Default for BytesLiteralValueInner { #[derive(Clone, Debug, Default, PartialEq)] pub struct BytesLiteral { pub range: TextRange, - pub value: Vec, + pub value: Box<[u8]>, } impl Ranged for BytesLiteral { @@ -1620,7 +1620,7 @@ impl Deref for BytesLiteral { type Target = [u8]; fn deref(&self) -> &Self::Target { - self.value.as_slice() + &self.value } } diff --git a/crates/ruff_python_parser/src/string.rs b/crates/ruff_python_parser/src/string.rs index 124ba688a3a7a5..0e1459f96e4365 100644 --- a/crates/ruff_python_parser/src/string.rs +++ b/crates/ruff_python_parser/src/string.rs @@ -218,9 +218,9 @@ impl StringParser { let mut value = String::with_capacity(self.source.len()); loop { - // Add the characters before the escape sequence to the string. - let before_with_slash = self.skip_bytes(index + 1); - let before = &before_with_slash[..before_with_slash.len() - 1]; + // Add the characters before the escape sequence (or curly brace) to the string. + let before_with_slash_or_brace = self.skip_bytes(index + 1); + let before = &before_with_slash_or_brace[..before_with_slash_or_brace.len() - 1]; value.push_str(before); // Add the escaped character to the string. @@ -284,7 +284,7 @@ impl StringParser { } Ok(ast::FStringElement::Literal(ast::FStringLiteralElement { - value, + value: value.into_boxed_str(), range: self.range, })) } @@ -305,7 +305,7 @@ impl StringParser { if self.kind.is_raw() { // For raw strings, no escaping is necessary. return Ok(StringType::Bytes(ast::BytesLiteral { - value: self.source.into_bytes(), + value: self.source.into_boxed_bytes(), range: self.range, })); } @@ -313,7 +313,7 @@ impl StringParser { let Some(mut escape) = memchr::memchr(b'\\', self.source.as_bytes()) else { // If the string doesn't contain any escape sequences, return the owned string. return Ok(StringType::Bytes(ast::BytesLiteral { - value: self.source.into_bytes(), + value: self.source.into_boxed_bytes(), range: self.range, })); }; @@ -349,7 +349,7 @@ impl StringParser { } Ok(StringType::Bytes(ast::BytesLiteral { - value, + value: value.into_boxed_slice(), range: self.range, })) }