Abu Razīn (may Allah be pleased with him) narrated that Allāh’s Messenger (salallāhu ‘alaihi wasallam) said,
ضَحِكَ رَبُّنَا مِنْ قُنُوطِ عِبَادِهِ وَقُرْبِ غِيَرِهِ
“Allāh laughs at the despair of His servants, and He soon changes it [for them].”
قُلْتُ يَا رَسُولَ اللَّهِ أَوَ يَضْحَكُ الرَّبُّ
I said, “O Messenger of Allah! Does the Lord laugh?”
He said, “Yes.”
قُلْتُ لَنْ نَعْدِمَ مِنْ رَبٍّ يَضْحَكُ خَيْرًا
I said, “We shall never be deprived of good by a Lord who laughs!”
(Reported by Ibn Mājah, no 181, graded hasan by Al-Albāni)
So, have good thoughts concerning your Lord (the Most Generous and Kind) just like this Companion because we will never be deprived of good from a Lord that laughs. The changing of the afflictions is in the Hands of Allah―indeed every single affair is with Allah. And ask Allah for Al-‘Āfiyah (wellbeing and safety) in this life and next.
The saying of the Messenger (salallāhu ‘alaihi wasallam), “and He soon changes it [for them]” means that he changes their state of affliction and suffering. So, Allah laughs at his servant who despairs when the slightest affliction reaches him alongside the fact that Allah will remove it from him shortly. He (the Most Kind) soon changes evil that reaches a person to good, sickness to wellbeing and health, trials and tests to happiness and joy. The believers realise (due to their strong īmān) that they will never be deprived of good from a Lord who laughs. And Ahlus-Sunnah wal-Jamā’ah affirm the attribute of laughter for Allah (the Most High).
Abu Hurairah (radiyallāhu ‘anhu) narrated that the Prophet (salallāhu ‘alaihi wasallam) said:
يقولُ اللَّهُ عزَّ وجلَّ أنا عندَ ظنِّ عبدي بي إن ظنَّ خيرًا فلهُ وإن ظنَّ شرًّا فله
Allah, the Mighty and Majestic, said: “I am to my slave as he thinks of Me. If he has good thoughts of Me, then that is good for him, and if He has bad thoughts then that is bad for him.” (Imām Ahmad reports in the Musnad, no. 9076, saheeh)
So, having good thoughts about Allāh, and being optimistic prevents a person from depression, despair, dark thoughts and feelings that he finds difficult to explain and cope with.
In being optimistic and having good thoughts about Allah, being hopeful of good and thinking “there is good around the corner, inshā’ Allāh” is in accordance to the Sunnah and through thinking like that, you are following the Sunnah of the Prophet (salallāhu ‘alaihi wasallam) because this is what he used to encourage:
لَا تَحْزَنْ إِنَّ اللَّهَ مَعَنَا ۖ فَأَنزَلَ اللَّهُ سَكِينَتَهُ عَلَيْهِ
“Do not be sad, indeed Allah is with us.” And Allah sent down His tranquillity upon him.” (At-Tawbah: 40)