I'm framing a small wall to create some closet space. If I mount the light switch box directly to the king stud of the doorway, the door trim will interfere with the switch plate, so I wanted to bump the box out by putting a short scrap of 2x4 between the box and the king stud. Is there anything in the NEC that prohibits that?
That's common practice. As long as the framing member is rigidly attached it doesn't really matter what other function it serves or what it's called. (Jester stud?)