Repository: microsoft/terminal
File: src/cascadia/TerminalApp/AppActionHandlers.cpp (C++)

Code before:
            control.SelectAll();
            args.Handled(true);
        }
    }

    void TerminalPage::_HandleSaveSnippet(const IInspectable& /*sender*/,
                                       const ActionEventArgs& args)
    {
        if constexpr (!Feature_SaveSnippet::IsEnabled())
        {
            return;
        }

        if (args)
        {
            if (const auto& realArgs = args.ActionArgs().try_as<SaveSnippetArgs>())
            {
                if (realArgs.Commandline().empty())
                {
                    if (const auto termControl{ _GetActiveControl() })
                    {
                        if (termControl.HasSelection())
                        {
                            const auto selections{ termControl.SelectedText(true) };
                            const auto selection = std::accumulate(selections.begin(), selections.end(), std::wstring());
                            realArgs.Commandline(selection);
                        }
                    }
                }

                if (realArgs.Commandline().empty())
                {
                    ActionSaveFailed(L"CommandLine is Required");
                    return;
                }

                try
                {
                    winrt::Microsoft::Terminal::Control::KeyChord keyChord = nullptr;
                    if (!realArgs.KeyChord().empty())
                    {
                        keyChord = KeyChordSerialization::FromString(winrt::to_hstring(realArgs.KeyChord()));
                    }
                    _settings.GlobalSettings().ActionMap().AddSendInputAction(realArgs.Name(), realArgs.Commandline(), keyChord);
                    _settings.WriteSettingsToDisk();
                    ActionSaved(realArgs.Commandline(), realArgs.Name(), realArgs.KeyChord());
                }
                catch (const winrt::hresult_error& ex)
                {
                    auto code = ex.code();
                    auto message = ex.message();

Review comment (refactor):
This shouldn't modify the `realArgs.Commandline()` but instead extract the hstring and then modify the local reference. Like this:

```cpp
auto commandline = realArgs.Commandline();
if (commandline.empty() {
    // ...
    commandline = selection;
}

if (commandline.empty()) {
    // ...
}
```

Code after:
            control.SelectAll();
            args.Handled(true);
        }
    }

    void TerminalPage::_HandleSaveSnippet(const IInspectable& /*sender*/,
                                          const ActionEventArgs& args)
    {
        if constexpr (!Feature_SaveSnippet::IsEnabled())
        {
            return;
        }

        if (args)
        {
            if (const auto& realArgs = args.ActionArgs().try_as<SaveSnippetArgs>())
            {
                auto commandLine = realArgs.Commandline();
                if (commandLine.empty())
                {
                    if (const auto termControl{ _GetActiveControl() })
                    {
                        if (termControl.HasSelection())
                        {
                            const auto selections{ termControl.SelectedText(true) };
                            const auto selection = std::accumulate(selections.begin(), selections.end(), std::wstring());
                            commandLine = selection;
                        }
                    }
                }

                if (commandLine.empty())
                {
                    ActionSaveFailed(L"CommandLine is Required");
                    return;
                }

                try
                {
                    KeyChord keyChord = nullptr;
                    if (!realArgs.KeyChord().empty())
                    {
                        keyChord = KeyChordSerialization::FromString(winrt::to_hstring(realArgs.KeyChord()));
                    }
                    _settings.GlobalSettings().ActionMap().AddSendInputAction(realArgs.Name(), commandLine, keyChord);
                    _settings.WriteSettingsToDisk();
                    ActionSaved(commandLine, realArgs.Name(), realArgs.KeyChord());
                }
                catch (const winrt::hresult_error& ex)
                {
                    auto code = ex.code();