Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to space out trailing return type of lambda with clang-format

There are a few things blocking me from switching to clang-format. When I have a trailing return type on a lambda that should wrap to the next line there is no space between the arrow and the trailing return type. How can I fix this?

For example this is the output from clang-format for the unformatted version of the same code below

auto func() {
    return [.......](auto one, auto long_parameter_list, auto another)
        ->SomeLongReturnType;
//      ^^^^^^^^^^^^^^^^^^^^^ How can I add a space in between those?
}
like image 297
Curious Avatar asked Oct 30 '22 00:10

Curious


1 Answers

TL;TR: Upgrade to clang-format 7.0 or later.

The clang-format 7.0 comes with fixes related to the trailing return, so you should be able to achieve the desired formatting:

Before

template <int K, typename E, typename L, int N>
auto ccccccccccccccccccccccc(detail::base<E, L, N>& p) -> std::add_lvalue_reference<E>::type;

After:

template <int K, typename E, typename L, int N>
auto ccccccccccccccccccccccc(detail::base<E, L, N> &p)
    -> std::add_lvalue_reference<E>::type;

Related is a subtle issue that clang-format 7 still suffers from https://bugs.llvm.org/show_bug.cgi?id=42835 and it does not indent the breaking of the trailing return if there is typename used:

Before:

template <int K, typename E, typename L, int N>
auto bbbbbbbbbbbbbbbbbbbbbbb(detail::base<E, L, N>& p) -> typename std::add_lvalue_reference<E>::type;

After:

template <int K, typename E, typename L, int N>
auto bbbbbbbbbbbbbbbbbbbbbbb(detail::base<E, L, N> &p) ->
    typename std::add_lvalue_reference<E>::type;

Related mailing list thread: [cfe-users] [clang-format] Trailing return type

like image 72
mloskot Avatar answered Nov 15 '22 07:11

mloskot