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',
'-Walloca',
'-Wunused-macros',
'-Wshadow',
'-Wno-unused-parameter',
'-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 */
keyboard_reset_current_keybind();
struct view *view;
struct action *action;
struct output *output;
struct output *target;
struct cursor_context ctx = {0};
if (cursor_ctx) {
ctx = *cursor_ctx;
}
struct action *action;
wl_list_for_each(action, actions, link) {
if (server->input_mode == LAB_INPUT_STATE_WINDOW_SWITCHER
&& 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
* previous action
*/
view = view_for_action(activator, server, action, &ctx);
struct view *view = view_for_action(activator, server, action, &ctx);
switch (action->type) {
case ACTION_TYPE_CLOSE:
@ -919,15 +916,14 @@ actions_run(struct view *activator, struct server *server,
case ACTION_TYPE_DEBUG:
debug_dump_scene(server);
break;
case ACTION_TYPE_EXECUTE:
{
case ACTION_TYPE_EXECUTE: {
struct buf cmd = BUF_INIT;
buf_add(&cmd, action_get_str(action, "command", NULL));
buf_expand_tilde(&cmd);
spawn_async_no_shell(cmd.data);
buf_reset(&cmd);
}
break;
}
case ACTION_TYPE_EXIT:
wl_display_terminate(server->wl_display);
break;
@ -1146,8 +1142,7 @@ actions_run(struct view *activator, struct server *server,
break;
}
/* Falls through to GoToDesktop */
case ACTION_TYPE_GO_TO_DESKTOP:
{
case ACTION_TYPE_GO_TO_DESKTOP: {
bool follow = true;
bool wrap = action_get_bool(action, "wrap", true);
const char *to = action_get_str(action, "to", NULL);
@ -1156,13 +1151,13 @@ actions_run(struct view *activator, struct server *server,
* removed the action during the initial parsing step as it is
* a required argument for both SendToDesktop and GoToDesktop.
*/
struct workspace *target = workspaces_find(
struct workspace *target_workspace = workspaces_find(
server->workspaces.current, to, wrap);
if (!target) {
if (!target_workspace) {
break;
}
if (action->type == ACTION_TYPE_SEND_TO_DESKTOP) {
view_move_to_workspace(view, target);
view_move_to_workspace(view, target_workspace);
follow = action_get_bool(action, "follow", true);
/* Ensure that the focus is not on another desktop */
@ -1171,20 +1166,22 @@ actions_run(struct view *activator, struct server *server,
}
}
if (follow) {
workspaces_switch_to(target,
workspaces_switch_to(target_workspace,
/*update_focus*/ true);
}
}
break;
case ACTION_TYPE_MOVE_TO_OUTPUT:
}
case ACTION_TYPE_MOVE_TO_OUTPUT: {
if (!view) {
break;
}
target = get_target_output(view->output, server, action);
if (target) {
view_move_to_output(view, target);
struct output *target_output =
get_target_output(view->output, server, action);
if (target_output) {
view_move_to_output(view, target_output);
}
break;
}
case ACTION_TYPE_FIT_TO_OUTPUT:
if (!view) {
break;
@ -1192,11 +1189,11 @@ actions_run(struct view *activator, struct server *server,
view_constrain_size_to_that_of_usable_area(view);
break;
case ACTION_TYPE_TOGGLE_SNAP_TO_REGION:
case ACTION_TYPE_SNAP_TO_REGION:
case ACTION_TYPE_SNAP_TO_REGION: {
if (!view) {
break;
}
output = view->output;
struct output *output = view->output;
if (!output) {
break;
}
@ -1218,6 +1215,7 @@ actions_run(struct view *activator, struct server *server,
wlr_log(WLR_ERROR, "Invalid SnapToRegion id: '%s'", region_name);
}
break;
}
case ACTION_TYPE_UNSNAP:
if (view && !view->fullscreen && !view_is_floating(view)) {
view_maximize(view, VIEW_AXIS_NONE,
@ -1231,20 +1229,21 @@ actions_run(struct view *activator, struct server *server,
view_toggle_keybinds(view);
}
break;
case ACTION_TYPE_FOCUS_OUTPUT:
output = output_nearest_to_cursor(server);
target = get_target_output(output, server, action);
if (target) {
desktop_focus_output(target);
case ACTION_TYPE_FOCUS_OUTPUT: {
struct output *output = output_nearest_to_cursor(server);
struct output *target_output =
get_target_output(output, server, action);
if (target_output) {
desktop_focus_output(target_output);
}
break;
}
case ACTION_TYPE_IF:
if (view) {
run_if_action(view, server, action);
}
break;
case ACTION_TYPE_FOR_EACH:
{
case ACTION_TYPE_FOR_EACH: {
struct wl_array views;
struct view **item;
bool matches = false;
@ -1255,29 +1254,29 @@ actions_run(struct view *activator, struct server *server,
}
wl_array_release(&views);
if (!matches) {
struct wl_list *actions;
actions = action_get_actionlist(action, "none");
if (actions) {
actions_run(view, server, actions, NULL);
}
struct wl_list *child_actions;
child_actions = action_get_actionlist(action, "none");
if (child_actions) {
actions_run(view, server, child_actions, NULL);
}
}
break;
case ACTION_TYPE_VIRTUAL_OUTPUT_ADD:
{
const char *output_name = action_get_str(action, "output_name",
NULL);
}
case ACTION_TYPE_VIRTUAL_OUTPUT_ADD: {
/* TODO: rename this argument to "outputName" */
const char *output_name =
action_get_str(action, "output_name", NULL);
output_virtual_add(server, output_name,
/*store_wlr_output*/ NULL);
}
break;
case ACTION_TYPE_VIRTUAL_OUTPUT_REMOVE:
{
const char *output_name = action_get_str(action, "output_name",
NULL);
}
case ACTION_TYPE_VIRTUAL_OUTPUT_REMOVE: {
/* TODO: rename this argument to "outputName" */
const char *output_name =
action_get_str(action, "output_name", NULL);
output_virtual_remove(server, output_name);
}
break;
}
case ACTION_TYPE_AUTO_PLACE:
if (view) {
enum view_placement_policy policy =
@ -1349,16 +1348,15 @@ actions_run(struct view *activator, struct server *server,
case ACTION_TYPE_ZOOM_OUT:
magnifier_set_scale(server, MAGNIFY_DECREASE);
break;
case ACTION_TYPE_WARP_CURSOR:
{
case ACTION_TYPE_WARP_CURSOR: {
const char *to = action_get_str(action, "to", "output");
const char *x = action_get_str(action, "x", "center");
const char *y = action_get_str(action, "y", "center");
struct output *output = output_nearest_to_cursor(server);
warp_cursor(view, output, to, x, y);
}
break;
}
case ACTION_TYPE_HIDE_CURSOR:
cursor_set_visible(&server->seat, false);
break;

View file

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