LLVM Discussion Forums

Clang-tidy readability-braces-around-statements

First time posting. Let me know if I’m in the wrong place or otherwise not following guidelines.

I’m trying to integrate clang-tidy into my codebase and I get a few unexpected errors that I’d like to understand.

To begin with, I’m getting readability-braces-around-statements in a strange spot.

Error:

/path/foo.cpp:15:77: error: statement should be inside braces [readability-braces-around-statements,-warnings-as-errors]
    if constexpr (std::is_invocable_v<Fn, T> || std::is_invocable_v<Fn, T&>) {
                                                                            ^
                                                                             {
/path/foo.cpp:17:54: error: statement should be inside braces [readability-braces-around-statements,-warnings-as-errors]
    } else if constexpr (std::is_invocable_v<Fn, T*>) {
                                                     ^
                                                      {

(Minimized) Code:

#include <algorithm>
#include <type_traits>

template <typename...>
constexpr std::false_type INVALID_SYNC_FUNCTION{};

template <typename T>
struct wrapper {
  T val;

  template <typename Fn>
  auto exec(Fn&& fn) {
    if constexpr (std::is_invocable_v<Fn, T> || std::is_invocable_v<Fn, T&>) {
      return fn(val);
    } else if constexpr (std::is_invocable_v<Fn, T*>) {
      return fn(&val);
    } else if constexpr (std::is_invocable_v<Fn>) {
      return fn();
    } else {
      static_assert(INVALID_SYNC_FUNCTION<Fn>);
    }
  }

  template <typename Fn>
  auto bar(Fn&& fn) {
    return exec([&]() {
      return exec(std::forward<Fn>(fn));
    });
  }
};

int foo(wrapper<int>* abc) {
  return abc->bar([](int x) {
    return x;
  });
}

I tried to minimize the code as much as possible. Further reductions seem to make the errors disappear.

For example, if I change the definition of bar to the following

template <typename Fn>
auto bar(Fn&& fn) {
  return exec(std::forward<Fn>(fn));
}

the errors disappear.

Any idea what’s going on?