Emacs cannot warn you that a file does not contain text unless it either does open it (so that it can see what the contents are), or otherwise asks some other program to find out (perhaps using something like the Unix file
command).
The problem with the latter approach is that Emacs can handle many kinds of binary file, so you would then need that external program to also know which kinds of binary file Emacs recognises, including support added by add-on libraries, which can vary dynamically and per-user.
I don't think there are any good options there.
What is it specifically that you are trying to avoid?
Edit:
As the required test for the given example is based only upon filenames, the following would be a viable approach.
(defvar my-find-file-check-source-extensions
'(".cpp" ".cc"))
(defadvice find-file-read-args (after my-find-file-read-args-check-source)
(let* ((filename (car ad-return-value))
(source-filename
(catch 'source-file-exists
(mapc (lambda (ext)
(let ((source-filename (concat filename ext)))
(when (file-exists-p source-filename)
(throw 'source-file-exists source-filename))))
my-find-file-check-source-extensions)
nil)))
(and source-filename
(not (y-or-n-p (format "Source file %s detected. Are you sure you \
want to open %s? " source-filename filename)))
(error "find-file aborted by user"))))
(ad-activate 'find-file-read-args)
I edited by question by adding an example. – None – 2012-01-02T01:43:02.533