← home

Removing Compiler Flag -Wno-unused-result

πŸ’‘
  • GCC doesn't warn about unused return values.
  • -Wno-unused-result β†’ what is this, can I remove it safely, and could it cause other problems?
πŸ’‘
  • Check carefully so as not to break other things.
πŸ’‘
  • GCC keeps suppressing warnings about unused results.
πŸ’‘
  • nice warnings and a solution to the mystery.

Honestly, I don't know where to start. As I usually do in this kind of situation, let's try something and see what happens.

Stage 1 - What does the flag mean?

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wunused-result

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wunused-result

Ok, then what is the attribute warn_unused_result?

https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-warn_005funused_005fresult

https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-warn_005funused_005fresult

Ok, so the compiler will yell for unused return values for functions with the attribute, but when we use -Wno-unused-result, it suppresses the warning.

Stage 2 - I need a plan, but I don’t have a plan.

Hmm, how do I track down which functions have that specific attribute? What if I just let the compiler do the work and throw an error if there is one? Where to start? Maybe from the most recent commit? This is what I did:

** On every compile attempt: removed -Wno-unused-result and added -Wunused-result explicitly.

  1. Removed the flag on the most recent commit and compiled with -Wunused-result, it compiled successfully.
  2. What about a year ago? Huh, it compiles successfully as well.
  3. Why is it there, then? When was this flag added? β€”> use git log --follow to track where it starts. It was the commit c5494ac06 (2015-11-21). It was refactored once on commit 62d7164d3 (2017-01-23).
  4. To see why it's used, I need to spot it β€” let the compiler do the work. β€”> ??? Why does it compile successfully on both c5494ac06 and 62d7164d3?
  5. After looking closely at CMakeLists.txt, I found -Werror is set for non-MSVC when STOP_ON_WARNING=ON. β€”> I guess it's preventative, then?
  6. I still need to be careful β€”> so I tested the first commit of each year from 2023 to 2026. β€”> all good!

Stage 3 - Run CI & submit PR

Done and merged βœ…


πŸ’‘
  • I didn't know how to build with a specific compiler before, and I learned how through this journey.
  • I learned how to use docker and how to write Dockerfile.
  • I learned various git commands and flags: git grep, git log --follow, git log --before, git log --oneline, and git show.
  • I learned that the prefix -W enables a warning and -Wno disables it for compiler flags generally.
  • I prefer building from the terminal (also for git) because IDE setup itself sometimes drives me crazy (it's not really intuitive for me). But I'm going to try setting up the configuration in CLion later too, because this feels like a good chance to learn, otherwise I might never do it.
  • So I learned the compilation pipeline is: configure β†’ generate β†’ build. Configure and generate happen in one call, e.g., cmake -B build-dir .... The build step happens when we call cmake --build build-dir ... or make/ninja directly.
  • I learned what a "generator" is. CMakeLists.txt contains human-readable text, and the generator creates a lower-level file containing the "plan" derived from it.
  • Before, I used to just memorize commands and flags. But now I'm learning them one by one, and it's quite interesting. CMake flags: -B and -G for the build target directory and the specific generator, respectively. -DCMAKE_C_COMPILER and -DCMAKE_CXX_COMPILER for the path to the specific compilers. The build target directory name is up to the user, and once set, I can just build with cmake --build <target_dir>.