From 9262acd5cc6688eff5decf568a86bfae0606e62e Mon Sep 17 00:00:00 2001 From: Martin Kalling Date: Wed, 10 Jun 2026 10:16:18 +0200 Subject: [PATCH] tsort: remove unnecessary uses of unsafe The unsafe code was used to look up values in a hash table without checking whether the values were present. Verifying that a lookup succeeds only requires a few additional instructions, while avoiding unsafe makes the code easier to understand, analyze, and maintain. If a lookup fails, the program now panics. Such a failure indicates a programming error. --- src/uu/tsort/src/tsort.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/uu/tsort/src/tsort.rs b/src/uu/tsort/src/tsort.rs index d59cefada0c..eaadabec16b 100644 --- a/src/uu/tsort/src/tsort.rs +++ b/src/uu/tsort/src/tsort.rs @@ -208,15 +208,15 @@ impl Graph { } fn name(&self) -> String { - //SAFETY: the name is interned during graph creation and stored as name_sym. - // gives much better performance on lookup. - unsafe { self.interner.resolve_unchecked(self.name_sym).to_owned() } + self.interner + .resolve(self.name_sym) + .expect("Name has been interned") + .to_owned() } fn get_node_name(&self, node_sym: Sym) -> &str { - //SAFETY: the only way to get a Sym is by manipulating an interned string. - // gives much better performance on lookup. - - unsafe { self.interner.resolve_unchecked(node_sym) } + self.interner + .resolve(node_sym) + .expect("Name has been interned") } fn add_edge(&mut self, from: Sym, to: Sym) {