Solution 1:

Try this:

function! EnterEnter()
  if getline(".") =~ '^\s*\(//\|#\|"\)\s*$'
    return "\<C-u>"
  else
    return "\<CR>"
  endif
endfunction

imap <expr> <CR> EnterEnter()

Solution 2:

I extended @romainl's answer to work with arbitrary languages by generating the regex from Vim's &commentstring:

function! s:IsOnlyComment(getlineArg)
  let commentRegex='^\s*'.substitute(&commentstring,'%s','\\s*','').'$'
  return strlen(matchstr(getline(a:getlineArg), commentRegex)) > 0
endfunction

function! SmartEnter()
  if s:IsOnlyComment('.')
    return "\<Esc>S"
  else
    return "\<CR>"
  endif
endfunction

inoremap <expr> <CR> SmartEnter()

However, I can't seem to remap <CR> at all, this just won't work. For now, I use <CR><CR> until this issue is solved.

Solution 3:

Remove r from 'formatoptions. That's what that option does. Turning it off will mean you never get vim doing that for you which means you will need to add the leading comment markers when you do actually want them but that's the tradeoff.