Skip to content

Commit 8e8a5b3

Browse files
committed
Merge pull request #146 from marcusklaas/fix-inverted-span
Format unnamed function arguments
2 parents 979d0c9 + 500fb78 commit 8e8a5b3

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/items.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ impl<'a> FmtVisitor<'a> {
289289

290290
arg_items = itemize_list(self.codemap,
291291
arg_items,
292-
args[min_args-1..].iter(),
292+
args[min_args-1..].iter().cloned(),
293293
",",
294294
")",
295-
|arg| arg.pat.span.lo,
295+
span_lo_for_arg,
296296
|arg| arg.ty.span.hi,
297297
|_| String::new(),
298298
comment_span_start,
@@ -780,9 +780,29 @@ impl<'a> FmtVisitor<'a> {
780780
// TODO we farm this out, but this could spill over the column limit, so we
781781
// ought to handle it properly.
782782
fn rewrite_fn_input(&self, arg: &ast::Arg) -> String {
783-
format!("{}: {}",
784-
pprust::pat_to_string(&arg.pat),
785-
pprust::ty_to_string(&arg.ty))
783+
if is_named_arg(arg) {
784+
format!("{}: {}",
785+
pprust::pat_to_string(&arg.pat),
786+
pprust::ty_to_string(&arg.ty))
787+
} else {
788+
pprust::ty_to_string(&arg.ty)
789+
}
790+
}
791+
}
792+
793+
fn span_lo_for_arg(arg: &ast::Arg) -> BytePos {
794+
if is_named_arg(arg) {
795+
arg.pat.span.lo
796+
} else {
797+
arg.ty.span.lo
798+
}
799+
}
800+
801+
fn is_named_arg(arg: &ast::Arg) -> bool {
802+
if let ast::Pat_::PatIdent(_, ident, _) = arg.pat.node {
803+
ident.node != token::special_idents::invalid
804+
} else {
805+
true
786806
}
787807
}
788808

tests/target/trait.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ trait Foo {
1818
fn read(&mut self, x: BufReader<R> /* Used to be MemReader */)
1919
where R: Read;
2020
}
21+
22+
pub trait WriteMessage {
23+
fn write_message(&mut self, &FrontendMessage) -> io::Result<()>;
24+
}

0 commit comments

Comments
 (0)