rust-lang / rust-mode

Emacs configuration for Rust
Apache License 2.0
1.12k stars 179 forks source link

"rust-format-on-save" sometimes leads to a "stdin" buffer #365

Closed xelephas closed 4 years ago

xelephas commented 4 years ago

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/
xelephas commented 4 years ago

I think this is a bug of rustfmt: https://github.com/rust-lang/rustfmt/issues/4037

Closing this issue.

xelephas commented 4 years ago

Actually, Could rust-mode handle this more gracefully, than opening up a stdin? Because I think this is quite confusing for the user.

Dushistov commented 4 years ago

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

/// 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,
    ];
}
Dushistov commented 4 years ago

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

mookid commented 4 years ago

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

mookid commented 4 years ago

Thanks for the report, please test.