A theorem on existence of neighbouring trajectories satisfying a state constraint, with applications to optimal control