mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-22 20:03:37 +00:00
leave the comment in parentheses of argumentless Fn (#3518)
This commit is contained in:
parent
1f61286293
commit
05547d90b5
@ -11,6 +11,7 @@ pub trait SpanUtils {
|
|||||||
fn span_after(&self, original: Span, needle: &str) -> BytePos;
|
fn span_after(&self, original: Span, needle: &str) -> BytePos;
|
||||||
fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
|
fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
|
||||||
fn span_before(&self, original: Span, needle: &str) -> BytePos;
|
fn span_before(&self, original: Span, needle: &str) -> BytePos;
|
||||||
|
fn span_before_last(&self, original: Span, needle: &str) -> BytePos;
|
||||||
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
|
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
|
||||||
fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos>;
|
fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos>;
|
||||||
}
|
}
|
||||||
@ -56,6 +57,17 @@ impl<'a> SpanUtils for SnippetProvider<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn span_before_last(&self, original: Span, needle: &str) -> BytePos {
|
||||||
|
let snippet = self.span_to_snippet(original).unwrap();
|
||||||
|
let mut offset = 0;
|
||||||
|
|
||||||
|
while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
|
||||||
|
offset += additional_offset + needle.len();
|
||||||
|
}
|
||||||
|
|
||||||
|
original.lo() + BytePos(offset as u32 - 1)
|
||||||
|
}
|
||||||
|
|
||||||
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
|
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
|
||||||
self.opt_span_before(original, needle)
|
self.opt_span_before(original, needle)
|
||||||
.map(|bytepos| bytepos + BytePos(needle.len() as u32))
|
.map(|bytepos| bytepos + BytePos(needle.len() as u32))
|
||||||
|
103
src/types.rs
103
src/types.rs
@ -8,7 +8,9 @@ use syntax::symbol::keywords;
|
|||||||
use crate::config::lists::*;
|
use crate::config::lists::*;
|
||||||
use crate::config::{IndentStyle, TypeDensity};
|
use crate::config::{IndentStyle, TypeDensity};
|
||||||
use crate::expr::{format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ExprType};
|
use crate::expr::{format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ExprType};
|
||||||
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
|
use crate::lists::{
|
||||||
|
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
|
||||||
|
};
|
||||||
use crate::macros::{rewrite_macro, MacroPosition};
|
use crate::macros::{rewrite_macro, MacroPosition};
|
||||||
use crate::overflow;
|
use crate::overflow;
|
||||||
use crate::pairs::{rewrite_pair, PairParts};
|
use crate::pairs::{rewrite_pair, PairParts};
|
||||||
@ -314,46 +316,55 @@ where
|
|||||||
let offset = shape.indent + 1;
|
let offset = shape.indent + 1;
|
||||||
Shape::legacy(budget, offset)
|
Shape::legacy(budget, offset)
|
||||||
};
|
};
|
||||||
|
|
||||||
let list_lo = context.snippet_provider.span_after(span, "(");
|
let list_lo = context.snippet_provider.span_after(span, "(");
|
||||||
let items = itemize_list(
|
let (list_str, tactic) = if inputs.len() == 0 {
|
||||||
context.snippet_provider,
|
let tactic = get_tactics(&[], &output, shape);
|
||||||
inputs,
|
let list_hi = context.snippet_provider.span_before_last(span, ")");
|
||||||
")",
|
let comment = context
|
||||||
",",
|
.snippet_provider
|
||||||
|arg| arg.span().lo(),
|
.span_to_snippet(mk_sp(list_lo, list_hi))?
|
||||||
|arg| arg.span().hi(),
|
.trim();
|
||||||
|arg| arg.rewrite(context, list_shape),
|
let comment = if comment.starts_with("//") {
|
||||||
list_lo,
|
format!(
|
||||||
span.hi(),
|
"{}{}{}",
|
||||||
false,
|
&list_shape.indent.to_string_with_newline(context.config),
|
||||||
);
|
comment,
|
||||||
|
&shape.block().indent.to_string_with_newline(context.config)
|
||||||
let item_vec: Vec<_> = items.collect();
|
)
|
||||||
|
} else {
|
||||||
// If the return type is multi-lined, then force to use multiple lines for
|
comment.to_string()
|
||||||
// arguments as well.
|
};
|
||||||
let tactic = if output.contains('\n') {
|
(comment, tactic)
|
||||||
DefinitiveListTactic::Vertical
|
|
||||||
} else {
|
} else {
|
||||||
definitive_tactic(
|
let items = itemize_list(
|
||||||
&*item_vec,
|
context.snippet_provider,
|
||||||
ListTactic::HorizontalVertical,
|
inputs,
|
||||||
Separator::Comma,
|
")",
|
||||||
shape.width.saturating_sub(2 + output.len()),
|
",",
|
||||||
)
|
|arg| arg.span().lo(),
|
||||||
};
|
|arg| arg.span().hi(),
|
||||||
let trailing_separator = if !context.use_block_indent() || variadic {
|
|arg| arg.rewrite(context, list_shape),
|
||||||
SeparatorTactic::Never
|
list_lo,
|
||||||
} else {
|
span.hi(),
|
||||||
context.config.trailing_comma()
|
false,
|
||||||
};
|
);
|
||||||
|
|
||||||
let fmt = ListFormatting::new(list_shape, context.config)
|
let item_vec: Vec<_> = items.collect();
|
||||||
.tactic(tactic)
|
let tactic = get_tactics(&item_vec, &output, shape);
|
||||||
.trailing_separator(trailing_separator)
|
let trailing_separator = if !context.use_block_indent() || variadic {
|
||||||
.ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
|
SeparatorTactic::Never
|
||||||
.preserve_newline(true);
|
} else {
|
||||||
let list_str = write_list(&item_vec, &fmt)?;
|
context.config.trailing_comma()
|
||||||
|
};
|
||||||
|
|
||||||
|
let fmt = ListFormatting::new(list_shape, context.config)
|
||||||
|
.tactic(tactic)
|
||||||
|
.trailing_separator(trailing_separator)
|
||||||
|
.ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
|
||||||
|
.preserve_newline(true);
|
||||||
|
(write_list(&item_vec, &fmt)?, tactic)
|
||||||
|
};
|
||||||
|
|
||||||
let args = if tactic == DefinitiveListTactic::Horizontal || !context.use_block_indent() {
|
let args = if tactic == DefinitiveListTactic::Horizontal || !context.use_block_indent() {
|
||||||
format!("({})", list_str)
|
format!("({})", list_str)
|
||||||
@ -381,6 +392,22 @@ fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
|
|||||||
colon_spaces(context.config)
|
colon_spaces(context.config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the return type is multi-lined, then force to use multiple lines for
|
||||||
|
// arguments as well.
|
||||||
|
fn get_tactics(item_vec: &[ListItem], output: &str, shape: Shape) -> DefinitiveListTactic {
|
||||||
|
if output.contains('\n') {
|
||||||
|
DefinitiveListTactic::Vertical
|
||||||
|
} else {
|
||||||
|
definitive_tactic(
|
||||||
|
item_vec,
|
||||||
|
ListTactic::HorizontalVertical,
|
||||||
|
Separator::Comma,
|
||||||
|
// 2 is for the case of ',\n'
|
||||||
|
shape.width.saturating_sub(2 + output.len()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Rewrite for ast::WherePredicate {
|
impl Rewrite for ast::WherePredicate {
|
||||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||||
// FIXME: dead spans?
|
// FIXME: dead spans?
|
||||||
|
29
tests/source/issue-3508.rs
Normal file
29
tests/source/issue-3508.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
fn foo<F>(foo2: F)
|
||||||
|
where
|
||||||
|
F: Fn(
|
||||||
|
// this comment is deleted
|
||||||
|
),
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo_block<F>(foo2: F)
|
||||||
|
where
|
||||||
|
F: Fn(
|
||||||
|
/* this comment is deleted */
|
||||||
|
),
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar(
|
||||||
|
bar2: impl Fn(
|
||||||
|
// this comment is deleted
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar_block(
|
||||||
|
bar2: impl Fn(
|
||||||
|
/* this comment is deleted */
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
}
|
22
tests/target/issue-3508.rs
Normal file
22
tests/target/issue-3508.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
fn foo<F>(foo2: F)
|
||||||
|
where
|
||||||
|
F: Fn(
|
||||||
|
// this comment is deleted
|
||||||
|
),
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo_block<F>(foo2: F)
|
||||||
|
where
|
||||||
|
F: Fn(/* this comment is deleted */),
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar(
|
||||||
|
bar2: impl Fn(
|
||||||
|
// this comment is deleted
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar_block(bar2: impl Fn(/* this comment is deleted */)) {}
|
Loading…
Reference in New Issue
Block a user