I heard a hadith which had a similar meaning to this. Our Holy Prophet (pbuh) criticized a companion for killing a person. The companion had killed the person as he thought that he wasn't a Muslim, although that wasn't the case. Hazrat Mohammad (pbuh) said to the companion that had you seen through his heart whether he was a Muslim or not and that the companion shouldn't have doubted the dead's faith as he had already recited kalma before.
If anyone has any reference to this hadith, kindly mention it in the comments.