The fuel tank cap does more than keep dirt out. It seals the evaporative emissions (EVAP) system. The cap has a pressure-relief valve inside. This valve maintains the correct pressure in the tank. If the seal fails, fuel vapors leak out, which triggers emission codes.
A worn rubber seal on the cap is the leading cause of EVAP leak codes. The seal flattens and cracks over time. Replacing the cap is the simplest fix for a recurring check engine light.