If the law says by possessing marijuana you are a dealer, but a cop finds someone with a small amount, it's likely for recreation and their possession brings no harm to society or others (what the law wants to prevent). Arresting them may be following the letter of the law, but not the intent (to stop distribution).
Another invented situation: cop pulls over someone driving erratically and too fast, then the driver is a woman who escaped being raped by her date. She was driving erratically because she was emotionally and physically distraught. Is giving her a ticket helping anyone? The cop could say "okay, take it easy and slow while I follow you to make sure you're out of danger and feel safe getting home".
Sorry I can't be more specific, I haven't gotten years of training on such situations.
But arresting and then having it cleared costs time and energy but adds nothing to society.
Look, I'm not advocating that they should have more freedom. I am saying there is already freedom because the world is not as clear as the law states, so police should be properly trained to be aware of their role.
My general point I think follows from your last paragraph, their role to protect the people comes before following the letter of the law, but they should always try to uphold the intent.