The concept of Defense in Depth takes that into account. When they are far, far away, bombs from the air. A little bit closer? Artillery. Closer still? Tanks, then mortars, rifles, handguns, bayonetes, then hands, then teeth. The plan is by the time you get to the last four options they are all dead or no longer a threat.
The last bayonet charge carried out by the US Army was in Korea on February 7, 1951, led by Captain Lewis Lee Millett. He was awarded the Medal of Honor for his actions but also privately told not to do anything that stupid again. About 20 of the 50 dead Chinese were found to have been killed by bayonets. The citation reads:
Just keep in mind what Murphy Law has to say on the matter - final protective fire doesn’t.
I wouldn’t be surprised if it hasn’t happened in Africa in recent days. In addition, out-of-date weaponry is quite common among the more desperate militias there. Ammo might be hard to get for some groups, so … use the blade.
Yes, but… you can’t rely on things always working out that way. If enemy troops are suddenly right on top of you (or if you’re suddenly right on top of them) asking them to back up a bit so you can shoot them may not be effective. Faced with the possibility of such a situation, having the option of affixing bayonets seems to me a sensible precaution.