googletest icon indicating copy to clipboard operation
googletest copied to clipboard

[FR]: `EXPECT_NO_THROW` and `ASSERT_NO_THROW` don't play nice with `[[nodiscard]]`

Open Jacobfaib opened this issue 2 months ago • 0 comments

Does the feature exist in the most recent commit?

Tested on v1.13.0, but the same problem also exists at head:

https://github.com/google/googletest/blob/79219e26e0e36b415a5804b6b017ad6c6cd99ad8/googletest/include/gtest/internal/gtest-internal.h#L1367-L1373

where GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_() expands as

https://github.com/google/googletest/blob/79219e26e0e36b415a5804b6b017ad6c6cd99ad8/googletest/include/gtest/internal/gtest-internal.h#L1312-L1316

Why do we need this feature?

I can work around it by doing

[[nodiscard]] int a_function_that_throws();

ASSERT_NO_THROW(static_cast<void>(a_function_that_throws()), ...)

but I feel like gtest should render this nicety for me.

Describe the proposal.

Wrap statement in static_cast<void>(statement) or std::ignore = statement. The former is probably more general.

Is the feature specific to an operating system, compiler, or build system version?

No.

Jacobfaib avatar Dec 11 '24 22:12 Jacobfaib