Make the deprecation detector in templates more accurate.  Fixes #4514.