Accurate computational predictions of band gaps are of practical importance to the modeling and development of semiconductor technologies, such as (opto)electronic devices and photoelectrochemical cells. Among available electronic-structure methods, density-functional theory (DFT) with the Hubbard U correction (DFT+U) applied to band edge states is a computationally tractable approach to improve the accuracy of band gap predictions beyond that of DFT calculations based on (semi)local functionals. At variance with DFT approximations, which are not intended to describe optical band gaps and other excited-state properties, DFT+U can be interpreted as an approximate spectral-potential method when U is determined by imposing the piecewise linearity of the total energy with respect to electronic occupations in the Hubbard manifold (thus removing self-interaction errors in this subspace), thereby providing a (heuristic) justification for using DFT+U to predict band gaps. However, it is still frequent in the literature to determine the Hubbard U parameters semiempirically by tuning their values to reproduce experimental band gaps, which ultimately alters the description of other total-energy characteristics. Here, we present an extensive assessment of DFT+U band gaps computed using self-consistent ab initio U parameters obtained from density-functional perturbation theory to impose the aforementioned piecewise linearity of the total energy. The study is carried out on 20 compounds containing transition-metal or p-block (group III-IV) elements, including oxides, nitrides, sulfides, oxynitrides, and oxysulfides...