Are you sure that everything you think is technically illegal actually is illegal? The examples that you give that most people run afoul of might well, in the right context, be considered fair use.
However I'd be far more dubious that the isolated snippet of code from someone's proprietary code base would be considered fair use. (You clearly feel it to be fair use, but I'm suspecting that a court would consider the circumstances around the use to be very different.)
Of course, as with all things copyright, you only get in serious trouble if the copyright holder comes after you. Which in this situation is rather unlikely to happen.