On it being disliked to wish for death because of some harm which has befallen a person, but there is no objection to it if it is because of fear of trial in the deen

Riyad as-Salihin (The Meadows of the Righteous)

by Imam Nawawi

67. Chapter: On it being disliked to wish for death because of some harm which has befallen a person, but there is no objection to it if it is because of fear of trial in the deen

585. Abu Hurayra reported that the Messenger of Allah, may Allah bless him and grant him peace, said, "None of you should wish for death. If he does good, he may increase in it, and if he does evil, he may turn in repentance." [Agreed upon. This is the version of al-Bukhari]

In a variant in Muslim, Abu Hurayra reported that the Messenger of Allah, may Allah bless him and grant him peace, said, "None of you should wish for death and he should not pray for it before it comes to him. When he dies, his actions come to an end. A believer's life only increases him in good."

586. Anas reported: "The Messenger of Allah, may Allah bless him and grant him peace, said, 'None of you should wish for death because of some harm which has befallen him. If he has got to do something, he should say, 'O Allah, make live if life is best for me and make me die if death is best for me." [Agreed upon]

587. Qays ibn Abi Hazim said, "We visited Khabbab ibn al-Aratt when he was ill and he had been cupped seven times. He said, 'Our companions who went before us have gone and this world did not cause them any decrease. We have acquired so much that the only way to spend it is in constructing buildings. If it were not that the Prophet, may Allah bless him and grant him peace, had forbidden us to pray for death, I would have prayed for it.' Then we came to him again when he was repairing one of his walls and he said, 'A Muslim is rewarded for everything he spends except for what he invests in buildings.'" [Agreed upon. This is the version of al-Bukhari]