I don't think it's a silly question, but it is a common error to anthropomorphise animals.
Insects respond to cues which they have evolved to respond to, and this is how they 'make decisions'. They do not have free will or any more complex decision-making process like common sense. This is evident is lots of insect behaviour: flying repeatedly at a closed window; landing on brightly coloured clothes instead of flowers; and returning to a food source when they are in real danger of being swatted!
When a fly senses the food (often by olfactory receptors), they are 'programmed' to fly towards it in response to some chemical they sense depending on the species and food. They may not have adapted a response to swatting, or perhaps the food cue overrides others. In nature swatting is not so much of a threat to a fly. Some animals may brush them away, but since they are not really doing any harm in feeding from another animal's food, they are mostly ignored.
CO2 traps are used to entice and kill mosquitoes. The mosquitoes are attracted to CO2 (as it is dispelled from the animals from which they blood feed), they will only evolve to avoid the traps if there is another cue which they could eventually associate with a negative effect.
Another thing to remember when thinking about insect behaviour is that their life strategy is very different to ours. Insects are more r-selected than humans, meaning that each individual life has not had so much energy put into it as a more K-selected animal (such as humans), and to compensate for this, many more young are produced. This often results in more risks being taken by individuals since there will still be a viable population even after many deaths.
Chapter 4 of 'The Insects' by Gullan & Cranston gives a good introduction to the sensory responses of insect behaviour. There are other books on the subject, 'Introduction to insect behaviour' by Atkins looks like a good starting point, but I have not read it yet.