Ethical factors to consider in case of a non-contributing co-author
The criteria to be a co-author depend on the field of research and on the journal, but usually at least some intellectual contribution to the paper is expected. Most journals have a list of requirements on their website.
Yes, you should talk to your supervisor: this will probably solve the problem.
No, unless the code he wrote gave you important insights (even if the code was not used afterwards). But also see the answer to (1).

There are three possible outcomes:

Your supervisor agrees to remove the co-author.

Your supervisor convinces you that the contribution of the co-author was enough to earn authorship.

Your supervisor wants to include the co-author for &quot;political&quot; reasons. This is wrong, but it happens quite a lot unfortunately. In this case it would probably be best to include the co-author anyway. It is wrong ethically, but if you and your co-authors did your best to prevent it that is on your supervisor and not on you: if you discussed it with him there is not much more you can do unless you want to risk the relationship with your advisor.