[PATCH] D141757: [clangd] allow extracting to variable for lambda expressions

Julian Schmidt via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 29 16:25:12 PDT 2023


5chmidti added inline comments.


================
Comment at: clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp:178
+      // Allow all expressions except partial LambdaExpr selections since we
+      // don't want to extract from the captures/default arguments of a lambda
+      if (isa<clang::Expr>(Stmt)) {
----------------
Noticed that this can be removed because extracting from captures is allowed. Will change in the next update


================
Comment at: clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp:182-184
+        if (InsertionPoint->Parent->ASTNode.get<ParmVarDecl>() != nullptr) {
+          return false;
+        }
----------------
This is supposed to stop the following invalid code from happening:
```
void foo() {
  int placeholder = 42;
  [](int x = placeholder {};
  extern void bar(int x = placeholder);
}
```

clangd does not seem to support extracting from the initializers of defaulted parameters, should I keep the condition as is, or should I do something different here? It is legal to have default arguments in global scope (examples below).

The following insertions could exist (out of scope for this patch):
```
int placeholder = 42;
void foo() {
  [](int x = placeholder {};
  extern void bar(int x = placeholder);
}
```
```
struct X {
  static inline int placeholder = 42;
  void foo(int x = placeholder) {}
};
```

Either way, I'll have to adjust the comment because this is not just to stop default parameter initializers in lambdas from being extracted to a local scope.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141757/new/

https://reviews.llvm.org/D141757



More information about the cfe-commits mailing list