Add common Clang warnings to cxx_base_flags and fix existing issues

This commit fixes several Clang warnings and updates cxx_base_flags to
always enable them, ensuring consistent warning coverage across builds.

Enabled additional warnings:
-Wswitch-default -Wswitch-enum -Wextra -Wcast-align -Wunused
-Wunreachable-code
This commit is contained in:
Klein, Thorsten 2025-10-23 17:38:53 +02:00
parent b2b9072ecb
commit 9d0754ba6b
12 changed files with 38 additions and 9 deletions

View File

@ -5777,7 +5777,7 @@ PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
} \ } \
template <typename arg_type> \ template <typename arg_type> \
bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \ bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \
const arg_type& arg, \ [[maybe_unused]] const arg_type& arg, \
[[maybe_unused]] ::testing::MatchResultListener* result_listener) const [[maybe_unused]] ::testing::MatchResultListener* result_listener) const
#define MATCHER_P(name, p0, description) \ #define MATCHER_P(name, p0, description) \
@ -5862,7 +5862,7 @@ PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
template <typename arg_type> \ template <typename arg_type> \
bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>:: \ bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>:: \
gmock_Impl<arg_type>::MatchAndExplain( \ gmock_Impl<arg_type>::MatchAndExplain( \
const arg_type& arg, \ [[maybe_unused]] const arg_type& arg, \
[[maybe_unused]] ::testing::MatchResultListener* result_listener) \ [[maybe_unused]] ::testing::MatchResultListener* result_listener) \
const const

View File

@ -301,6 +301,8 @@ void UnorderedElementsAreMatcherImplBase::DescribeToImpl(
case UnorderedMatcherRequire::Subset: case UnorderedMatcherRequire::Subset:
*os << "an injection from elements to requirements exists such that:\n"; *os << "an injection from elements to requirements exists such that:\n";
break; break;
default:
break;
} }
const char* sep = ""; const char* sep = "";
@ -343,6 +345,8 @@ void UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(
case UnorderedMatcherRequire::Subset: case UnorderedMatcherRequire::Subset:
*os << "no injection from elements to requirements exists such that:\n"; *os << "no injection from elements to requirements exists such that:\n";
break; break;
default:
break;
} }
const char* sep = ""; const char* sep = "";
for (size_t i = 0; i != matcher_describers_.size(); ++i) { for (size_t i = 0; i != matcher_describers_.size(); ++i) {

View File

@ -301,6 +301,7 @@ void ReportUninterestingCall(CallReaction reaction, const std::string& msg) {
"knowing-when-to-expect-useoncall for details.\n", "knowing-when-to-expect-useoncall for details.\n",
stack_frames_to_skip); stack_frames_to_skip);
break; break;
case kFail:
default: // FAIL default: // FAIL
Expect(false, nullptr, -1, msg); Expect(false, nullptr, -1, msg);
} }

View File

@ -73,7 +73,7 @@ class TemplatedCopyable {
TemplatedCopyable() = default; TemplatedCopyable() = default;
template <typename U> template <typename U>
TemplatedCopyable(const U& other) {} // NOLINT TemplatedCopyable(const U&) {} // NOLINT
}; };
class FooInterface { class FooInterface {

View File

@ -2829,6 +2829,8 @@ class PredicateFormatterFromMatcherTest : public ::testing::Test {
// an "interested" listener; so this will return |true|, thus // an "interested" listener; so this will return |true|, thus
// simulating a flaky matcher. // simulating a flaky matcher.
return listener->IsInterested(); return listener->IsInterested();
default:
break;
} }
GTEST_LOG_(FATAL) << "This should never be reached"; GTEST_LOG_(FATAL) << "This should never be reached";

View File

@ -91,7 +91,17 @@ macro(config_compiler_and_linker)
endif() endif()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR
CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
set(cxx_base_flags "-Wall -Wshadow -Wconversion -Wundef") set(cxx_base_flags "-Wall \
-Wshadow \
-Wconversion \
-Wundef \
-Wswitch-default \
-Wswitch-enum \
-Wextra \
-Wcast-align \
-Wunused \
-Wunreachable-code \
")
set(cxx_exception_flags "-fexceptions") set(cxx_exception_flags "-fexceptions")
set(cxx_no_exception_flags "-fno-exceptions") set(cxx_no_exception_flags "-fno-exceptions")
set(cxx_strict_flags "-W -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wunused-parameter -Wcast-align -Winline -Wredundant-decls") set(cxx_strict_flags "-W -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wunused-parameter -Wcast-align -Winline -Wredundant-decls")

View File

@ -243,6 +243,8 @@ GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \ gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
break; \ break; \
} \ } \
default: \
break; \
} \ } \
} \ } \
} else \ } else \

