Leave it to Alan Tudyk to improv so well that a director keeps it in the movie. Even more impressive, he improvised while wearing a mo-cap suit and they build the special effects around it to fit. So what part was it?
Reported by ComicBook.com, the scene in question has Kaytoo (The repurposed Imperial droid) pretending to escort Cassian and Jyn off of Jedha. When the stormtrooper questions Kaytoo, he smacks Cassian to sell the scene to the trooper. However, this was completely unscripted.
Alan Tudyk, the voice and motion capture of K-2SO actually smacks Diego Luna across the face and delivers his line about there being "A fresh one if you mouth off again." When Luna covers his face in the scene, you'd think it was because a metal hand slapped him in the face, but it's actually the actor covering his laughter.
Cassian is a bit of a jerk up to this point in the movie, so the smack seemed warranted. For reference, here's the scene in question: