“… So if Allāh wants good for His servant, He opens for him the doors of repentance, regret, brokenness, humility, need, and seeking His help, sincerity in turning to Him, continuously turning to Him in plea, du’ā, drawing closer to Him with as many good deeds as possible— enough to make a sin a reason for his mercy, to the point that the enemy of Allāh would say: I wish I had left him and not made him fall into the sin!
This is the meaning of what one of the Salaf has said: A servant may commit a sin by which he enters Paradise and performs a good deed by which he enters Hellfire.
They said: How so?
He replied: The servant may commit the sin, and then it continues to be before his eyes with fear of it, with regret, broken, crying, shy from his Lord تعالى, bowing his head between His Hands, broken-hearted to Him, so that sin becomes the reason for the happiness of that servant and his success, until that sin becomes even more beneficial for him than many good deeds because of what comes along among the causes for the happiness and success of a servant, until that sin would be a reason that he would enter Paradise.
Meanwhile, he may perform a good deed, but considers it a favour to his Lord, becomes arrogant with it, and looks to himself with pride, becomes infatuated with himself because of it, looks down on others because of it, and says: I did this, I did that. This becomes a cause of arrogance and self-admiration, pride and a sense of superiority, which would be the cause of his destruction. So, if Allāh wants good for this poor soul, He tests him with something that would break him, humiliate his neck, and minimize his self-view to himself, and if He wants other than that for this servant, He lets him be proud and arrogant, and that is true forsaking which leads to his downfall.”
• Ibn Al-Qayyim رحمه الله | Al-Wābil As-Sayyib p. 8