Praise be to Allah.
By the mercy of Allah, may He be exalted, he does not bring a person to account for whispers in the heart (waswas) and passing thoughts, as the Prophet (blessings and peace of Allah be upon him) said: “Allah, may He be glorified and exalted, will forgive my ummah (followers) for whatever crosses their minds so long as they do not act upon it or speak of it.” Narrated by al-Bukhari (5269) and Muslim (127).
If the one who is suffering from depression and the like thinks to himself of kufr (disbelief) or sin, he will not be brought to account for that on condition that he does not utter it or act upon it.
If we assume that this sick person uttered what was on his mind under the pressure of his illness or of the whispers in his heart in the sense that he was overcome and could not help it, and these words came out of him without him intending that, then he will not be brought to account for that, because he did not intend to say it out loud.
Al-Bukhari wrote a chapter in his Saheeh entitled: Divorce given in a state of anger, under compulsion or under the effect of intoxicants or insanity; the verdict about them; mistakes and forgetfulness in issuing a divorce or (uttering the words of) shirk (disbelief) and so on, because of the words of the Prophet (blessings and peace of Allah be upon him): “Actions are but by intentions, and each man will have but that which he intended”; Ash-Sha‘bi recited (the verse meaning) Punish us not if we forget or fall into error [al-Baqarah 2:286]; what is not valid of the confession of one who is affected by whispers in his heart; the Prophet (blessings and peace of Allah be upon him) said to the one who confessed against himself, “Are you insane?”; ‘Uqbah ibn ‘Amir said: The divorce of one who is affected by whispers in the heart is not valid. End quote.
Al-Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said: If one who is affected by whispers in the heart utters the word of divorce, it is not valid if he did not intend to do that, because these words may be uttered by one who is affected by whispers in the heart without him intending to do that or wanting to do it; rather he is compelled to do that and cannot help it because of the strength of the force and the weakness of his resistance. The Prophet (blessings and peace of Allah be upon him) said: “There is no divorce in the case of compulsion.” So the divorce does not count as such if he did not really have the intention to do that with peace of mind concerning it. This is something that he may be compelled to do without intending it or choosing to do it, so it does not count as a divorce. End quote from Fatawa Islamiyyah, 3/277
This explanation given by the Shaykh (may Allah have mercy on him) is also applicable in the case of one who is suffering from depression. If he says something when he is compelled, without intending or wanting to, then he will not be brought to account for it.
But the one who is affected in that way should try to treat the problem and rid himself of distress and anxiety; he should think about the blessings that Allah, may He be exalted, has bestowed upon him so that gratitude will take hold in his heart and cause him to be steadfastly content with what Allah decrees; this will save him from being angry with his Lord for there is no slave but Allah has granted him innumerable blessings with regard to his spiritual and physical health, his family and other matters.
But if he did know what he was saying and intended it, but he thought that that would bring him relief, or he was heedless in some of what he said, (then the matter is different) just as there are texts that speak of a stern warning concerning women who wail (in mourning), even though that is something that usually happens at times of extreme grief. And the Prophet (blessings and peace of Allah be upon him) warned against uttering words that incur the wrath of Allah at times of calamity. He (blessings and peace of Allah be upon him) said, when his son Ibraheem died: “The eyes weep and the heart grieves, but we do not say anything but that which pleases our Lord.”Narrated by al-Bukhari (1303) and Muslim (2315).
According to a report narrated by Ibn Majah (1589), he said: “…but we do not say anything that angers the Lord.”
It should be noted that getting angry will not benefit him or remove his distress; rather what will benefit him is patience and seeking reward with Allah, as the Prophet (blessings and peace of Allah be upon him) said: “The greatest reward comes with the greatest trial. When Allah loves a people He tests them. Whoever accepts that wins His pleasure but whoever is discontent with that earns His wrath.”
Narrated by at-Tirmidhi (2396) and Ibn Majah (4031). Classed as saheeh (authentic) by al-Albani in Saheeh at-Tirmidhi.
See also the answer to question no. 118325
And Allah knows best.