Cancer risk is influenced by inherited mutations, DNA replication errors, and environmental factors. However, the influence of genetic variation in immunosurveillance on cancer risk is not well understood. Leveraging population-level data from the UK Biobank and FinnGen, we show that heterozygosity at the human leukocyte antigen (HLA)-II loci is associated with reduced lung cancer risk in smokers. Fine-mapping implicated amino acid heterozygosity in the HLA-II peptide binding groove in reduced lung cancer risk, and single-cell analyses showed that smoking drives enrichment of proinflammatory lung macrophages and HLA-II+ epithelial cells. In lung cancer, widespread loss of HLA-II heterozygosity (LOH) favored loss of alleles with larger neopeptide repertoires. Thus, our findings nominate genetic variation in immunosurveillance as a critical risk factor for lung cancer.