This is very much wrong, and something I've always disagreed with.
The reasoning behind it, is 'conflict of interest' (I'm just passing on the reason I was told when I worked for 7-11). The employees in the store look at a 'product forcast', decide how many cookies to make (heat up some pre-made dough) and package for sale. If they are permitted to keep or donate expiring product: they may intentionally make more than needed, ensuring they get free stuff. This goes for all of 7-11s 'made in house' (assembled as best, usually just re-heated. Fried Chicken was the closest to 'fresh' they sell) products. Cookies, sandwiches, hot food, etc.
I get that viewpoint; but I think they should punish abuse of the system, not outright prohibit saving perfectly good food, if nearly expiring/expired, for good causes like the needy/homeless.