From 0f73dff17d8c13efd4679c349d4c26782a59ed98 Mon Sep 17 00:00:00 2001 From: "Matthew \"strager\" Glazar" Date: Sun, 5 Nov 2023 02:11:01 -0500 Subject: [PATCH] refactor(cli): work around GCC false positive warning GCC complains that arg_var might not be initialized: vector.h:238:23: error: '*((void*)& next_vim_file_bufnr +8)' may be used uninitialized in this function [-Werror=maybe-uninitialized] 238 | this->data_end_ = new (this->data_end_) T(std::forward(args)...); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ options.cpp:36:5: note: '*((void*)& next_vim_file_bufnr +8)' was declared here 36 | } next_vim_file_bufnr; | ^~~~~~~~~~~~~~~~~~~ This is a false positive in GCC. Whenever arg_var is pushed onto a Vector, we check if number is non-null. Whenever number is non-null, arg_var is initialized. Work around the false positive by refactoring the code such that arg_var and number are initialized together. --- src/quick-lint-js/cli/options.cpp | 39 ++++++++++++++++++------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/quick-lint-js/cli/options.cpp b/src/quick-lint-js/cli/options.cpp index 943cdeffbc..d5235f1537 100644 --- a/src/quick-lint-js/cli/options.cpp +++ b/src/quick-lint-js/cli/options.cpp @@ -30,10 +30,11 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) { Monotonic_Allocator temporary_allocator("parse_options"); - struct { - std::optional number; + struct Next_Vim_File_Bufnr { + int number; const char* arg_var; - } next_vim_file_bufnr; + }; + std::optional next_vim_file_bufnr; Bump_Vector files_to_lint("files_to_lint", allocator); Bump_Vector error_unrecognized_options( @@ -53,12 +54,16 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) { if (is_stdin && has_stdin) { o.has_multiple_stdin = true; } else { - File_To_Lint file{.path = path, - .config_file = active_config_file, - .path_for_config_search = next_path_for_config_search, - .language = language, - .is_stdin = is_stdin, - .vim_bufnr = next_vim_file_bufnr.number}; + File_To_Lint file{ + .path = path, + .config_file = active_config_file, + .path_for_config_search = next_path_for_config_search, + .language = language, + .is_stdin = is_stdin, + .vim_bufnr = next_vim_file_bufnr.has_value() + ? std::optional(next_vim_file_bufnr->number) + : std::nullopt, + }; files_to_lint.emplace_back(file); } if (is_stdin) { @@ -67,7 +72,7 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) { unused_language_option = nullptr; next_path_for_config_search = nullptr; - next_vim_file_bufnr.number = std::nullopt; + next_vim_file_bufnr = std::nullopt; }; auto add_stdin_file = [&]() { add_file("", /*is_stdin=*/true); }; @@ -160,12 +165,14 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) { error_unrecognized_options.emplace_back(arg_value); continue; } - if (next_vim_file_bufnr.number != std::nullopt) { + if (next_vim_file_bufnr.has_value()) { warning_vim_bufnr_without_file.emplace_back( - next_vim_file_bufnr.arg_var); + next_vim_file_bufnr->arg_var); } - next_vim_file_bufnr.number = bufnr; - next_vim_file_bufnr.arg_var = arg_value; + next_vim_file_bufnr = Next_Vim_File_Bufnr{ + .number = bufnr, + .arg_var = arg_value, + }; } QLJS_OPTION(const char* arg_value, "--exit-fail-on"sv) { @@ -189,8 +196,8 @@ Options parse_options(int argc, char** argv, Monotonic_Allocator* allocator) { if (unused_language_option) { warning_language_without_file.emplace_back(unused_language_option); } - if (next_vim_file_bufnr.number != std::nullopt) { - warning_vim_bufnr_without_file.emplace_back(next_vim_file_bufnr.arg_var); + if (next_vim_file_bufnr.has_value()) { + warning_vim_bufnr_without_file.emplace_back(next_vim_file_bufnr->arg_var); } if (o.path_for_stdin != nullptr) { for (File_To_Lint& file : files_to_lint) {