Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enforce usage of to_ascii_lowercase_cow #4014

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
allow-dbg-in-tests = true

disallowed-methods = [
{ path = "str::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrExtension::to_ascii_lowercase_cow` instead." },
{ path = "std::ffi::OsStr::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrExtension::to_ascii_lowercase_cow` instead." }
]
7 changes: 4 additions & 3 deletions crates/biome_css_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ version = "0.5.7"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
biome_rowan = { workspace = true, features = ["serde"] }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
biome_rowan = { workspace = true, features = ["serde"] }
biome_string_case = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }

[features]
schema = ["schemars", "biome_rowan/serde"]
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_css_syntax/src/file_source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use biome_rowan::FileSourceError;
use biome_string_case::StrExtension;
use std::{ffi::OsStr, path::Path};

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
Expand Down Expand Up @@ -73,6 +74,6 @@ impl TryFrom<&Path> for CssFileSource {
};
// We assume the file extensions are case-insensitive
// and we use the lowercase form of them for pattern matching
Self::try_from_extension(&extension.to_ascii_lowercase())
Self::try_from_extension(&extension.to_ascii_lowercase_cow())
}
}
7 changes: 4 additions & 3 deletions crates/biome_graphql_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
biome_rowan = { workspace = true, features = ["serde"] }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
biome_rowan = { workspace = true, features = ["serde"] }
biome_string_case = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }

[features]
schema = ["schemars", "biome_rowan/serde"]
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_graphql_syntax/src/file_source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use biome_rowan::FileSourceError;
use biome_string_case::StrExtension;
use std::ffi::OsStr;
use std::path::Path;

Expand Down Expand Up @@ -70,6 +71,6 @@ impl TryFrom<&Path> for GraphqlFileSource {
};
// We assume the file extensions are case-insensitive
// and we use the lowercase form of them for pattern matching
Self::try_from_extension(&extension.to_ascii_lowercase())
Self::try_from_extension(&extension.to_ascii_lowercase_cow())
}
}
7 changes: 4 additions & 3 deletions crates/biome_html_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ version = "0.5.7"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
biome_rowan = { workspace = true, features = ["serde"] }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
biome_rowan = { workspace = true, features = ["serde"] }
biome_string_case = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }

[features]
schema = ["schemars", "biome_rowan/serde"]
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_html_syntax/src/file_source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use biome_rowan::FileSourceError;
use biome_string_case::StrExtension;
use std::{ffi::OsStr, path::Path};

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
Expand Down Expand Up @@ -79,6 +80,6 @@ impl TryFrom<&Path> for HtmlFileSource {
};
// We assume the file extensions are case-insensitive
// and we use the lowercase form of them for pattern matching
Self::try_from_extension(&extension.to_ascii_lowercase())
Self::try_from_extension(&extension.to_ascii_lowercase_cow())
}
}
9 changes: 5 additions & 4 deletions crates/biome_js_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ repository.workspace = true
version = "0.5.7"

[dependencies]
biome_rowan = { workspace = true, features = ["serde"] }
enumflags2 = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
biome_rowan = { workspace = true, features = ["serde"] }
biome_string_case = { workspace = true }
enumflags2 = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }

[dev-dependencies]
biome_js_factory = { path = "../biome_js_factory" }
Expand Down
5 changes: 3 additions & 2 deletions crates/biome_js_syntax/src/file_source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use biome_rowan::FileSourceError;
use biome_string_case::StrExtension;
use std::{borrow::Cow, ffi::OsStr, path::Path};

/// Enum of the different ECMAScript standard versions.
Expand Down Expand Up @@ -362,7 +363,7 @@ impl TryFrom<&Path> for JsFileSource {
.file_name()
// We assume the file extensions are case-insensitive.
// Thus, we normalize the filrname to lowercase.
.map(|filename| filename.as_encoded_bytes().to_ascii_lowercase());
.map(|filename| filename.as_encoded_bytes().to_ascii_lowercase_cow());

// We assume the file extensions are case-insensitive
// and we use the lowercase form of them for pattern matching
Expand All @@ -377,7 +378,7 @@ impl TryFrom<&Path> for JsFileSource {
.extension()
// We assume the file extensions are case-insensitive.
// Thus, we normalize the extension to lowercase.
.map(|ext| Cow::Owned(ext.to_ascii_lowercase()))
.map(|ext| ext.to_ascii_lowercase_cow())
.ok_or(FileSourceError::MissingFileExtension)?,
};

Expand Down
7 changes: 4 additions & 3 deletions crates/biome_json_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ version = "0.5.7"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
biome_rowan = { workspace = true, features = ["serde"] }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
biome_rowan = { workspace = true, features = ["serde"] }
biome_string_case = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }

[features]
schema = ["biome_rowan/serde", "schemars"]
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_json_syntax/src/file_source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use biome_rowan::FileSourceError;
use biome_string_case::StrExtension;
use core::str;
use std::{
ffi::OsStr,
Expand Down Expand Up @@ -279,7 +280,7 @@ impl TryFrom<&Path> for JsonFileSource {
};
// We assume the file extensions are case-insensitive
// and we use the lowercase form of them for pattern matching
Self::try_from_extension(&extension.to_ascii_lowercase())
Self::try_from_extension(&extension.to_ascii_lowercase_cow())
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/biome_string_case/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ impl StrExtension for str {
fn to_ascii_lowercase_cow(&self) -> Cow<Self> {
let has_ascii_uppercase = self.bytes().any(|b| b.is_ascii_uppercase());
if has_ascii_uppercase {
#[allow(clippy::disallowed_methods)]
Cow::Owned(self.to_ascii_lowercase())
} else {
Cow::Borrowed(self)
Expand All @@ -440,6 +441,7 @@ impl StrExtension for std::ffi::OsStr {
.iter()
.any(|b| b.is_ascii_uppercase());
if has_ascii_uppercase {
#[allow(clippy::disallowed_methods)]
Cow::Owned(self.to_ascii_lowercase())
} else {
Cow::Borrowed(self)
Expand Down
7 changes: 4 additions & 3 deletions crates/biome_yaml_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ version = "0.0.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
biome_rowan = { workspace = true, features = ["serde"] }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
biome_rowan = { workspace = true, features = ["serde"] }
biome_string_case = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }

[features]
schema = ["biome_rowan/serde", "schemars"]
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_yaml_syntax/src/file_source.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use biome_rowan::FileSourceError;
use std::{ffi::OsStr, path::Path};
use biome_string_case::StrExtension;

#[cfg_attr(feature = "schema", derive(schemars::YamlSchema))]
#[derive(
Expand Down Expand Up @@ -88,7 +89,7 @@ impl TryFrom<&Path> for YamlFileSource {
};
// We assume the file extensions are case-insensitive
// and we use the lowercase form of them for pattern matching
Self::try_from_extension(&extension.to_ascii_lowercase())
Self::try_from_extension(&extension.to_ascii_lowercase_cow())
}
}

Expand Down
Loading