Valid code is not normally cleaned up.
Calibre does not check/
fix links on a save. They do need to be
Code legal, even if the target does not exist.
Even
running the Check tool, will not modify code unless you then choose one of the fixes that applies to the link.
Running the TOC tool can affect the NCX. All depends on your work flow, I usually use the from Headings option. If I mess up

my headings, I will see

the error of my ways .