Repository: rust-lang/rust-mode (language: rust)

Issue:
"rust-format-on-save" sometimes leads to a "stdin" buffer
Seems like I couldn't save the following code if I have `(rust-format-on-save t)`. It always leads to a "stdin" buffer.
I think it's because of the empty lines (with spaces) between the `arg` blocks. If I remove them, it works fine.

```
use clap::{App, Arg, ArgMatches, SubCommand};

pub fn func() -> ArgMatches<'static> {
    App::new("test")
        .version("0.1.0")
        .subcommand(
            SubCommand::with_name("add")
                .about("Add a new entry")
                .arg(
                    Arg::with_name("ENTRY")
                )

                
                .arg(
                    Arg::with_name("DATE_MATH")
                        .takes_value(true)
                        .required(false)
                        .multiple(false)
                        .help("The date, can be in the form of 2019-12-20 or -1d/w (1 day/week before)"),
                ),
        )
        .get_matches()
}
```
The`*rustfmt*` buffer says:

```
error[internal]: left behind trailing whitespace
  --> stdin:13:13:0
   |
13 |                 
   | ^^^^^^^^^^^^^^^^
   |

warning: rustfmt has failed to format. See previous 1 errors.
```


Below is my environment:

```
GNU Emacs 27.0.50 (build 1, x86_64-apple-darwin19.0.0, NS appkit-1894.10 Version 10.15.1 (Build 19B88)) of 2019-11-26

$ rustfmt  --version
rustfmt 1.4.11-stable (18382352 2019-12-03)

$ elpa/rust-mode-20200322.1749/
```


Discussion / hints:
I think this is a bug of `rustfmt`: https://github.com/rust-lang/rustfmt/issues/4037

Closing this issue.
Actually, Could `rust-mode` handle this more gracefully, than opening up a stdin? Because I think this is quite confusing for the user.
Looks like #357 still alive. 

For me problem occurs when I try to code bellow.
  rust-mode          20200322.1749
  GNU Emacs 26.3 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.10) of 2019-08-29

```rust
/// input data
/// output parity data
pub fn encode(data: [u8; 8]) -> u8 {
    let data = u64::from_le_bytes(data);

    let mut ret = 0u8;
    for (i, mask) in generated::PARITY_BITS_MASK.iter().enumerate() {
        let parity_bit = ((data & mask).count_ones() & 1) as u8;
        ret |= parity_bit << i;
    }
    ret = ((data.count_ones() & 1) as u8) << 7;
    ret
}

/// correct input data if there is one bit error,
/// return Err(()) if double bit error was detected
pub fn decode(data: &mut [u8; 8], parity: u8) -> Result<(), ()> {
    let data = u64::from_le_bytes(data);

    let overall_parity = ((data.count_ones() & 1) as u8) << 7;
    let overall_expected = (parity >> 7) & 1;
    let overall_correct = 
    let mut index_of_error = 0;
    for (i, mask) in generated::PARITY_BITS_MASK.iter().enumerate() {
        let parity = ((data & mask).count_ones() & 1) as u8;
        let expected = (parity >> i) & 1;
        if parity != expected {
            index_of_error |= 1u8 << i;
        }
    }
    

    unimplemented!()
}

mod generated {
    pub const PARITY_BITS_MASK: [u64; 7] = [
        0xab55555556aaad5b,
        0xcd9999999b33366d,
        0xf1e1e1e1e3c3c78e,
        0x1fe01fe03fc07f0,
        0x1fffe0003fff800,
        0x1fffffffc000000,
        0xfe00000000000000,
    ];
}
```
This problem happens again.

If run rustfmt in terminal I got:

```
$ rustfmt parser.rs
error[internal]: left behind trailing whitespace
   --> parser.rs:122:122:0
    |
122 |             
    | ^^^^^^^^^^^^
    |

warning: rustfmt has failed to format. See previous 1 errors.
```

May be just revert 
a36c96ab556ddf9cd098ab0109acfdd94b106675
?
cc @nikodemus @mookid 

I confirm I can reproduce the bug. I'll try to find some time to fix it this weekend.

Fix patch:
diff --git a/rust-mode.el b/rust-mode.el
index f91d5ff5..9d17cc08 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -198,7 +198,7 @@ to the function arguments.  When nil, `->' will be indented one level."
   :safe #'booleanp
   :group 'rust-mode)
 
-(defcustom rust-format-show-buffer nil
+(defcustom rust-format-show-buffer t
   "Show *rustfmt* buffer if formatting detected problems."
   :type 'boolean
   :safe #'booleanp
@@ -1484,6 +1484,8 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
   (with-current-buffer (get-buffer rust-rustfmt-buffername)
     (goto-char (point-min))
     (while (re-search-forward "--> <stdin>:" nil t)
+      (replace-match (format "--> %s:" buffer-name)))
+    (while (re-search-forward "--> stdin:" nil t)
       (replace-match (format "--> %s:" buffer-name)))))
 
 ;; If rust-mode has been configured to navigate to source of the error
@@ -1532,7 +1534,7 @@ rustfmt complain in the echo area."
                                     (if (re-search-forward "\nerror:.+\n" nil t)
                                         (buffer-substring p0 (point))
                                       (buffer-substring p0 (point-max)))))))))
-        (when (and target-buffer target-point)
+        (when (and target-buffer (get-buffer target-buffer) target-point)
           (switch-to-buffer target-buffer)
           (goto-char (point-min))
           (forward-line (1- (car target-point)))


Test patch: