Fix a small regression introduced in 6da9fade and add tests.
Migrated from D3081 (view on Phabricator)