refactor: replace unsafe unreachable usage with @panic

This commit is contained in:
CJ van den Berg 2024-03-24 20:12:59 +01:00
parent 2d1fb680e8
commit 4b86579745
7 changed files with 17 additions and 15 deletions

View file

@ -966,7 +966,7 @@ pub fn load(self: *const Self, reader: anytype, size: usize) !Root {
return error.BufferUnderrun;
const final_read = try reader.read(buf);
if (final_read != 0)
unreachable;
@panic("unexpected data in final read");
var leaf_count: usize = 1;
for (0..buf.len) |i| {

View file

@ -272,7 +272,7 @@ const FilteredWalker = struct {
if (is_filtered_dir(base.name))
continue;
var new_dir = top.iter.dir.openDir(base.name, .{ .iterate = true }) catch |err| switch (err) {
error.NameTooLong => unreachable, // no path sep in base.name
error.NameTooLong => @panic("unexpected error.NameTooLong"), // no path sep in base.name
else => continue,
};
{

View file

@ -157,7 +157,7 @@ pub const List = struct {
pub fn send(self: *const List, from: tp.pid_ref, m: tp.message) tp.result {
if (self.recursion_check)
unreachable;
@panic("recursive EventHandler call");
const self_nonconst = @constCast(self);
self_nonconst.recursion_check = true;
defer self_nonconst.recursion_check = false;

View file

@ -10,7 +10,7 @@ pub const Context = struct {
args: tp.message = .{},
pub fn fmt(value: anytype) Context {
return .{ .args = tp.message.fmtbuf(&context_buffer, value) catch unreachable };
return .{ .args = tp.message.fmtbuf(&context_buffer, value) catch @panic("command.Context.fmt failed") };
}
};
threadlocal var context_buffer: [tp.max_message_size]u8 = undefined;

View file

@ -409,7 +409,9 @@ pub const Editor = struct {
return primary;
if (idx == 0) {
self.logger.print("ERROR: no more cursors", .{});
(@constCast(self).cursels.addOne() catch unreachable).* = CurSel{};
(@constCast(self).cursels.addOne() catch |e| switch (e) {
error.OutOfMemory => @panic("get_primary error.OutOfMemory"),
}).* = CurSel{};
}
return self.get_primary();
}
@ -2770,7 +2772,7 @@ pub const Editor = struct {
if (query.len == 0) return;
const history = if (self.find_history) |*hist| hist else ret: {
self.find_history = std.ArrayList([]const u8).init(self.a);
if (self.find_history) |*hist| break :ret hist else unreachable;
break :ret &self.find_history.?;
};
for (history.items, 0..) |entry, i|
if (std.mem.eql(u8, entry, query))

View file

@ -150,7 +150,7 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
try tui.set_base_style_alpha(self.plane, "", theme.editor, nc.ALPHA_TRANSPARENT, nc.ALPHA_TRANSPARENT);
self.plane.erase();
self.plane.home();
if (self.fire) |*fire| fire.render() catch unreachable;
if (self.fire) |*fire| fire.render();
const style_title = if (tui.find_scope_style(theme, "function")) |sty| sty.style else theme.editor;
const style_subtext = if (tui.find_scope_style(theme, "comment")) |sty| sty.style else theme.editor;
@ -199,7 +199,7 @@ pub fn handle_resize(self: *Self, pos: Widget.Box) void {
self.plane.resize_simple(@intCast(pos.h), @intCast(pos.w)) catch return;
if (self.fire) |*fire| {
fire.deinit();
self.fire = Fire.init(self.a, self.plane, pos) catch unreachable;
self.fire = Fire.init(self.a, self.plane, pos) catch return;
}
}
@ -278,7 +278,7 @@ const Fire = struct {
const fire_black: u8 = 0;
const fire_white: u8 = fire_palette.len - 1;
fn render(self: *Fire) !void {
fn render(self: *Fire) void {
var rand = self.prng.random();
//update fire buf
@ -328,9 +328,9 @@ const Fire = struct {
const px_hi = self.screen_buf[frame_y * self.FIRE_W + frame_x];
const px_lo = self.screen_buf[(frame_y + 1) * self.FIRE_W + frame_x];
try self.plane.set_fg_palindex(fire_palette[px_hi]);
try self.plane.set_bg_palindex(fire_palette[px_lo]);
_ = try self.plane.putstr(px);
self.plane.set_fg_palindex(fire_palette[px_hi]) catch {};
self.plane.set_bg_palindex(fire_palette[px_lo]) catch {};
_ = self.plane.putstr(px) catch {};
}
self.plane.cursor_move_yx(-1, 0) catch {};
self.plane.cursor_move_rel(1, 0) catch {};

View file

@ -335,10 +335,10 @@ fn create_editor(self: *Self) tp.result {
var editor_widget = ed.create(self.a, Widget.to(self)) catch |e| return tp.exit_error(e);
errdefer editor_widget.deinit(self.a);
if (editor_widget.get("editor")) |editor| {
editor.subscribe(EventHandler.to_unowned(self.statusbar)) catch unreachable;
editor.subscribe(EventHandler.bind(self, handle_editor_event)) catch unreachable;
editor.subscribe(EventHandler.to_unowned(self.statusbar)) catch @panic("subscribe unsupported");
editor.subscribe(EventHandler.bind(self, handle_editor_event)) catch @panic("subscribe unsupported");
self.editor = if (editor.dynamic_cast(ed.EditorWidget)) |p| &p.editor else null;
} else unreachable;
} else @panic("mainview editor not found");
self.widgets.replace(0, editor_widget);
self.resize();
}