[clang-tools-extra] [clangd] Offer Extract to Function for single expression-statements (PR #219945)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 8 06:07:35 PDT 2026


================
@@ -298,11 +314,6 @@ computeEnclosingFuncRange(const FunctionDecl *EnclosingFunction,
 // returns true if Child can be a single RootStmt being extracted from
 // EnclosingFunc.
 bool validSingleChild(const Node *Child, const FunctionDecl *EnclosingFunc) {
-  // Don't extract expressions.
-  // FIXME: We should extract expressions that are "statements" i.e. not
-  // subexpressions
-  if (Child->ASTNode.get<Expr>())
----------------
timon-ul wrote:

Not sure if this is the cause, but currently I can extract subexpressions, which I shouldn't be able to (and which produce broken code. Here my example:
```
void test() {
std::cout << ^3;
}
```
I get offered the "extract to function" (which I am sure I shouldn't get offered) and it turns into
```
void extracted() { 3; }
void test() {
std::cout << extracted();
}
```
which obviously is broken code.

Maybe you should also add a test for this case.

https://github.com/llvm/llvm-project/pull/219945


More information about the cfe-commits mailing list