Regain air bubbles before reaching water surface
While swimming you can regain air bubbles at around 0.8 blocks from water surface, if you do a strong attack on a mob while underwater and swimming. If you were not swimming, hit a mob and started swimming the bug will not occur, and once you go over the surface it will be fixed.