change-major-mode-with-file-name is a variable defined in `files.el'.
Its value is

Non-nil means C-x C-w should set the major mode from the file name.
However, the mode will not be changed if
(1) a local variables list or the `-*-' line specifies a major mode, or
(2) the current major mode is a "special" mode,
not suitable for ordinary files, or
(3) the new file name does not particularly specify any mode.

You can customize this variable.