[all-commits] [llvm/llvm-project] d9b12f: [clangd] Extract to Function: mark unmodified capt...

Christian Kandeler via All-commits all-commits at lists.llvm.org
Wed Sep 30 03:43:24 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: d9b12f5c63ff211229a1809a6878c32fc166c14a
      https://github.com/llvm/llvm-project/commit/d9b12f5c63ff211229a1809a6878c32fc166c14a
  Author: Christian Kandeler <christian.kandeler at qt.io>
  Date:   2026-09-30 (Wed, 30 Sep 2026)

  Changed paths:
    M clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
    M clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp

  Log Message:
  -----------
  [clangd] Extract to Function: mark unmodified captured parameters const

Every captured variable was always passed by non-const reference, even when the extracted code never modifies it, resulting in misleading function signatures.

Determine, for each captured variable, whether it's ever (possibly) mutated within the extraction zone, and add `const` to the parameter's type when it isn't. Still passed by reference either way, to avoid a copy.

The check is folded directly into the existing zone traversal (`captureZoneInfo`'s `ExtractionZoneVisitor`), so its cost stays proportional to the size of the code being extracted. Direct mutations (assignment, increment/decrement, non-const method calls, explicit casts to non-const reference, non-const-reference call arguments, ...) are recognized precisely from each occurrence's immediate syntactic context. Anything that aliases a captured variable (a reference bound to it, its address taken, capture by reference in a lambda, a forwarding-reference call argument, a non-const-ref range-for loop variable) is conservatively treated as a possible mutation, without tracing whether the alias itself is later mutated -- trading a little precision in alias-heavy code for a simple, linear-cost check.

Array-typed captures are never made const, since array-to-pointer decay and array-element mutation have enough edge cases that it wasn't worth special-casing for a rare pattern.

Assisted-by: Claude



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list