Skip to content

Extract to function incorrectly marks refactoring as unavailable when using for loop with a lambda. #12711

Open
@KKhanhH

Description

@KKhanhH

Environment

  • OS and Version: Windows 10
  • VS Code Version: 1.91.1
  • C/C++ Extension Version: 1.21.6
  • If using SSH remote, specify OS of remote machine:

Bug Summary and Steps to Reproduce

Bug Summary:
When using a lambda with a return value inside of a for loop, cpptools will not allow for extracting to function refactor. The extract to function button is grayed out in the context menu with no reason as to why. When the keyboard shortcut is used (ctrl-shift-r & ctrl-e), the reason given for not being able to extract is because the for loop has control paths that exit without the return value.

Minimal reproducible example:

std::vector<int> testFunction(int a, int b, int size) {
    std::vector<int> vec;
    auto testAdd = [&]() {
        return a+b;
    };
    for(int i = 0; i < size; i++) {
        vec.push_back(a+b);
    }
}

Steps to reproduce:

  1. Select the lambda and the for loop.
  2. Press the key combination for extract to function
  3. Receive a tooltip mentioning the above error

Expected behavior:
Extract to function is successful and prompts with function name.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions