Clef change at start of system on cutaway staff causes missing barline and extra staff lines
Discovered this working on #315861: Musescore 3.6 - Hidden staff clef changes from other parts appearing in top staff of visible system. The trigger seems to be a staff set to hide when empty and also cutaway, where the staff does not appear on one system but does in the first measure of the next, and that measure has a clef change that involves a courtesy clef. The original bug (and it affected all hidden staves, not just cutaway) was the courtesy clef would actually appear on the first staff of the first system. My PR https://github.com/musescore/MuseScore/pull/7306 fixes that. However, if the hidden staff is also a cutaway, two other bad things happen:
the top staff of the first system gets an extra set of staff lines drawn to support the courtesy clef that was erroneously being drawn there
the staff above the cutaway is missing the end barline
Here's a picture of what it looks like with my PR applied:
You can see the extra staff lines drawn at the end of the top staff of the first system; without my PR (eg, in 3.6.0), there also would have been a clef showing there, that really belongs to the hidden third staff. My PR eliminates the clef but not the extra staff lines. That part of the problem would be solved by making the same fix to Measure::scanElements() that my PR makes to Segment::scanElements(). But that change does not address the missing barline. So there is some other problem in how all of this is managed.
Luckily, the combination of things needed to make this happen mean it won't be a very commonly encountered problem. And, non-obvious though it might be, the workaround is to disable the courtesy clef for the clef at the start of the second system / third staff. The clef wasn't going to show anyhow since the staff is hidden on the first system, this fixes both glitches:
Attached is the score.