There is a check for other versioned formula with the same name in the file audit. This just memoizes all of the versioned formulae in a tap during the first call and then uses that much shorter list everytime it checks for things.
Library
This directory contains all the code run by the official brew command in Homebrew and all formulae (package descriptions) in taps (repositories containing formulae) in Taps subdirectories.