Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add some Safety comments when using unwrap()
  • Loading branch information
RobinMalfait committed Oct 30, 2024
commit 916093efe008c763f8da28603903d7e3ed381fc2
4 changes: 4 additions & 0 deletions crates/oxide/src/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ pub fn hoist_static_glob_parts(entries: &Vec<GlobEntry>) -> Vec<GlobEntry> {
// folders.
if pattern.is_empty() && base.is_file() {
result.push(GlobEntry {
// SAFETY: `parent()` will be available because we verify `base` is a file, thus a
// parent folder exists.
base: base.parent().unwrap().to_string_lossy().to_string(),
// SAFETY: `file_name()` will be available because we verify `base` is a file.
pattern: base.file_name().unwrap().to_string_lossy().to_string(),
});
}
Expand Down Expand Up @@ -100,6 +103,7 @@ pub fn optimize_patterns(entries: &Vec<GlobEntry>) -> Vec<GlobEntry> {
GlobEntry {
base,
pattern: match size {
// SAFETY: we can unwrap here because we know that the size is 1.
1 => patterns.next().unwrap(),
_ => {
let mut patterns = patterns.collect::<Vec<_>>();
Expand Down
2 changes: 2 additions & 0 deletions crates/oxide/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ impl<'a> Extractor<'a> {
}

// The ':` must be preceded by a-Z0-9 because it represents a property name.
// SAFETY: the Self::validate_arbitrary_property function from above validates that the
// `:` exists.
let colon = utility.find(":").unwrap();

if !utility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function extractV3Base(
// ^^^^^^^^^ -> Base
let rawVariants = segment(rawCandidate, ':')

// Safety: At this point it is safe to use TypeScript's non-null assertion
// SAFETY: At this point it is safe to use TypeScript's non-null assertion
// operator because even if the `input` was an empty string, splitting an
// empty string by `:` will always result in an array with at least one
// element.
Expand Down
6 changes: 3 additions & 3 deletions packages/tailwindcss/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function compileCandidates(
}

astNodes.sort((a, z) => {
// Safety: At this point it is safe to use TypeScript's non-null assertion
// SAFETY: At this point it is safe to use TypeScript's non-null assertion
// operator because if the ast nodes didn't exist, we introduced a bug
// above, but there is no need to re-check just to be sure. If this relied
// on pure user input, then we would need to check for its existence.
Expand Down Expand Up @@ -194,7 +194,7 @@ export function applyVariant(
return
}

// Safety: At this point it is safe to use TypeScript's non-null assertion
// SAFETY: At this point it is safe to use TypeScript's non-null assertion
// operator because if the `candidate.root` didn't exist, `parseCandidate`
// would have returned `null` and we would have returned early resulting in
// not hitting this code path.
Expand Down Expand Up @@ -322,7 +322,7 @@ function getPropertySort(nodes: AstNode[]) {
let q: AstNode[] = nodes.slice()

while (q.length > 0) {
// Safety: At this point it is safe to use TypeScript's non-null assertion
// SAFETY: At this point it is safe to use TypeScript's non-null assertion
// operator because we guarded against `q.length > 0` above.
let node = q.shift()!
if (node.kind === 'declaration') {
Expand Down