mlesniew / PicoMQTT

ESP MQTT client and broker library
GNU Lesser General Public License v3.0
219 stars 25 forks source link

PicoMQTT/src/PicoMQTT/outgoing_packet.cpp:216:19: error: this statement may fall through [-Werror=implicit-fallthrough=] #20

Closed mfechner closed 8 months ago

mfechner commented 8 months ago

I use you lib in combination with OpenDTU application running on an ESP32. The project is written using PlatformIO.

The project defined the following build_flags:

build_flags =
    -DCOMPONENT_EMBED_FILES=webapp_dist/index.html.gz:webapp_dist/zones.json.gz:webapp_dist/favicon.ico:webapp_dist/favicon.png:webapp_dist/js/app.js.gz
    -DPIOENV=\"$PIOENV\"
    -Wall -Wextra -Werror
    -std=c++17
    -std=gnu++17
; -DPICOMQTT_DEBUG_TRACE_FUNCTIONS
build_unflags =
    -std=gnu++11

Compilation of the project fails with:

.pio/libdeps/generic_esp32/PicoMQTT/src/PicoMQTT/outgoing_packet.cpp: In member function 'virtual bool PicoMQTT::OutgoingPacket::send()':
.pio/libdeps/generic_esp32/PicoMQTT/src/PicoMQTT/outgoing_packet.cpp:216:19: error: this statement may fall through [-Werror=implicit-fallthrough=]
             state = State::sent;
             ~~~~~~^~~~~~~
.pio/libdeps/generic_esp32/PicoMQTT/src/PicoMQTT/outgoing_packet.cpp:217:9: note: here
         case State::sent:
         ^~~~
Compiling .pio\build\generic_esp32\libad3\Hoymiles\HoymilesRadio.cpp.o
Compiling .pio\build\generic_esp32\libad3\Hoymiles\HoymilesRadio_CMT.cpp.o
Archiving .pio\build\generic_esp32\lib2d3\libU8g2.a
cc1plus.exe: all warnings being treated as errors
*** [.pio\build\generic_esp32\lib89b\PicoMQTT\PicoMQTT\outgoing_packet.cpp.o] Error 1

Removing the -Werror flag work-around the problem.

Do you see a possibility to fix this?

mlesniew commented 8 months ago

The fall-through in this switch block is there on purpose -- we want control to enter the next case and execute it.

The -Wextra flag enables -Wimplicit-fallthrough, which makes C/C++ emit a w warning for missing break statements at the end of case blocks. -Werror additionally makes GCC treat warnings as errors -- that's why compilation fails.

The best way to fix this is by telling the compiler that falling through is intentional, using one of the methods documented here: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wimplicit-fallthrough_003d

Can you try if this works and submit a pull request?

mfechner commented 8 months ago

I verified the linked patch and this fixes the compilation error.