None of you should die Except while he is having good thoughts about Allah – Imam Nawawī’s & al-Manāwī
Jābir (رضي الله عنه) said: Three days before he died, I heard the prophet (صلّى الله عليه وسلّم) say:
« لاَ يَمُوتَنَّ أَحَدُكُمْ إِلاَّ وَهُوَ يُحْسِنُ الظَّنَّ بِاللَّهِ عَزَّ وَجَلَّ »
 None of you should die except while he is having good thoughts about Allah (عزّ وجلّ)
The following is again from al-Nawawī’s explanation of Ṣaḥīḥ Muslim.
Regarding the statement of the prophet » None of you should die except while he is having good thoughts about Allah « scholars have said that this is a warning against losing hope and despairing (of Allah’s mercy) and an encouragement to have hope especially at the time of death.
Allah (سبحانه وتعالى) also said in the previous ḥadīth » “I am as my servant thinks of Me.” « and (considering both ḥadīth) scholars explain that » having good thoughts about Allah « means that an individual should expect Allah to have mercy on him and pardon him.
Some scholars have also said that during times of wellbeing and health, an individual should have both hope and fear in Allah and these two feelings should be equally balanced. Others say that one’s fear should be more, but when the signs of death approach, a person’s hope in Allah’s mercy should become stronger. This is because the very purpose of having fear of Allah is to prevent oneself from committing sins and ugly deeds and to increase in obedience and good deeds. And those things are most often not possible during that situation (i.e., during the last moments of one’s death).
Instead, in that situation, assuming good thoughts about Allah is preferable which would cause one to have feelings of destitution and submission and to feel oneself in total need of Allah (تعالى).
The ḥadīth mentioned by Imām Muslim right after this one also supports this understanding:
« يُبْعَثُ كُلُّ عَبْدٍ عَلَى مَا مَاتَ عَلَيْهِ »
Every servant will be resurrected according to what he died upon.
Scholars explain that it means each individual will be resurrected in the same state in which he died. The next ḥadīth (in Imām Muslim’s authentic collection) also supports this:
« إِذَا أَرَادَ اللَّهُ بِقَوْمٍ عَذَابًا أَصَابَ الْعَذَابُ مَنْ كَانَ فِيهِمْ ثُمَّ بُعِثُوا عَلَىأَعْمَالِهِمْ.»
When Allah intends to punish an entire group of people, the punishment afflicts them all, but then each is resurrected according to (his individual) deeds.
And the ḥadīth:
« إِنَّمَا يُبْعَثُ النَّاسُ عَلَى نِيَّاتِهِمْ »
People will only be resurrected upon their (individual) intentions. 
The following explanation is from al-Manāwī’s, Fayḍ al-Qadīr.
His (صلّى الله عليه وسلّم) statement » None of you should die except while he is having good thoughts about Allah « means: Let none of you die under any circumstance except while in the state of thinking good about Allah (تعالى) in that He will be merciful with you and pardon you. This is because when a person’s time of passing arrives and his journey comes to an end, his fear (of Allah) has no real purpose (to prevent him from bad deeds during life). It would perhaps lead him to despair and to a feeling of restriction of (Allah’s) mercy and bounties.
Whoever at that time, even if his heart were laden with major sins, begins expecting the best (of his Lord) and increases in hope, he has then perfected his provisions for his arrival to his Lord…
The prophet (صلّى الله عليه وسلّم) instructs one to ensure he does not die in any state other than expecting and assuming good about Allah. And this does not mean one can change destiny or fate. Rather, what is intended is that an individual is commanded to have good thoughts (about Allah) so that when death overcomes him, he is in such a state.
نسأل الله الغفور الرحيم لتوفيقه على حسن الظنّ به خصوصًا عند الموت
 Collected by Muslim.
 Collected by Ibn Mājah and al-Albānī says it is authentic in Ṣaḥīḥ Sunan Ibn Mājah (no. 3407).
Source: Excerpted from the article “Thinking Good About Allah” – With Explanations by: Ibn Ḥajar al-‘Asqalānī, al-Nawawī, & al-Manāwī – authentic-translations.com