Incorrect "Unreachable Code" Warning

While refactoring some code in studio, I came across the following bug in the attached code:

image

function Profile._GetProfile(User)
	local self
	
	if typeof(User) == "Instance" then
		self = PlayerProfileCache[User]
		
		if not self then
			self = setmetatable({}, Profile)
			PlayerProfileCache[User] = self
		end
		
		return
	else
		error("Undefined behavior.")
	end
	
	return self
end

The warning is pointing to the line containing return self. As you can clearly see, the line can indeed be reached if the User argument is not an Instance.

2 Likes

If the User argument is not an Instance, the “error” branch terminates the function before reaching the trailing return.

5 Likes

Aaaand I can’t believe I missed that.

I need some sleep.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.