Whenever a funeral procession passed by the Messenger of Allah ﷺ, he would ask the people whether the deceased had any debts.If they said yes, he would ask, "Did he leave behind enough to settle them?"If they said yes, he would pray over him. Otherwise, he would say, "Pray over your companion yourselves."
حدثنا سليمان بن داود قال أنبأنا أبو زبيد عن الأعمش عن أبي صالح
كان رسول الله ﷺ إذا مرت به جنازة سألهم عليه دين فإن قالوا نعم قال ترك وفاء فإن قالوا نعم صلى عليه وإلا قال صلوا على صاحبكم
Its isnad is sahih. Bukhari related it in 3/24, 26, 128, 7/86, Muslim in 3/1237 no. 1619, Abu Dawud in 2710 in the chapter on Sales, Strictness Regarding Debt, Tirmidhi in 3/373 no. 1070, and Nasai in 4/65 no. 1963 in the chapter on Funerals, Praying Over One Who Has a Debt.