From 872b630b780ac907ec9ed766992636f164586337 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 7 May 2026 11:16:18 -0700 Subject: [PATCH 1/8] Only format keywords before fn when selected --- src/items.rs | 60 ++++++++++++++++++- src/source_map.rs | 2 +- .../issue-6868-before-fn/fully_selected.rs | 16 +++++ tests/source/issue-6868-before-fn/partial.rs | 16 +++++ .../source/issue-6868-before-fn/unselected.rs | 16 +++++ .../issue-6868-before-fn/fully_selected.rs | 3 + tests/target/issue-6868-before-fn/partial.rs | 12 ++++ .../target/issue-6868-before-fn/unselected.rs | 12 ++++ 8 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 tests/source/issue-6868-before-fn/fully_selected.rs create mode 100644 tests/source/issue-6868-before-fn/partial.rs create mode 100644 tests/source/issue-6868-before-fn/unselected.rs create mode 100644 tests/target/issue-6868-before-fn/fully_selected.rs create mode 100644 tests/target/issue-6868-before-fn/partial.rs create mode 100644 tests/target/issue-6868-before-fn/unselected.rs diff --git a/src/items.rs b/src/items.rs index a2c0e8e0f50..f012aae43d2 100644 --- a/src/items.rs +++ b/src/items.rs @@ -365,6 +365,49 @@ impl<'a> FnSig<'a> { )); result } + + /// Calculates the span for the parts of the signature before the `fn` keyword. + fn span_before_fn(&self) -> Span { + let mut lo = None; + let mut hi = None; + + let mut extend_span = |span: Span| { + lo = Some(lo.map_or(span.lo(), |lo| min(lo, span.lo()))); + hi = Some(hi.map_or(span.hi(), |hi| max(hi, span.hi()))); + }; + + if !matches!(self.visibility.kind, ast::VisibilityKind::Inherited) { + extend_span(self.visibility.span); + } + + match self.defaultness { + ast::Defaultness::Default(span) | ast::Defaultness::Final(span) => extend_span(span), + ast::Defaultness::Implicit => {} + } + + if let ast::Const::Yes(span) = self.constness { + extend_span(span); + } + + if let Some(coroutine_kind) = self.coroutine_kind.as_ref() { + extend_span(coroutine_kind.span()); + } + + match self.safety { + ast::Safety::Unsafe(span) | ast::Safety::Safe(span) => extend_span(span), + ast::Safety::Default => {} + } + + match self.ext { + ast::Extern::Implicit(span) | ast::Extern::Explicit(_, span) => extend_span(span), + ast::Extern::None => {} + } + + let lo = lo.unwrap_or_else(|| self.visibility.span.lo()); + let hi = hi.unwrap_or(lo); + + mk_sp(lo, hi) + } } impl<'a> FmtVisitor<'a> { @@ -2465,7 +2508,22 @@ fn rewrite_fn_base( let where_clause = &fn_sig.generics.where_clause; let mut result = String::with_capacity(1024); - result.push_str(&fn_sig.to_str(context)); + + // Everything before `fn` + let span_before_fn = fn_sig.span_before_fn(); + if context + .config + .file_lines() + .contains(&context.psess.lookup_line_range(span_before_fn)) + { + result.push_str(&fn_sig.to_str(context)); + } else { + let before_ident_span = mk_sp(span_before_fn.hi(), ident.span.lo()); + let fn_lo = context + .snippet_provider + .span_before_last(before_ident_span, "fn"); + result.push_str(context.snippet(mk_sp(span_before_fn.lo(), fn_lo))); + } // fn foo result.push_str("fn "); diff --git a/src/source_map.rs b/src/source_map.rs index 76e0d24cf1e..fc8bd9476a5 100644 --- a/src/source_map.rs +++ b/src/source_map.rs @@ -65,7 +65,7 @@ impl SpanUtils for SnippetProvider { offset += additional_offset + needle.len(); } - original.lo() + BytePos(offset as u32 - 1) + original.lo() + BytePos(offset as u32 - needle.len() as u32) } fn opt_span_after(&self, original: Span, needle: &str) -> Option { diff --git a/tests/source/issue-6868-before-fn/fully_selected.rs b/tests/source/issue-6868-before-fn/fully_selected.rs new file mode 100644 index 00000000000..b2e61f74d88 --- /dev/null +++ b/tests/source/issue-6868-before-fn/fully_selected.rs @@ -0,0 +1,16 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fully_selected.rs","range":[3,10]}] + +pub +( +crate +) +const +unsafe +extern +"C" +fn +foo +( +) +{ +} diff --git a/tests/source/issue-6868-before-fn/partial.rs b/tests/source/issue-6868-before-fn/partial.rs new file mode 100644 index 00000000000..2166f691b60 --- /dev/null +++ b/tests/source/issue-6868-before-fn/partial.rs @@ -0,0 +1,16 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/partial.rs","range":[7,8]}] + +pub +( +crate +) +const +unsafe +extern +"C" +fn +foo +( +) +{ +} diff --git a/tests/source/issue-6868-before-fn/unselected.rs b/tests/source/issue-6868-before-fn/unselected.rs new file mode 100644 index 00000000000..510334a2b79 --- /dev/null +++ b/tests/source/issue-6868-before-fn/unselected.rs @@ -0,0 +1,16 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/unselected.rs","range":[11,14]}] + +pub +( +crate +) +const +unsafe +extern +"C" +fn +foo +( +) +{ +} diff --git a/tests/target/issue-6868-before-fn/fully_selected.rs b/tests/target/issue-6868-before-fn/fully_selected.rs new file mode 100644 index 00000000000..e7c3bb91c6f --- /dev/null +++ b/tests/target/issue-6868-before-fn/fully_selected.rs @@ -0,0 +1,3 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fully_selected.rs","range":[3,10]}] + +pub(crate) const unsafe extern "C" fn foo() {} diff --git a/tests/target/issue-6868-before-fn/partial.rs b/tests/target/issue-6868-before-fn/partial.rs new file mode 100644 index 00000000000..b6eda8a7e03 --- /dev/null +++ b/tests/target/issue-6868-before-fn/partial.rs @@ -0,0 +1,12 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/partial.rs","range":[7,8]}] + +pub +( +crate +) +const +unsafe +extern +"C" +fn foo() { +} diff --git a/tests/target/issue-6868-before-fn/unselected.rs b/tests/target/issue-6868-before-fn/unselected.rs new file mode 100644 index 00000000000..c9d9ef162c7 --- /dev/null +++ b/tests/target/issue-6868-before-fn/unselected.rs @@ -0,0 +1,12 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/unselected.rs","range":[11,14]}] + +pub +( +crate +) +const +unsafe +extern +"C" +fn foo() { +} From bb9fc0ff77bdf594912621814a31401564b9880b Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 7 May 2026 12:04:59 -0700 Subject: [PATCH 2/8] Simplify span_before_fn --- src/items.rs | 75 ++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/src/items.rs b/src/items.rs index f012aae43d2..943b161fe85 100644 --- a/src/items.rs +++ b/src/items.rs @@ -366,47 +366,40 @@ impl<'a> FnSig<'a> { result } - /// Calculates the span for the parts of the signature before the `fn` keyword. - fn span_before_fn(&self) -> Span { - let mut lo = None; - let mut hi = None; - - let mut extend_span = |span: Span| { - lo = Some(lo.map_or(span.lo(), |lo| min(lo, span.lo()))); - hi = Some(hi.map_or(span.hi(), |hi| max(hi, span.hi()))); - }; - - if !matches!(self.visibility.kind, ast::VisibilityKind::Inherited) { - extend_span(self.visibility.span); - } - - match self.defaultness { - ast::Defaultness::Default(span) | ast::Defaultness::Final(span) => extend_span(span), - ast::Defaultness::Implicit => {} - } - - if let ast::Const::Yes(span) = self.constness { - extend_span(span); - } - - if let Some(coroutine_kind) = self.coroutine_kind.as_ref() { - extend_span(coroutine_kind.span()); - } - - match self.safety { - ast::Safety::Unsafe(span) | ast::Safety::Safe(span) => extend_span(span), - ast::Safety::Default => {} - } - - match self.ext { - ast::Extern::Implicit(span) | ast::Extern::Explicit(_, span) => extend_span(span), - ast::Extern::None => {} - } - - let lo = lo.unwrap_or_else(|| self.visibility.span.lo()); - let hi = hi.unwrap_or(lo); + /// Calculates the span for the parts of the signature before the `fn` + /// keyword. + /// + /// The returned span does not include the whitespace between the last + /// non-`fn` keyword and `fn`. If there are no keywords before `fn` then the + /// returned span is empty. + fn span_before_fn( + &self, + context: &RewriteContext<'_>, + span: Span, + ident: symbol::Ident, + ) -> Span { + // Get the span for everything up to the `fn` keyword. + let before_ident_span = mk_sp(span.lo(), ident.span.lo()); + let fn_lo = context + .snippet_provider + .span_before_last(before_ident_span, "fn"); + let before_fn_span = mk_sp(span.lo(), fn_lo); - mk_sp(lo, hi) + // Scan backwards from `fn` to find the last non-whitespace character. + context + .snippet(before_fn_span) + .char_indices() + .rev() + .find(|(_, c)| !c.is_whitespace()) + .map_or_else( + || mk_sp(fn_lo, fn_lo), + |(idx, c)| { + mk_sp( + before_fn_span.lo(), + before_fn_span.lo() + BytePos((idx + c.len_utf8()) as u32), + ) + }, + ) } } @@ -2510,7 +2503,7 @@ fn rewrite_fn_base( let mut result = String::with_capacity(1024); // Everything before `fn` - let span_before_fn = fn_sig.span_before_fn(); + let span_before_fn = fn_sig.span_before_fn(context, span, ident); if context .config .file_lines() From c2101f2e4e8546a8f280bdf4e65ac9f72102c2b2 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 7 May 2026 12:15:54 -0700 Subject: [PATCH 3/8] Actually let's say that you have to also select `fn` That avoids the need to scan backwards for a non-whitespace character --- src/items.rs | 22 +------------------ .../issue-6868-before-fn/fully_selected.rs | 2 +- .../issue-6868-before-fn/fully_selected.rs | 2 +- 3 files changed, 3 insertions(+), 23 deletions(-) diff --git a/src/items.rs b/src/items.rs index 943b161fe85..0f2dbf55fd3 100644 --- a/src/items.rs +++ b/src/items.rs @@ -368,38 +368,18 @@ impl<'a> FnSig<'a> { /// Calculates the span for the parts of the signature before the `fn` /// keyword. - /// - /// The returned span does not include the whitespace between the last - /// non-`fn` keyword and `fn`. If there are no keywords before `fn` then the - /// returned span is empty. fn span_before_fn( &self, context: &RewriteContext<'_>, span: Span, ident: symbol::Ident, ) -> Span { - // Get the span for everything up to the `fn` keyword. let before_ident_span = mk_sp(span.lo(), ident.span.lo()); let fn_lo = context .snippet_provider .span_before_last(before_ident_span, "fn"); - let before_fn_span = mk_sp(span.lo(), fn_lo); - // Scan backwards from `fn` to find the last non-whitespace character. - context - .snippet(before_fn_span) - .char_indices() - .rev() - .find(|(_, c)| !c.is_whitespace()) - .map_or_else( - || mk_sp(fn_lo, fn_lo), - |(idx, c)| { - mk_sp( - before_fn_span.lo(), - before_fn_span.lo() + BytePos((idx + c.len_utf8()) as u32), - ) - }, - ) + mk_sp(span.lo(), fn_lo) } } diff --git a/tests/source/issue-6868-before-fn/fully_selected.rs b/tests/source/issue-6868-before-fn/fully_selected.rs index b2e61f74d88..4a3195dfa4d 100644 --- a/tests/source/issue-6868-before-fn/fully_selected.rs +++ b/tests/source/issue-6868-before-fn/fully_selected.rs @@ -1,4 +1,4 @@ -// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fully_selected.rs","range":[3,10]}] +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fully_selected.rs","range":[3,11]}] pub ( diff --git a/tests/target/issue-6868-before-fn/fully_selected.rs b/tests/target/issue-6868-before-fn/fully_selected.rs index e7c3bb91c6f..7c1885e8cf6 100644 --- a/tests/target/issue-6868-before-fn/fully_selected.rs +++ b/tests/target/issue-6868-before-fn/fully_selected.rs @@ -1,3 +1,3 @@ -// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fully_selected.rs","range":[3,10]}] +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fully_selected.rs","range":[3,11]}] pub(crate) const unsafe extern "C" fn foo() {} From 33b3c886c4da7b6dd3e0751a5bcbfa7ea8810031 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 7 May 2026 12:19:04 -0700 Subject: [PATCH 4/8] Remove the helper function --- src/items.rs | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/items.rs b/src/items.rs index 0f2dbf55fd3..b1e62e4d761 100644 --- a/src/items.rs +++ b/src/items.rs @@ -365,22 +365,6 @@ impl<'a> FnSig<'a> { )); result } - - /// Calculates the span for the parts of the signature before the `fn` - /// keyword. - fn span_before_fn( - &self, - context: &RewriteContext<'_>, - span: Span, - ident: symbol::Ident, - ) -> Span { - let before_ident_span = mk_sp(span.lo(), ident.span.lo()); - let fn_lo = context - .snippet_provider - .span_before_last(before_ident_span, "fn"); - - mk_sp(span.lo(), fn_lo) - } } impl<'a> FmtVisitor<'a> { @@ -2483,7 +2467,12 @@ fn rewrite_fn_base( let mut result = String::with_capacity(1024); // Everything before `fn` - let span_before_fn = fn_sig.span_before_fn(context, span, ident); + let before_ident_span = mk_sp(span.lo(), ident.span.lo()); + let fn_lo = context + .snippet_provider + .span_before_last(before_ident_span, "fn"); + let span_before_fn = mk_sp(span.lo(), fn_lo); + if context .config .file_lines() @@ -2491,10 +2480,6 @@ fn rewrite_fn_base( { result.push_str(&fn_sig.to_str(context)); } else { - let before_ident_span = mk_sp(span_before_fn.hi(), ident.span.lo()); - let fn_lo = context - .snippet_provider - .span_before_last(before_ident_span, "fn"); result.push_str(context.snippet(mk_sp(span_before_fn.lo(), fn_lo))); } From 6b1ad0c4afd74a6e5d0e996181c1b6a9643c2615 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 7 May 2026 13:25:46 -0700 Subject: [PATCH 5/8] Let's also handle spacing between fn and the ident --- src/items.rs | 17 +++++++++++++++-- .../issue-6868-before-fn/fully_selected.rs | 4 +++- tests/target/issue-6868-before-fn/partial.rs | 3 ++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/items.rs b/src/items.rs index b1e62e4d761..b9a610022ef 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2483,8 +2483,21 @@ fn rewrite_fn_base( result.push_str(context.snippet(mk_sp(span_before_fn.lo(), fn_lo))); } - // fn foo - result.push_str("fn "); + result.push_str("fn"); + + // If both `fn` and the ident are selected, put them both on the same line. + // Otherwise, preserve the snippet between `fn` and the ident. + let fn_ident_span = mk_sp(fn_lo, ident.span.hi()); + if context + .config + .file_lines() + .contains(&context.psess.lookup_line_range(fn_ident_span)) + { + result.push(' '); + } else { + let fn_hi = fn_lo + BytePos(2); + result.push_str(context.snippet(mk_sp(fn_hi, ident.span.lo()))); + } // Generics. let overhead = if let FnBraceStyle::SameLine = fn_brace_style { diff --git a/tests/target/issue-6868-before-fn/fully_selected.rs b/tests/target/issue-6868-before-fn/fully_selected.rs index 7c1885e8cf6..2f78aa6ae0a 100644 --- a/tests/target/issue-6868-before-fn/fully_selected.rs +++ b/tests/target/issue-6868-before-fn/fully_selected.rs @@ -1,3 +1,5 @@ // rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fully_selected.rs","range":[3,11]}] -pub(crate) const unsafe extern "C" fn foo() {} +pub(crate) const unsafe extern "C" fn +foo() { +} diff --git a/tests/target/issue-6868-before-fn/partial.rs b/tests/target/issue-6868-before-fn/partial.rs index b6eda8a7e03..50bc2f433a3 100644 --- a/tests/target/issue-6868-before-fn/partial.rs +++ b/tests/target/issue-6868-before-fn/partial.rs @@ -8,5 +8,6 @@ const unsafe extern "C" -fn foo() { +fn +foo() { } From 569100533fa91d6ff68faeb8d0b74d8a3500fa12 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 7 May 2026 13:47:33 -0700 Subject: [PATCH 6/8] Add helper for checking spans against file-lines --- src/config/file_lines.rs | 9 ++++++++- src/items.rs | 13 +++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/config/file_lines.rs b/src/config/file_lines.rs index 85e91251307..37404fa4feb 100644 --- a/src/config/file_lines.rs +++ b/src/config/file_lines.rs @@ -6,11 +6,14 @@ use std::path::PathBuf; use std::sync::Arc; use std::{cmp, fmt, iter, str}; -use rustc_span::SourceFile; +use rustc_span::{SourceFile, Span}; use serde::{Deserialize, Deserializer, Serialize, Serializer, ser}; use serde_json as json; use thiserror::Error; +use crate::rewrite::RewriteContext; +use crate::source_map::LineRangeUtils; + /// A range of lines in a file, inclusive of both ends. pub struct LineRange { pub(crate) file: Arc, @@ -276,6 +279,10 @@ impl FileLines { pub(crate) fn contains_range(&self, file_name: &FileName, lo: usize, hi: usize) -> bool { self.file_range_matches(file_name, |r| r.contains(Range::new(lo, hi))) } + + pub(crate) fn contains_span(&self, context: &RewriteContext<'_>, span: Span) -> bool { + self.contains(&context.psess.lookup_line_range(span)) + } } /// `FileLines` files iterator. diff --git a/src/items.rs b/src/items.rs index b9a610022ef..5c62fd66afc 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2465,6 +2465,7 @@ fn rewrite_fn_base( let where_clause = &fn_sig.generics.where_clause; let mut result = String::with_capacity(1024); + let file_lines = context.config.file_lines(); // Everything before `fn` let before_ident_span = mk_sp(span.lo(), ident.span.lo()); @@ -2473,11 +2474,7 @@ fn rewrite_fn_base( .span_before_last(before_ident_span, "fn"); let span_before_fn = mk_sp(span.lo(), fn_lo); - if context - .config - .file_lines() - .contains(&context.psess.lookup_line_range(span_before_fn)) - { + if file_lines.contains_span(context, span_before_fn) { result.push_str(&fn_sig.to_str(context)); } else { result.push_str(context.snippet(mk_sp(span_before_fn.lo(), fn_lo))); @@ -2488,11 +2485,7 @@ fn rewrite_fn_base( // If both `fn` and the ident are selected, put them both on the same line. // Otherwise, preserve the snippet between `fn` and the ident. let fn_ident_span = mk_sp(fn_lo, ident.span.hi()); - if context - .config - .file_lines() - .contains(&context.psess.lookup_line_range(fn_ident_span)) - { + if file_lines.contains_span(context, fn_ident_span) { result.push(' '); } else { let fn_hi = fn_lo + BytePos(2); From cec85daadecce9e9d24a9852f16902ba9a92129b Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 7 May 2026 14:09:39 -0700 Subject: [PATCH 7/8] Add test for just selecting fn and the ident --- .../source/issue-6868-before-fn/fn_and_ident.rs | 16 ++++++++++++++++ .../target/issue-6868-before-fn/fn_and_ident.rs | 12 ++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/source/issue-6868-before-fn/fn_and_ident.rs create mode 100644 tests/target/issue-6868-before-fn/fn_and_ident.rs diff --git a/tests/source/issue-6868-before-fn/fn_and_ident.rs b/tests/source/issue-6868-before-fn/fn_and_ident.rs new file mode 100644 index 00000000000..aad742288ef --- /dev/null +++ b/tests/source/issue-6868-before-fn/fn_and_ident.rs @@ -0,0 +1,16 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fn_and_ident.rs","range":[11,12]}] + +pub +( +crate +) +const +unsafe +extern +"C" +fn +foo +( +) +{ +} diff --git a/tests/target/issue-6868-before-fn/fn_and_ident.rs b/tests/target/issue-6868-before-fn/fn_and_ident.rs new file mode 100644 index 00000000000..5e2d24580a8 --- /dev/null +++ b/tests/target/issue-6868-before-fn/fn_and_ident.rs @@ -0,0 +1,12 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fn_and_ident.rs","range":[11,12]}] + +pub +( +crate +) +const +unsafe +extern +"C" +fn foo() { +} From 57c1519b9457d58c8fac9549c77ef8c6145d7a57 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 7 May 2026 16:11:45 -0700 Subject: [PATCH 8/8] Actually let's use a macro to be more consistent with existing code --- src/config/file_lines.rs | 9 +-------- src/items.rs | 5 ++--- src/utils.rs | 10 ++++++++++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/config/file_lines.rs b/src/config/file_lines.rs index 37404fa4feb..85e91251307 100644 --- a/src/config/file_lines.rs +++ b/src/config/file_lines.rs @@ -6,14 +6,11 @@ use std::path::PathBuf; use std::sync::Arc; use std::{cmp, fmt, iter, str}; -use rustc_span::{SourceFile, Span}; +use rustc_span::SourceFile; use serde::{Deserialize, Deserializer, Serialize, Serializer, ser}; use serde_json as json; use thiserror::Error; -use crate::rewrite::RewriteContext; -use crate::source_map::LineRangeUtils; - /// A range of lines in a file, inclusive of both ends. pub struct LineRange { pub(crate) file: Arc, @@ -279,10 +276,6 @@ impl FileLines { pub(crate) fn contains_range(&self, file_name: &FileName, lo: usize, hi: usize) -> bool { self.file_range_matches(file_name, |r| r.contains(Range::new(lo, hi))) } - - pub(crate) fn contains_span(&self, context: &RewriteContext<'_>, span: Span) -> bool { - self.contains(&context.psess.lookup_line_range(span)) - } } /// `FileLines` files iterator. diff --git a/src/items.rs b/src/items.rs index 5c62fd66afc..4978a62ca01 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2465,7 +2465,6 @@ fn rewrite_fn_base( let where_clause = &fn_sig.generics.where_clause; let mut result = String::with_capacity(1024); - let file_lines = context.config.file_lines(); // Everything before `fn` let before_ident_span = mk_sp(span.lo(), ident.span.lo()); @@ -2474,7 +2473,7 @@ fn rewrite_fn_base( .span_before_last(before_ident_span, "fn"); let span_before_fn = mk_sp(span.lo(), fn_lo); - if file_lines.contains_span(context, span_before_fn) { + if file_lines_contains!(context, span_before_fn) { result.push_str(&fn_sig.to_str(context)); } else { result.push_str(context.snippet(mk_sp(span_before_fn.lo(), fn_lo))); @@ -2485,7 +2484,7 @@ fn rewrite_fn_base( // If both `fn` and the ident are selected, put them both on the same line. // Otherwise, preserve the snippet between `fn` and the ident. let fn_ident_span = mk_sp(fn_lo, ident.span.hi()); - if file_lines.contains_span(context, fn_ident_span) { + if file_lines_contains!(context, fn_ident_span) { result.push(' '); } else { let fn_hi = fn_lo + BytePos(2); diff --git a/src/utils.rs b/src/utils.rs index b676803379f..fed60101171 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -381,6 +381,16 @@ macro_rules! out_of_file_lines_range { }; } +/// Returns `true` if the given span is fully contained by the selected file lines. +macro_rules! file_lines_contains { + ($self:ident, $span:expr) => { + $self + .config + .file_lines() + .contains(&$self.psess.lookup_line_range($span)) + }; +} + macro_rules! skip_out_of_file_lines_range_err { ($self:ident, $span:expr) => { if out_of_file_lines_range!($self, $span) {