For me a lot depends on where the kid said it. If it was during lunch, passing period, in the bus line, etc. (all "free" times) and it was in a private conversation, then I think the teacher was out of line both eavesdropping and then taking action on something that was said.
I could see the teacher clearing their throat or raising an eyebrow, just to let the kid know they had been heard, but formal discipline smacks of a thin skin and a tendency to abuse power--in short, a jerk!
If it was during class/study hall/library time, then I think the teacher was within their rights to issue a detention.