[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:34 PDT 2026


================
@@ -130,7 +135,18 @@ const Node *getParentOfRootStmts(const Node *CommonAnc) {
   const Node *Parent = nullptr;
   switch (CommonAnc->Selected) {
   case SelectionTree::Selection::Unselected:
-    // Typically a block, with the { and } unselected, could also be ForStmt etc
+    // Typically a block, with the { and } unselected, could also be ForStmt
+    // etc. However, CommonAnc may instead be a single statement that is
+    // itself Unselected only because all of its own tokens are claimed by
+    // its children (see isUnselectedRootStmtCandidate); in that case it's a
+    // root statement in its own right, and we need its actual parent, same
+    // as in the Complete case below.
+    if (isUnselectedRootStmtCandidate(CommonAnc)) {
+      Parent = CommonAnc->Parent;
+      if (Parent->ASTNode.get<DeclStmt>())
----------------
timon-ul wrote:

I was a bit confused about this condition until I scrolled down a few lines and saw the same statement with a comment. Either you move said comment up and refer to it below or you factor it ous as a function (or if you have any other nice ideas do it that way), but I think we should not have it where the comment comes with the 2nd time this logic is used.

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


More information about the cfe-commits mailing list