[clang-tools-extra] [clang-tidy] Filter out googletest TUs in bugprone-unchecked-optional-access (PR #115051)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 13 15:36:16 PST 2024


https://github.com/5chmidti requested changes to this pull request.

I don't think we can ignore a TU simply because it has the Google Test header included, which this will do.
This would ignore real problems, such as

```c++
#include <gtest/gtest.h>
#include <optional>
void issue(const absl::optional<int> &opt) {
  if (!opt.has_value())
  *opt;
}
```

---

The issue with the Google Test macros seems to be, that the dataflow check does not understand using a helper for the cast to `bool` like this:

```c++
#include <optional>

struct Converter {
    template <typename T>
    Converter(T&& val) : success{static_cast<bool>(std::forward<T>(val))} {}
    operator bool() const { return success; }
    bool success;
};

void bar(std::optional<int> v) {
    switch (0)
    case 0:
    default:
        if (const auto wrapped_check = Converter{v})
            ;
        else
            return;
    auto val = *v;
}
```

https://godbolt.org/z/7EdsGYhaW

(found the FP by expanding the `ASSERT_TRUE` macro and reducing manually)

https://github.com/llvm/llvm-project/pull/115051


More information about the cfe-commits mailing list