My solution was to change the Analysis scope option ("Show compiler errors and warnings for") from Entire solution to something else, like Open documents or Current document.
You can find this setting in Tools → Options → Text Editor → C# → Advanced → Analysis.