(dir-locals-collect-variables): Don't autoload if not needed (bug#74349)
While dir-local settings for `c-mode` may require (auto)loading `cc-mode.el`
to get all the `safe-local-variable` properties, they may not. So before
(auto)loading that file, make sure we don't already have all the
`safe-local-variable` properties we need.
* lisp/files.el (dir-locals--load-mode-if-needed): New function.
(hack-one-local-variable): Don't inf-loop if `eval` calls a major mode.
(dir-locals-collect-variables): Use `dir-locals--load-mode-if-needed`.
(cherry picked from commit
f713258416f224b93e4f25b2db24d5e8797bcbff)