Add [[nodiscard]] to GetParam() to prevent accidental misuse.

This helps avoid a situation where someone sets up a parameterized test but forgets to actually use the parameter.

PiperOrigin-RevId: 758455362
Change-Id: Ie4db03e82b6a4e1787be96f154b3fbb25657ae64
This commit is contained in:
Mike Kruskal 2025-05-13 18:25:24 -07:00 committed by Copybara-Service
parent 8b8ef3ff0d
commit 9f79a9597a
2 changed files with 2 additions and 2 deletions

View File

@ -1693,7 +1693,7 @@ class WithParamInterface {
// The current parameter value. Is also available in the test fixture's // The current parameter value. Is also available in the test fixture's
// constructor. // constructor.
static const ParamType& GetParam() { [[nodiscard]] static const ParamType& GetParam() {
GTEST_CHECK_(parameter_ != nullptr) GTEST_CHECK_(parameter_ != nullptr)
<< "GetParam() can only be called inside a value-parameterized test " << "GetParam() can only be called inside a value-parameterized test "
<< "-- did you intend to write TEST_P instead of TEST_F?"; << "-- did you intend to write TEST_P instead of TEST_F?";

View File

@ -1174,7 +1174,7 @@ TEST_P(ParameterizedDerivedTest, SeesSequence) {
class ParameterizedDeathTest : public ::testing::TestWithParam<int> {}; class ParameterizedDeathTest : public ::testing::TestWithParam<int> {};
TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) { TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
EXPECT_DEATH_IF_SUPPORTED(GetParam(), ".* value-parameterized test .*"); EXPECT_DEATH_IF_SUPPORTED((void)GetParam(), ".* value-parameterized test .*");
} }
INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest, INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest,