Skip to content

Commit

Permalink
Merge branch 'master' into add-edition-2027
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhuichen authored Sep 25, 2024
2 parents 43fffbd + 6157568 commit 4dbeea4
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 37 deletions.
60 changes: 59 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,64 @@
# Changelog

## [Unreleased]
## [1.8.0] 2024-09-20

### Fixed
- Fix issue where rustfmt would crash on Windows when using the `ignore` option [#6178](https://github.com/rust-lang/rustfmt/issues/6178)

### Changed
- `rustfmt --version` now prints a commit hash that is 10 characters long [#6258](https://github.com/rust-lang/rustfmt/pull/6258)
- `rustfmt --version` will no longer print empty git information when git information isn't available at build time.
For example, git information is not available when building rustfmt from a source tarball [#6266](https://github.com/rust-lang/rustfmt/pull/6266)
- `version` has been soft deprecated and replaced by `style_edition`.
`style_edition=2024` is equivalent to `version=Two` and `style_edition={2015|2018|2021}`
are equivalent to `version=One` [#6247](https://github.com/rust-lang/rustfmt/pull/6247)
- When `style_edition=2024` is configured `overflow_delimited_expr` will default to `true` [#6260](https://github.com/rust-lang/rustfmt/pull/6260).
```rust
// with style_edition=2015
do_thing(
x,
Bar {
x: value,
y: value2,
},
);

// with style_edition=2024
do_thing(x, Bar {
x: value,
y: value2,
});
```
- When `style_edition=2024` is configured rustfmt will apply the [style guide's version sorting algorithm]
when sorting imports [#6284](https://github.com/rust-lang/rustfmt/pull/6284)
```rust
// with style_edition=2015
use std::num::{NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8};

// with style_edition=2024
use std::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64};
```
[style guide's version sorting algorithm]: https://doc.rust-lang.org/nightly/style-guide/#sorting
- When parsing rustfmt configurations fails, rustfmt will now include the path to the toml file in the erorr message [#6302](https://github.com/rust-lang/rustfmt/issues/6302)

### Added
- rustfmt now formats trailing where clauses in type aliases [#5887](https://github.com/rust-lang/rustfmt/pull/5887)
```rust
type Foo
= Bar
where
A: B,
C: D;
```
- Users can now configure which `style_edition` rustfmt uses when formatting their code as specified
in [RFC 3338](https://rust-lang.github.io/rfcs/3338-style-evolution.html). Users are encouraged to configure `style_edition`
in their `rustfmt.toml` files, but the value can also be specified via the cli with `--unstable-features --style-edition={style_edition}`.
When `style_edition` is not explicitly configured it will be inferred from the `edition` configuration.
When neither `style_edition` nor `edition` are configured `style_edition` defaults to `2015` [#6247](https://github.com/rust-lang/rustfmt/pull/6247)

### Misc
- Removed `tracing-attributes` dependency [#6208](https://github.com/rust-lang/rustfmt/pull/6208)
- Reduced syn's features in the internal `config_proc_macro` crate [#6237](https://github.com/rust-lang/rustfmt/pull/6237)

## [1.7.1] 2024-06-24

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "rustfmt-nightly"
version = "1.7.1"
version = "1.8.0"
description = "Tool to find and fix Rust formatting issues"
repository = "https://github.com/rust-lang/rustfmt"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ fn format(

for file in files {
if !file.exists() {
eprintln!("Error: file `{}` does not exist", file.to_str().unwrap());
eprintln!("Error: file `{}` does not exist", file.display());
session.add_operational_error();
} else if file.is_dir() {
eprintln!("Error: `{}` is a directory", file.to_str().unwrap());
eprintln!("Error: `{}` is a directory", file.display());
session.add_operational_error();
} else {
// Check the file directory if the config-path could not be read or not provided
Expand Down
8 changes: 3 additions & 5 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{borrow::Cow, iter};

use itertools::{MultiPeek, multipeek};
use itertools::{Itertools as _, MultiPeek, multipeek};
use rustc_span::Span;
use tracing::{debug, trace};

Expand Down Expand Up @@ -1056,8 +1056,7 @@ fn light_rewrite_comment(
config: &Config,
is_doc_comment: bool,
) -> String {
let lines: Vec<&str> = orig
.lines()
orig.lines()
.map(|l| {
// This is basically just l.trim(), but in the case that a line starts
// with `*` we want to leave one space before it, so it aligns with the
Expand All @@ -1075,8 +1074,7 @@ fn light_rewrite_comment(
// Preserve markdown's double-space line break syntax in doc comment.
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment)
})
.collect();
lines.join(&format!("\n{}", offset.to_string(config)))
.join(&format!("\n{}", offset.to_string(config)))
}

/// Trims comment characters and possibly a single space from the left of a string.
Expand Down
2 changes: 1 addition & 1 deletion src/config/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl From<rustc_span::FileName> for FileName {
impl fmt::Display for FileName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FileName::Real(p) => write!(f, "{}", p.to_str().unwrap()),
FileName::Real(p) => write!(f, "{}", p.display()),
FileName::Stdin => write!(f, "<stdin>"),
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,10 +1049,10 @@ make_backup = false
max_width = 100
"#;
let config = Config::from_toml(toml, Path::new("./rustfmt.toml")).unwrap();
assert_eq!(config.array_width(), usize::max_value());
assert_eq!(config.attr_fn_like_width(), usize::max_value());
assert_eq!(config.chain_width(), usize::max_value());
assert_eq!(config.fn_call_width(), usize::max_value());
assert_eq!(config.array_width(), usize::MAX);
assert_eq!(config.attr_fn_like_width(), usize::MAX);
assert_eq!(config.chain_width(), usize::MAX);
assert_eq!(config.fn_call_width(), usize::MAX);
assert_eq!(config.single_line_if_else_max_width(), 0);
assert_eq!(config.struct_lit_width(), 0);
assert_eq!(config.struct_variant_width(), 0);
Expand Down
8 changes: 4 additions & 4 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ impl WidthHeuristics {
// Using this WidthHeuristics means we ignore heuristics.
pub fn null() -> WidthHeuristics {
WidthHeuristics {
fn_call_width: usize::max_value(),
attr_fn_like_width: usize::max_value(),
fn_call_width: usize::MAX,
attr_fn_like_width: usize::MAX,
struct_lit_width: 0,
struct_variant_width: 0,
array_width: usize::max_value(),
chain_width: usize::max_value(),
array_width: usize::MAX,
chain_width: usize::MAX,
single_line_if_else_max_width: 0,
single_line_let_else_max_width: 0,
}
Expand Down
11 changes: 6 additions & 5 deletions src/emitter/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::rustfmt_diff::{DiffLine, Mismatch, make_diff};
use serde::Serialize;
use serde_json::to_string as to_json_string;
use serde_json::to_writer as to_json_writer;

#[derive(Debug, Default)]
pub(crate) struct JsonEmitter {
Expand All @@ -26,7 +26,8 @@ struct MismatchedFile {

impl Emitter for JsonEmitter {
fn emit_footer(&self, output: &mut dyn Write) -> Result<(), io::Error> {
writeln!(output, "{}", &to_json_string(&self.mismatched_files)?)
to_json_writer(&mut *output, &self.mismatched_files)?;
writeln!(output)
}

fn emit_formatted_file(
Expand Down Expand Up @@ -56,7 +57,7 @@ impl JsonEmitter {
filename: &FileName,
diff: Vec<Mismatch>,
) -> Result<(), io::Error> {
let mut mismatches = vec![];
let mut mismatches = Vec::with_capacity(diff.len());
for mismatch in diff {
let original_begin_line = mismatch.line_number_orig;
let expected_begin_line = mismatch.line_number;
Expand Down Expand Up @@ -252,7 +253,7 @@ mod tests {
)
.unwrap();
let _ = emitter.emit_footer(&mut writer);
let exp_json = to_json_string(&vec![MismatchedFile {
let exp_json = serde_json::to_string(&vec![MismatchedFile {
name: String::from(file_name),
mismatches: vec![
MismatchedBlock {
Expand Down Expand Up @@ -338,7 +339,7 @@ mod tests {
}],
};

let exp_json = to_json_string(&vec![exp_bin, exp_lib]).unwrap();
let exp_json = serde_json::to_string(&vec![exp_bin, exp_lib]).unwrap();
assert_eq!(&writer[..], format!("{exp_json}\n").as_bytes());
}
}
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ fn rewrite_int_lit(
format!(
"0x{}{}",
hex_lit,
token_lit.suffix.map_or(String::new(), |s| s.to_string())
token_lit.suffix.as_ref().map_or("", |s| s.as_str())
),
context.config.max_width(),
shape,
Expand Down
6 changes: 3 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,9 +1163,9 @@ pub(crate) fn convert_try_mac(

pub(crate) fn macro_style(mac: &ast::MacCall, context: &RewriteContext<'_>) -> Delimiter {
let snippet = context.snippet(mac.span());
let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::MAX);
let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::MAX);
let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::MAX);

if paren_pos < bracket_pos && paren_pos < brace_pos {
Delimiter::Parenthesis
Expand Down
2 changes: 1 addition & 1 deletion src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub(crate) fn rewrite_string<'a>(
stripped_str
.len()
.checked_next_power_of_two()
.unwrap_or(usize::max_value()),
.unwrap_or(usize::MAX),
);
result.push_str(fmt.opener);

Expand Down
12 changes: 4 additions & 8 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ fn is_file_skip(path: &Path) -> bool {
fn get_test_files(path: &Path, recursive: bool) -> Vec<PathBuf> {
let mut files = vec![];
if path.is_dir() {
for entry in fs::read_dir(path).expect(&format!(
"couldn't read directory {}",
path.to_str().unwrap()
)) {
for entry in
fs::read_dir(path).expect(&format!("couldn't read directory {}", path.display()))
{
let entry = entry.expect("couldn't get `DirEntry`");
let path = entry.path();
if path.is_dir() && recursive {
Expand All @@ -122,10 +121,7 @@ fn get_test_files(path: &Path, recursive: bool) -> Vec<PathBuf> {
}

fn verify_config_used(path: &Path, config_name: &str) {
for entry in fs::read_dir(path).expect(&format!(
"couldn't read {} directory",
path.to_str().unwrap()
)) {
for entry in fs::read_dir(path).expect(&format!("couldn't read {} directory", path.display())) {
let entry = entry.expect("couldn't get directory entry");
let path = entry.path();
if path.extension().map_or(false, |f| f == "rs") {
Expand Down

0 comments on commit 4dbeea4

Please sign in to comment.