Issue 58146
Summary [clang-format] MAYBE BUG: Inline lambda will move following params, to the nowhere
Labels new issue
Assignees
Reporter florianbecker
    Input:
```cpp
/* stl header */
#include <functional>
#include <iostream>

/* modern.cpp.core */
#include <Timer.h>

constexpr int intervallSeconds = 1;
constexpr int secondsToMilliseconds = 1000;
constexpr int exitSeconds = 15;

int main() {

  int intervall = 1;
  auto intervallTimer = vx::Timer();

  intervallTimer.setInterval( [ &intervallTimer, &intervall ]() {
    std::cout << "Intervall: " << intervall << std::endl;
    intervall++;
    if ( intervall >= exitSeconds ) {

      intervallTimer.stop();
    }
  }, intervallSeconds * secondsToMilliseconds );

  while ( true ) {

    /* Leave the app run infinity */
    if ( !intervallTimer.isRunning() ) {

      break;
    }
  }

  return EXIT_SUCCESS;
}
```

Will end like that:
```cpp
/* stl header */
#include <functional>
#include <iostream>

/* modern.cpp.core */
#include <Timer.h>

constexpr int intervallSeconds = 1;
constexpr int secondsToMilliseconds = 1000;
constexpr int exitSeconds = 15;

int main() {

  int intervall = 1;
  auto intervallTimer = vx::Timer();

  intervallTimer.setInterval( [ &intervallTimer, &intervall ]() {
    std::cout << "Intervall: " << intervall << std::endl;
    intervall++;
    if ( intervall >= exitSeconds ) {

      intervallTimer.stop();
    }
  },
                              intervallSeconds * secondsToMilliseconds );

  while ( true ) {

    /* Leave the app run infinity */
    if ( !intervallTimer.isRunning() ) {

      break;
    }
  }

  return EXIT_SUCCESS;
}
```

Workaround:
Define a function and do not write the lambda inline. Will keep the formatting.
```cpp
/* stl header */
#include <functional>
#include <iostream>

/* modern.cpp.core */
#include <Timer.h>

constexpr int intervallSeconds = 1;
constexpr int secondsToMilliseconds = 1000;
constexpr int exitSeconds = 15;

int main() {

  int intervall = 1;
  auto intervallTimer = vx::Timer();

  using func = std::function<void( void )>;
  const func runOnInterval = [ &intervallTimer, &intervall ]() {
    std::cout << "Intervall: " << intervall << std::endl;
    intervall++;
    if ( intervall >= exitSeconds ) {

      intervallTimer.stop();
    }
  };

  intervallTimer.setInterval( runOnInterval, intervallSeconds * secondsToMilliseconds );

  while ( true ) {

    /* Leave the app run infinity */
    if ( !intervallTimer.isRunning() ) {

      break;
    }
  }

  return EXIT_SUCCESS;
}
```

Tested on clang-format-15 and clang-format-14

My configuration:
[clang-format.txt](https://github.com/llvm/llvm-project/files/9709136/clang-format.txt)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to