View File

@ -1471,6 +1471,8 @@ class Hunk {
++adds_; ++adds_;
hunk_adds_.push_back(std::make_pair('+', line)); hunk_adds_.push_back(std::make_pair('+', line));
break; break;
default:
break;
} }
} }
@ -3281,6 +3283,7 @@ static const char* GetAnsiColorCode(GTestColor color) {
return "2"; return "2";
case GTestColor::kYellow: case GTestColor::kYellow:
return "3"; return "3";
case GTestColor::kDefault:
default: default:
assert(false); assert(false);
return "9"; return "9";
@ -3537,6 +3540,9 @@ void PrettyUnitTestResultPrinter::OnTestPartResult(
// If the test part succeeded, we don't need to do anything. // If the test part succeeded, we don't need to do anything.
case TestPartResult::kSuccess: case TestPartResult::kSuccess:
return; return;
case TestPartResult::kNonFatalFailure:
case TestPartResult::kFatalFailure:
case TestPartResult::kSkip:
default: default:
// Print failure message from the assertion // Print failure message from the assertion
// (e.g. expected this and got that). // (e.g. expected this and got that).
@ -3755,6 +3761,9 @@ void BriefUnitTestResultPrinter::OnTestPartResult(
// If the test part succeeded, we don't need to do anything. // If the test part succeeded, we don't need to do anything.
case TestPartResult::kSuccess: case TestPartResult::kSuccess:
return; return;
case TestPartResult::kNonFatalFailure:
case TestPartResult::kFatalFailure:
case TestPartResult::kSkip:
default: default:
// Print failure message from the assertion // Print failure message from the assertion
// (e.g. expected this and got that). // (e.g. expected this and got that).

View File

@ -346,7 +346,7 @@ TEST_F(TestForDeathTest, SwitchStatement) {
ASSERT_DEATH(_Exit(1), "") << "exit in default switch handler"; ASSERT_DEATH(_Exit(1), "") << "exit in default switch handler";
switch (0) switch (0)
case 0: default:
EXPECT_DEATH(_Exit(1), "") << "exit in switch case"; EXPECT_DEATH(_Exit(1), "") << "exit in switch case";
GTEST_DISABLE_MSC_WARNINGS_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_()
@ -1499,7 +1499,7 @@ TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {
ASSERT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in default switch handler"; ASSERT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in default switch handler";
switch (0) switch (0)
case 0: default:
EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in switch case"; EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in switch case";
GTEST_DISABLE_MSC_WARNINGS_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_()

View File

@ -239,7 +239,7 @@ TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
} }
switch (0) switch (0)
case 0: default:
GTEST_CHECK_(true) << "Check failed in switch case"; GTEST_CHECK_(true) << "Check failed in switch case";
} }

View File

@ -63,6 +63,7 @@ class MyEnvironment : public testing::Environment {
case FATAL_FAILURE: case FATAL_FAILURE:
FAIL() << "Expected fatal failure in global set-up."; FAIL() << "Expected fatal failure in global set-up.";
break; break;
case NO_FAILURE:
default: default:
break; break;
} }

View File

@ -4253,7 +4253,7 @@ TEST(AssertionSyntaxTest, WorksWithSwitch) {
} }
switch (0) switch (0)
case 0: default:
EXPECT_FALSE(false) << "EXPECT_FALSE failed in switch case"; EXPECT_FALSE(false) << "EXPECT_FALSE failed in switch case";
// Binary assertions are implemented using a different code path // Binary assertions are implemented using a different code path
@ -4265,7 +4265,7 @@ TEST(AssertionSyntaxTest, WorksWithSwitch) {
} }
switch (0) switch (0)
case 0: default:
EXPECT_NE(1, 2); EXPECT_NE(1, 2);
} }