Add -Wshadow + reformat switch cases

Adding -Wshadow will prevent unintentional variable overrides.

Also, wrapping switch cases with declarations with braces will make our
logic more robust by limiting lifetimes of variables.
This commit is contained in:
tokyo4j 2025-05-23 13:09:15 +09:00 committed by Johan Malm
parent e19f0fc267
commit 68bf55d724
4 changed files with 91 additions and 94 deletions

View file

@ -34,6 +34,7 @@ add_project_arguments(cc.get_supported_arguments([
'-Wmissing-prototypes', '-Wmissing-prototypes',
'-Walloca', '-Walloca',
'-Wunused-macros', '-Wunused-macros',
'-Wshadow',
'-Wno-unused-parameter', '-Wno-unused-parameter',
'-Wno-expansion-to-defined', '-Wno-expansion-to-defined',

View file

@ -871,15 +871,12 @@ actions_run(struct view *activator, struct server *server,
/* This cancels any pending on-release keybinds */ /* This cancels any pending on-release keybinds */
keyboard_reset_current_keybind(); keyboard_reset_current_keybind();
struct view *view;
struct action *action;
struct output *output;
struct output *target;
struct cursor_context ctx = {0}; struct cursor_context ctx = {0};
if (cursor_ctx) { if (cursor_ctx) {
ctx = *cursor_ctx; ctx = *cursor_ctx;
} }
struct action *action;
wl_list_for_each(action, actions, link) { wl_list_for_each(action, actions, link) {
if (server->input_mode == LAB_INPUT_STATE_WINDOW_SWITCHER if (server->input_mode == LAB_INPUT_STATE_WINDOW_SWITCHER
&& action->type != ACTION_TYPE_NEXT_WINDOW && action->type != ACTION_TYPE_NEXT_WINDOW
@ -896,7 +893,7 @@ actions_run(struct view *activator, struct server *server,
* Refetch view because it may have been changed due to the * Refetch view because it may have been changed due to the
* previous action * previous action
*/ */
view = view_for_action(activator, server, action, &ctx); struct view *view = view_for_action(activator, server, action, &ctx);
switch (action->type) { switch (action->type) {
case ACTION_TYPE_CLOSE: case ACTION_TYPE_CLOSE:
@ -919,15 +916,14 @@ actions_run(struct view *activator, struct server *server,
case ACTION_TYPE_DEBUG: case ACTION_TYPE_DEBUG:
debug_dump_scene(server); debug_dump_scene(server);
break; break;
case ACTION_TYPE_EXECUTE: case ACTION_TYPE_EXECUTE: {
{ struct buf cmd = BUF_INIT;
struct buf cmd = BUF_INIT; buf_add(&cmd, action_get_str(action, "command", NULL));
buf_add(&cmd, action_get_str(action, "command", NULL)); buf_expand_tilde(&cmd);
buf_expand_tilde(&cmd); spawn_async_no_shell(cmd.data);
spawn_async_no_shell(cmd.data); buf_reset(&cmd);
buf_reset(&cmd);
}
break; break;
}
case ACTION_TYPE_EXIT: case ACTION_TYPE_EXIT:
wl_display_terminate(server->wl_display); wl_display_terminate(server->wl_display);
break; break;
@ -1146,45 +1142,46 @@ actions_run(struct view *activator, struct server *server,
break; break;
} }
/* Falls through to GoToDesktop */ /* Falls through to GoToDesktop */
case ACTION_TYPE_GO_TO_DESKTOP: case ACTION_TYPE_GO_TO_DESKTOP: {
{ bool follow = true;
bool follow = true; bool wrap = action_get_bool(action, "wrap", true);
bool wrap = action_get_bool(action, "wrap", true); const char *to = action_get_str(action, "to", NULL);
const char *to = action_get_str(action, "to", NULL); /*
/* * `to` is always != NULL here because otherwise we would have
* `to` is always != NULL here because otherwise we would have * removed the action during the initial parsing step as it is
* removed the action during the initial parsing step as it is * a required argument for both SendToDesktop and GoToDesktop.
* a required argument for both SendToDesktop and GoToDesktop. */
*/ struct workspace *target_workspace = workspaces_find(
struct workspace *target = workspaces_find( server->workspaces.current, to, wrap);
server->workspaces.current, to, wrap); if (!target_workspace) {
if (!target) { break;
break; }
} if (action->type == ACTION_TYPE_SEND_TO_DESKTOP) {
if (action->type == ACTION_TYPE_SEND_TO_DESKTOP) { view_move_to_workspace(view, target_workspace);
view_move_to_workspace(view, target); follow = action_get_bool(action, "follow", true);
follow = action_get_bool(action, "follow", true);
/* Ensure that the focus is not on another desktop */ /* Ensure that the focus is not on another desktop */
if (!follow && server->active_view == view) { if (!follow && server->active_view == view) {
desktop_focus_topmost_view(server); desktop_focus_topmost_view(server);
}
}
if (follow) {
workspaces_switch_to(target,
/*update_focus*/ true);
} }
} }
if (follow) {
workspaces_switch_to(target_workspace,
/*update_focus*/ true);
}
break; break;
case ACTION_TYPE_MOVE_TO_OUTPUT: }
case ACTION_TYPE_MOVE_TO_OUTPUT: {
if (!view) { if (!view) {
break; break;
} }
target = get_target_output(view->output, server, action); struct output *target_output =
if (target) { get_target_output(view->output, server, action);
view_move_to_output(view, target); if (target_output) {
view_move_to_output(view, target_output);
} }
break; break;
}
case ACTION_TYPE_FIT_TO_OUTPUT: case ACTION_TYPE_FIT_TO_OUTPUT:
if (!view) { if (!view) {
break; break;
@ -1192,11 +1189,11 @@ actions_run(struct view *activator, struct server *server,
view_constrain_size_to_that_of_usable_area(view); view_constrain_size_to_that_of_usable_area(view);
break; break;
case ACTION_TYPE_TOGGLE_SNAP_TO_REGION: case ACTION_TYPE_TOGGLE_SNAP_TO_REGION:
case ACTION_TYPE_SNAP_TO_REGION: case ACTION_TYPE_SNAP_TO_REGION: {
if (!view) { if (!view) {
break; break;
} }
output = view->output; struct output *output = view->output;
if (!output) { if (!output) {
break; break;
} }
@ -1218,6 +1215,7 @@ actions_run(struct view *activator, struct server *server,
wlr_log(WLR_ERROR, "Invalid SnapToRegion id: '%s'", region_name); wlr_log(WLR_ERROR, "Invalid SnapToRegion id: '%s'", region_name);
} }
break; break;
}
case ACTION_TYPE_UNSNAP: case ACTION_TYPE_UNSNAP:
if (view && !view->fullscreen && !view_is_floating(view)) { if (view && !view->fullscreen && !view_is_floating(view)) {
view_maximize(view, VIEW_AXIS_NONE, view_maximize(view, VIEW_AXIS_NONE,
@ -1231,53 +1229,54 @@ actions_run(struct view *activator, struct server *server,
view_toggle_keybinds(view); view_toggle_keybinds(view);
} }
break; break;
case ACTION_TYPE_FOCUS_OUTPUT: case ACTION_TYPE_FOCUS_OUTPUT: {
output = output_nearest_to_cursor(server); struct output *output = output_nearest_to_cursor(server);
target = get_target_output(output, server, action); struct output *target_output =
if (target) { get_target_output(output, server, action);
desktop_focus_output(target); if (target_output) {
desktop_focus_output(target_output);
} }
break; break;
}
case ACTION_TYPE_IF: case ACTION_TYPE_IF:
if (view) { if (view) {
run_if_action(view, server, action); run_if_action(view, server, action);
} }
break; break;
case ACTION_TYPE_FOR_EACH: case ACTION_TYPE_FOR_EACH: {
{ struct wl_array views;
struct wl_array views; struct view **item;
struct view **item; bool matches = false;
bool matches = false; wl_array_init(&views);
wl_array_init(&views); view_array_append(server, &views, LAB_VIEW_CRITERIA_NONE);
view_array_append(server, &views, LAB_VIEW_CRITERIA_NONE); wl_array_for_each(item, &views) {
wl_array_for_each(item, &views) { matches |= run_if_action(*item, server, action);
matches |= run_if_action(*item, server, action); }
} wl_array_release(&views);
wl_array_release(&views); if (!matches) {
if (!matches) { struct wl_list *child_actions;
struct wl_list *actions; child_actions = action_get_actionlist(action, "none");
actions = action_get_actionlist(action, "none"); if (child_actions) {
if (actions) { actions_run(view, server, child_actions, NULL);
actions_run(view, server, actions, NULL);
}
} }
} }
break; break;
case ACTION_TYPE_VIRTUAL_OUTPUT_ADD: }
{ case ACTION_TYPE_VIRTUAL_OUTPUT_ADD: {
const char *output_name = action_get_str(action, "output_name", /* TODO: rename this argument to "outputName" */
NULL); const char *output_name =
output_virtual_add(server, output_name, action_get_str(action, "output_name", NULL);
output_virtual_add(server, output_name,
/*store_wlr_output*/ NULL); /*store_wlr_output*/ NULL);
}
break; break;
case ACTION_TYPE_VIRTUAL_OUTPUT_REMOVE: }
{ case ACTION_TYPE_VIRTUAL_OUTPUT_REMOVE: {
const char *output_name = action_get_str(action, "output_name", /* TODO: rename this argument to "outputName" */
NULL); const char *output_name =
output_virtual_remove(server, output_name); action_get_str(action, "output_name", NULL);
} output_virtual_remove(server, output_name);
break; break;
}
case ACTION_TYPE_AUTO_PLACE: case ACTION_TYPE_AUTO_PLACE:
if (view) { if (view) {
enum view_placement_policy policy = enum view_placement_policy policy =
@ -1349,16 +1348,15 @@ actions_run(struct view *activator, struct server *server,
case ACTION_TYPE_ZOOM_OUT: case ACTION_TYPE_ZOOM_OUT:
magnifier_set_scale(server, MAGNIFY_DECREASE); magnifier_set_scale(server, MAGNIFY_DECREASE);
break; break;
case ACTION_TYPE_WARP_CURSOR: case ACTION_TYPE_WARP_CURSOR: {
{ const char *to = action_get_str(action, "to", "output");
const char *to = action_get_str(action, "to", "output"); const char *x = action_get_str(action, "x", "center");
const char *x = action_get_str(action, "x", "center"); const char *y = action_get_str(action, "y", "center");
const char *y = action_get_str(action, "y", "center"); struct output *output = output_nearest_to_cursor(server);
struct output *output = output_nearest_to_cursor(server);
warp_cursor(view, output, to, x, y); warp_cursor(view, output, to, x, y);
}
break; break;
}
case ACTION_TYPE_HIDE_CURSOR: case ACTION_TYPE_HIDE_CURSOR:
cursor_set_visible(&server->seat, false); cursor_set_visible(&server->seat, false);
break; break;

View file

@ -101,11 +101,11 @@ _osd_update(struct server *server)
/* Border */ /* Border */
set_cairo_color(cairo, theme->osd_border_color); set_cairo_color(cairo, theme->osd_border_color);
struct wlr_fbox fbox = { struct wlr_fbox border_fbox = {
.width = width, .width = width,
.height = height, .height = height,
}; };
draw_cairo_border(cairo, fbox, theme->osd_border_width); draw_cairo_border(cairo, border_fbox, theme->osd_border_width);
/* Boxes */ /* Boxes */
uint16_t x; uint16_t x;
@ -385,9 +385,9 @@ workspaces_switch_to(struct workspace *target, bool update_focus)
* below that should take care of the issue. * below that should take care of the issue.
*/ */
if (update_focus) { if (update_focus) {
struct view *view = server->active_view; struct view *active_view = server->active_view;
if (!view || (!view->visible_on_all_workspaces if (!active_view || (!active_view->visible_on_all_workspaces
&& !view_is_always_on_top(view))) { && !view_is_always_on_top(active_view))) {
desktop_focus_topmost_view(server); desktop_focus_topmost_view(server);
} }
} }

View file

@ -243,8 +243,6 @@ handle_commit(struct wl_listener *listener, void *data)
* *
* This is not ideal, but it is the cleanest option. * This is not ideal, but it is the cleanest option.
*/ */
struct wlr_xdg_toplevel *toplevel =
xdg_toplevel_from_view(view);
toplevel->scheduled.width = view->current.width; toplevel->scheduled.width = view->current.width;
toplevel->scheduled.height = view->current.height; toplevel->scheduled.height = view->current.height;
} }