There are few reasonable uses for expanding foam in this context. I think the rust issue may have been with early versions or with latex foam, which wouldn't be appropriate here. I can't swear there aren't any rust problems, but I did have some in a car for 5 years before removing most of it and saw no evidence of corrosion.
The first important point is that it really isn't removable without going to great lengths. Even then, you won't be able to get the sheet metal clean again without removing the paint or e-coat at the same time. It's meant for weatherproofing and is very durable. It forms a rigid closed cell material that is quite inert - materially and acoustically. It degrades when exposed to UV, so exposed areas may need to be painted. It is very messy stuff to apply.
All of that said, there are two applications where it might be useful. The first is filling voids that would otherwise transmit sound from one area to another. Rear quarter panels are an ideal illustration if they have inner and outer skins. Fill the void with expanding foam and air won't move through any more. If this is part of a noise reduction strategy, it would probably be better to fill the void with an absorbent material.
The second is in reinforcing panels to resist deformation caused by high SPL. Quarter panels are a good example here too - fill the void and you effectively lock the inner skin to the outer skin, strengthening both. Filling the space between the trunk lid outer skin and inner skin may strengthen both. If you're just trying to stop rattles between the 2, a few dabs of RTV silicone is a much neater solution.