Herein, we report the rational synthesis of porous g-C3N4 co-modified with oxygen (O) and fluorine (F) for the first time. Incorporating colloidal SiO2 during thermal polymerization introduces lattice oxygen, forming C–O bonds, while post-treatment with NH4·HF2 establishes C–F bonds. The dual incorporation of O and F elements extends visible light absorption and effectively promotes the separation and transport of photoexcited charge carriers. Consequently, the co-modified g-C3N4 (O,F-g-C3N4) achieves a 13.2-fold increase in H2 evolution rate compared to pristine g-C3N4. This synthesized O,F-g-C3N4 is then dispersed in waterborne polyurethane (WPU) to create an anti-corrosive coating for Q235 carbon steel substrates. Water resistance, mechanical property, and electrochemical characterization analyses reveal that the O,F-g-C3N4/WPU composite coating exhibits remarkable corrosion resistance with a high protection efficiency of 90.23%. This work offers a straightforward approach for developing highly efficient g-C3N4-based photocatalysts and corrosion-resistant coatings.