Autoimmune myocarditis, or cardiac muscle inflammation, is a rare but frequently fatal side-effect of immune checkpoint inhibitors (ICIs), a class of cancer therapies. Despite the dangers that side-effects such as these pose to patients, they are rarely, if ever, included explicitly when mechanistic mathematical modelling of cancer therapy is used for optimization of treatment. In this paper, we develop a two-compartment mathematical model which incorporates the impact of ICIs on both the heart and the tumour. Such a model can be used to inform the conditions under which autoimmune myocarditis may develop as a consequence of treatment. We use this model in an optimal control framework to design optimized dosing schedules for three types of ICI therapy that balance the positive and negative effects of treatment. We show that including the negative side-effects of ICI treatment explicitly within the mathematical framework significantly impacts the predictions for the optimized dosing schedule, thus stressing the importance of a holistic approach to optimizing cancer therapy regimens.