Safe way to swap/move plugins in tracks?

I searched in examples but I couldn’t find one.
For the time being, I made very simple function, it works fine for now. Is this OK?

void movePlugin(te::Plugin::Ptr from, te::Plugin::Ptr to)
{
    if (auto destTrack = to->getOwnerTrack())
    {
        int index = destTrack->pluginList.indexOf(to.get());
        from->deleteFromParent();
        destTrack->pluginList.insertPlugin(from, index, nullptr);
    }
}

Now there are problems when destination track has no plugin, or when I want to move a plugin to the last of plugin-chain. (But it’s okay for me because what I am making has only one track always, for now)
I want to ask you whether there are any pitfalls in my function.
Any ideas or advices will much be appreciated!
Thanks.

Yes, that is fine.

In future you may need to worry about freeze points on the track and reconnecting rack inputs. If you don’t use those features, then no need to worry about them.

Thanks!

In future you may need to worry about freeze points on the track and reconnecting rack inputs.

OK, I’ll keep it in mind!

Can I request a tutorial or an example about how to move plugins?
It would be great if PluginDemo had such function or if there is fine-tuned movePlugin() in EngineHelper namespace.
I’m happy if you consider it to issue list.
Where should I send a request?
Thanks.

What you’ve written will work. The problem with the position is that you’re assuming the source and destination indices are the same. You probably want to be more explicit about what index you’re inserting in to?

Thanks!