`newline_style=Auto` doesn't report any issue on mixed line endings

Open
#7,238 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
tooling

Research direction

Start by reading the issue’s referenced newline handling in src/source_file.rs and src/formatting.rs, then trace how rustfmt --check detects newline style. Reproduce with the mixed-ending sample and compare behavior when newline_style is unset, Unix, and Windows; done means mixed endings are reported in auto mode and empty-file formatting is idempotent.

Written by the indexing model from the issue text.

Description

A-whitespace C-bug SO-newline_style

Summary

I don't trust GitHub to respect line-endings if I paste code, so:

$ printf 'fn main() {\r\n    blah() // no CR!\n}\r\n' > file.rs
$ rustfmt --check ./files.rs

I can't imagine this is very impactful, I would expect almost all text editors to force the same line endings within a file (so someone would likely need to go out of their way to hit this)

Expected behavior

rustfmt reports the inconsistency like: Incorrect newline style in file.rs

Actual behavior

rustfmt reports nothing

Configuration

Ensure newline_style is unset or Auto

Reproduction Steps

See above. Note that with newline_style set this reports correctly:

$ rustfmt --check --config newline_style=Unix ./samples/lines.rs 
Incorrect newline style in lines.rs
$ rustfmt --check --config newline_style=Windows ./samples/lines.rs 
Incorrect newline style in lines.rs

Meta

$ rustfmt --version
rustfmt 1.11.0-nightly (3e92e1a2d2 2026-11-01)

Notes

I found this while working on https://github.com/rust-lang/rustfmt/pull/7233. The issue is when we don't set newline_style we get the original file content from the source map here which only contains Unix line endings (so strips the CR). When we determine the newline style to apply we check the output from the snippet provider, which I don't think will ever contain carriage returns (but I haven't dug deep enough to confirm).

This also results in non-idempotent formatting of empty files in Windows:

  • Format "": apply_newline_style finds no newlines, defaults to Native, so we write out \r\n
  • Second turn: format \r\n: per above apply_newline_style reads from snippet, which strips out \r so it only sees \n and determines Unix line endings, so we write out \n
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 11h
Merged PRs (30d)
29

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-lang/rustfmt

All issues in rust-lang/rustfmt

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.