Skip to content

Consider making "switch" statements cover all cases #3627

@anthonygraca

Description

@anthonygraca

Describe the bug

I was playing around with some compiler flags recommended from the Airbus Security Lab and it looks like using the -Wswitch-enum flag triggers some errors/warning in the googletest code base.

I took a look at the C++ Core Guidelines and they have a rule under Enum.2 that says developers should enforce the rule by "[flagging instances of] switch-statements where the cases cover most but not all enumerators of an enumeration". Handling all cases is also recommended by the MISRA guidelines as well, which would allow googletest to be more likely used in code bases involving automobile and aerospace applications.

Solution: This is an easy fix. I would add sufficient tests and I would fill in the new cases with a [[fallthrough]] to the logic handled in the default case. If this issue is approved as valid, I can prepare a PR within a day. A fall-through is desired in this case as described in the google c++ style guide.

Steps to reproduce the bug

A developer can reproduce this bug by adding the following line near the top of his/her CMakeLists.txt
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-Wswitch-enum"}

Generate makefiles:

mkdir mybuild
cd mybuild
cmake -Dgtest_build_tests=ON -Dgmock_build_tests=ON ${GTEST_REPO_DIR}
cmake -DPYTHON_EXECUTABLE=path/to/python ...

Calling make should cause warnings too show.

make

Alternatively with Bazel
bazel test --cxxopt='-Wswitch-enum' --test_output=errors //...

Does the bug persist in the most recent commit?

This bug is found with commit 16f637f from October 11 (which is the latest on master as of writing).

What operating system and version are you using?

I am using an M1 MacBook Pro running macOS Big Sur Version 11.6

What compiler and version are you using?

I am using Clang++ Version 13.0.0

What build system are you using?

I am using CMake 3.21.3

Additional context

N/A

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions