In the US, building codes are codified in law, and very frequently come with criminal codes attached for violations of them (most of them being misdemeanor violations). I have a few examples scattered across this board that I've written over the past couple of years.
I have seen personally the case where an insurer failed to pay on a claim (for a nearby homeowner). The appeals process was fruitless, it was clear that building codes, and therefore the law, were violated and there was a specific clause in the policy for it.
My insurance policy has a specific clause that states they do not cover loss in case of violation of building codes:
2. We do not insure under any coverage for any loss which
would not have occurred in the absence of one or more
of the following excluded events. We do not insure for
such loss regardless of: (a) the cause of the excluded
event; or (b) other causes of the loss; or (c) whether other
causes acted concurrently or in any sequence with the
excluded event to produce the loss; or (d) whether the
event occurs suddenly or gradually, involves isolated or
widespread damage, arises from natural or external
forces, or occurs as a result of any combination of these:
a. Ordinance or Law, meaning enforcement of any
ordinance or law regulating the construction, repair
or demolition of a building or other structure.
Now, you're right, many states have different laws and take different positions on how much policyholders need to be protected from insurance companies. In all cases, as you rightly note, the insurance company holds the money and has much of the power. If they refuse to pay, it will be up to you to fight them, whether through administrative hearings, court, etc. All the while, you'll be out of at least some of the money, and perhaps out of a home.
And you're also right when you say that many times, those who have done their wiring illegally do get claims paid -- many times, damage makes it too difficult to determine that wiring was done improperly.
So, do you want to take that risk? It's up to you. I follow the codes for any improvement done to my home, even though a) there is no permit requirement and b) there is no inspection requirement; I do it out of safety concern for my family.
Unfortunately, there will always be people who believe that because it works, it must be safe. That's not always the case.
(Finally, you're right -- if you're denied on a loss claim due to this, you will want to talk to a legal specialist. In some cases, you may be able to recover even if you're in the wrong. I don't take any other position than to follow the codes as codified in law.)