Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustfmt thinks it should be able to modify string values #5744

Open
matthiaskrgr opened this issue Apr 8, 2023 · 6 comments
Open

rustfmt thinks it should be able to modify string values #5744

matthiaskrgr opened this issue Apr 8, 2023 · 6 comments

Comments

@matthiaskrgr
Copy link
Member

fn main() {
    println!(
        "Hello, world!
a    
    a
"
    );
}

note how line 2 inside the format string is actually "a ".
apparently rustfmt things it should be able to change this which is scary because it could potentially alter program behaviour

cargo fmt -- --error-on-unformatted --unstable-features

error[internal]: left behind trailing whitespace
 --> /tmp/frustfmt/src/main.rs:4:4:2
  |
4 | a
  |  ^^^^
  |
  = note: set `error_on_unformatted = false` to suppress the warning against comments or string literals
@ytmimi
Copy link
Contributor

ytmimi commented Apr 9, 2023

@matthiaskrgr thanks for the report.

From what I can tell rustfmt doesn't modify the string lit. Running rustfmt with --check on your input snippet doesn't produce a diff:

rustfmt --config=error_on_unformatted=false --check

rustfmt is simply warning the user that there's trailing whitespace in the input that it was unable to remove, but as far as I know it will never try to modify the string lit.

@simonwuelker
Copy link

simonwuelker commented Jun 8, 2023

for completeness, the same issue also arises inside macro invocations , without the need to pass --error-on-unformatted :

fn main() {
    a!(
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()   
    ) 
}

(Not sure why the identifier has to be this long)

$ rustfmt src/lib.rs
error[internal]: left behind trailing whitespace
 --> <redacted>/src/lib.rs:3:3:109
  |
3 |         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()   
  |                                                                                                             ^^^
  |

warning: rustfmt has failed to format. See previous 1 errors.

@ytmimi
Copy link
Contributor

ytmimi commented Jun 8, 2023

@Wuelle I want to point out that what you're experiencing is unrelated to the current issue. In your case there is trailing whitespace in the input file that rustfmt is unable to remove because of the long identifier name. Bumping the max_width to some sufficiently high value should alleviate your issue.

@JakkuSakura
Copy link

I think I encountered the same issue combined with #5711

error[internal]: left behind trailing whitespace
   |
11 |                                            &strategy.strategy_id as &(dyn ToSql + Sync), 
   |                                              ^
   |


#[cfg(test)]
mod tests {
    #[tokio::test]
    async fn test_foo() -> Result<()> {
        db.query("
                                                INSERT INTO tbl.bar 
                                                (fkey_strategy_id, pkey_id, blockchain, token_name, token_address, quantity, updated_at, created_at) 
                                                VALUES 
                                                ($1, $2, $3, $4, $5, $6, $7, $8);
                                                ", &[
                                                &strategy.strategy_id as &(dyn ToSql + Sync), 
                                ]).await?;

        Ok(())
    }
}

example2.zip

@ytmimi
Copy link
Contributor

ytmimi commented Jun 14, 2023

@qiujiangkun this is not the same issue. The trailing whitespace that rustfmt is warning you about isn't contained in the query stinrg, it's after the of &strategy.strategy_id as &(dyn ToSql + Sync),.

The following line is way to long to fit into the default width of 100, and therefore rustfmt is unable to format the chain db.query(...).await.

(fkey_strategy_id, pkey_id, blockchain, token_name, token_address, quantity, updated_at, created_at)

rustfmt doesn't modify whitespace within string literals so you could fix this problem by dedenting the SQL query or by setting the max_width to some sufficiently large value. In testing 200 worked.

@JakkuSakura
Copy link

JakkuSakura commented Jun 15, 2023

I don't expect internal errors while formatting a valid program.
In my case, it refused to format the rest of the code.

I can accept behavior like this:
give a friendly non-internal warning and proceed to format the rest of the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants