The judge or court before whom a person is brought by writ of habeas corpus shall examine the writ and the papers attached to it; and if no legal cause be shown for the imprisonment or restraint, or if it appear that the imprisonment or restraint, though at first legal, cannot for any cause be lawfully prolonged, the applicant shall be discharged.
Tex. Code Crim. Proc. § 11.40