On the day of the Battle of the Trench, the Messenger of Allah ﷺ was carrying earth alongside us, and the dust had covered the whiteness of his belly. He was reciting,"By Allah, had it not been for You, we would not have been guided, nor given in charity, nor prayed. So send down tranquility upon us, for those people have defied us." The narrator said, "And sometimes he would say, 'The chiefs have defied us. When they seek to put us to trial, we refuse.'"He would raise his voice with these words.
كان رسول الله ﷺ يوم الأحزاب ينقل معنا التراب، و لقد وارى التراب بياض بطنه، وهو يقول: والله لولا أنت ما اهتدينا، ولا تصدقنا ولا صلينا، فأنزلن سكينة علينا، إن الألى قد أبوا علينا- قال: وربما قال: إن الملا قد أبوا علينا- إذا أرادوا فتنة أبينا، ويرفع بها صوته.
Bukhari reported it in the chapter on wishing (7236), and Muslim in jihad and military expeditions (1803), via the route of Shubah, from Abu Ishaq, from Bara bin Azib, who narrated it.It may be understood from these hadiths that it is not permissible to use the word "if only" to object to matters of divine decree and the law, but it is permissible to use it when wishing for good.