This sounds like typical Sonar behaviour to me. If the code you start with is already failing, you’d better hope you never have to change it, not even to make improvements, because as soon as you try to make one improvement in the code, the quality gate will kick you in the balls for not fixing every other issue in the same area.
Some classic examples of this from recent memory:
- I removed a warning suppression and Sonar failed the quality gate complaining about suppressing warnings.
- I made a line of code shorter and Sonar quality gate complained about the line of code being too long.
- I fixed the indenting on a line and Sonar quality gate complained about some existing issue that happened to have code on the same line.
I think Sonar is just not current designed in a way to encourage people to improve code quality because most of the time, when trying to do so, the quality gate prevents you making the change.
Rather than doing warning accounting on a line by line basis, it should be looking at the total warnings before and after and only complaining if the total went up.