<!--
title: Judicious move semantics
domain: runtimes
topic: Code Style
language: C++
source: oven-sh/bun
updated: 2025-06-03
url: https://awesomereviewers.com/reviewers/bun-judicious-move-semantics/
-->

Apply C++ move semantics and lambda modifiers only when necessary. The `mutable` keyword should only be used in lambdas when you need to modify captured variables, including when using move operations on them:

```cpp
// Correct: mutable required for WTFMove(message)
context->postImmediateCppTask([protectedThis = Ref { *this }, message = WTFMove(message)](ScriptExecutionContext& ctx) mutable {
    // Using WTFMove(message) inside the lambda
});

// Incorrect: unnecessary mutable
context->postImmediateCppTask([protectedThis = Ref { *this }, message](ScriptExecutionContext& ctx) mutable {
    // No modification of captured variables
});
```

Similarly, avoid redundant moves on function objects unless there's a specific rvalue ref-qualified overload of `operator()` you intend to call:

```cpp
// Correct: Direct invocation
completionCallback();

// Unnecessary: redundant move
WTFMove(completionCallback)();
```

Using move operations judiciously improves code clarity and helps avoid subtle bugs related to object ownership.
