I use X-Mouse Button Control, but you can use any other macro software such as Razer Synapse or if your mouse has a proprietary macro software
Basically, introduce a delay between when you press the key and when the key is actually activated. This prevents the simultaneous or almost simultaneous pressing of both Melee and Weapon Switch keys, which is what triggers the bug.
You’re welcome