[clang-tools-extra] [clangd] Extract to function: Pass unmodified scalar parameters by value (PR #227675)
Aleksandr Platonov via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 1 00:12:08 PDT 2026
================
@@ -1014,15 +1014,25 @@ bool createParameters(NewFunction &ExtractedFunc,
return false;
// Parameter qualifiers are same as the Decl's qualifiers.
QualType TypeInfo = VD->getType().getNonReferenceType();
- // Add const if it's not (conservatively) mutated in the zone: it's
- // still passed by reference to avoid a copy, but the reference doesn't
- // need to be mutable. Array types are never made const: mutating array
- // elements through a non-const-ref loop variable or a decayed pointer
- // argument is common and easy to miss conservatively, so we don't try.
- if (!DeclInfo.IsPossiblyMutated && !TypeInfo->isArrayType())
- TypeInfo.addConst();
// FIXME: check if parameter will be a non l-value reference.
bool IsPassedByReference = true;
+ if (!DeclInfo.IsPossiblyMutated) {
+ // A scalar (arithmetic, pointer, enumeration, ...) is at least as
+ // cheap to copy as to pass by reference, and less noisy. Any
+ // pre-existing const is dropped: it's a no-op on a by-value
----------------
ArcsinX wrote:
Unsure that it's right to drop const.
E.g.
```cpp
void bar(int &);
void bar(const int &);
void foo(const int A) {
[[
bar(A); // calls bar(const int &)
]]
}
```
After extraction:
```cpp
void extracted(int A) {
bar(A); // calls bar(int &), behavior changed!
}
void foo(const int A) {
extracted(A);
}
```
https://github.com/llvm/llvm-project/pull/227675
More information about the cfe-commits
mailing list