Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d7e1f00

Browse files
committed
Put arguments of the fail attribute on the same line as println!
1 parent 052ba6c commit d7e1f00

File tree

4 files changed

+55
-51
lines changed

4 files changed

+55
-51
lines changed

src/attr.rs

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ use config::lists::*;
1515
use config::IndentStyle;
1616
use expr::rewrite_literal;
1717
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
18+
use overflow;
1819
use rewrite::{Rewrite, RewriteContext};
1920
use shape::Shape;
2021
use types::{rewrite_path, PathContext};
2122
use utils::{count_newlines, mk_sp};
2223

23-
use std::borrow::Cow;
2424
use syntax::ast;
2525
use syntax::source_map::{BytePos, Span, DUMMY_SP};
2626

@@ -216,56 +216,21 @@ impl Rewrite for ast::MetaItem {
216216
}
217217
ast::MetaItemKind::List(ref list) => {
218218
let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?;
219-
220-
let has_comma = ::expr::span_ends_with_comma(context, self.span);
221-
let trailing_comma = if has_comma { "," } else { "" };
222-
let combine = list.len() == 1 && match list[0].node {
223-
ast::NestedMetaItemKind::Literal(..) => false,
224-
ast::NestedMetaItemKind::MetaItem(ref inner_meta_item) => {
225-
match inner_meta_item.node {
226-
ast::MetaItemKind::List(..) => rewrite_path(
227-
context,
228-
PathContext::Type,
229-
None,
230-
&inner_meta_item.ident,
231-
shape,
232-
)
233-
.map_or(false, |s| s.len() + path.len() + 2 <= shape.width),
234-
_ => false,
235-
}
236-
}
237-
};
238-
239-
let argument_shape = argument_shape(
240-
path.len() + 1,
241-
2 + trailing_comma.len(),
242-
combine,
243-
shape,
219+
let has_trailing_comma = ::expr::span_ends_with_comma(context, self.span);
220+
overflow::rewrite_with_parens(
244221
context,
245-
)?;
246-
let item_str = format_arg_list(
222+
&path,
247223
list.iter(),
248-
|nested_meta_item| nested_meta_item.span.lo(),
249-
|nested_meta_item| nested_meta_item.span.hi(),
250-
|nested_meta_item| nested_meta_item.rewrite(context, argument_shape),
224+
// 1 = "]"
225+
shape.sub_width(1)?,
251226
self.span,
252-
context,
253-
argument_shape,
254-
// 3 = "()" and "]"
255-
shape
256-
.offset_left(path.len())?
257-
.sub_width(3 + trailing_comma.len())?,
258-
Some(context.config.width_heuristics().fn_call_width),
259-
combine,
260-
)?;
261-
262-
let indent = if item_str.starts_with('\n') {
263-
shape.indent.to_string_with_newline(context.config)
264-
} else {
265-
Cow::Borrowed("")
266-
};
267-
268-
format!("{}({}{}{})", path, item_str, trailing_comma, indent)
227+
context.config.width_heuristics().fn_call_width,
228+
Some(if has_trailing_comma {
229+
SeparatorTactic::Always
230+
} else {
231+
SeparatorTactic::Never
232+
}),
233+
)?
269234
}
270235
ast::MetaItemKind::NameValue(ref literal) => {
271236
let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?;

src/overflow.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,17 @@ const SPECIAL_MACRO_WHITELIST: &[(&str, usize)] = &[
6767
("debug_assert_ne!", 2),
6868
];
6969

70+
const SPECIAL_ATTR_WHITELIST: &[(&str, usize)] = &[
71+
// From the `failure` crate.
72+
("fail", 0),
73+
];
74+
75+
#[derive(Debug)]
7076
pub enum OverflowableItem<'a> {
7177
Expr(&'a ast::Expr),
7278
GenericParam(&'a ast::GenericParam),
7379
MacroArg(&'a MacroArg),
80+
NestedMetaItem(&'a ast::NestedMetaItem),
7481
SegmentParam(&'a SegmentParam<'a>),
7582
StructField(&'a ast::StructField),
7683
TuplePatField(&'a TuplePatField<'a>),
@@ -98,6 +105,7 @@ impl<'a> OverflowableItem<'a> {
98105
OverflowableItem::Expr(expr) => f(*expr),
99106
OverflowableItem::GenericParam(gp) => f(*gp),
100107
OverflowableItem::MacroArg(macro_arg) => f(*macro_arg),
108+
OverflowableItem::NestedMetaItem(nmi) => f(*nmi),
101109
OverflowableItem::SegmentParam(sp) => f(*sp),
102110
OverflowableItem::StructField(sf) => f(*sf),
103111
OverflowableItem::TuplePatField(pat) => f(*pat),
@@ -109,6 +117,13 @@ impl<'a> OverflowableItem<'a> {
109117
match self {
110118
OverflowableItem::Expr(expr) => is_simple_expr(expr),
111119
OverflowableItem::MacroArg(MacroArg::Expr(expr)) => is_simple_expr(expr),
120+
OverflowableItem::NestedMetaItem(nested_meta_item) => match nested_meta_item.node {
121+
ast::NestedMetaItemKind::Literal(..) => true,
122+
ast::NestedMetaItemKind::MetaItem(ref meta_item) => match meta_item.node {
123+
ast::MetaItemKind::Word => true,
124+
_ => false,
125+
},
126+
},
112127
_ => false,
113128
}
114129
}
@@ -149,6 +164,12 @@ impl<'a> OverflowableItem<'a> {
149164
MacroArg::Pat(..) => false,
150165
MacroArg::Item(..) => len == 1,
151166
},
167+
OverflowableItem::NestedMetaItem(nested_meta_item) if len == 1 => {
168+
match nested_meta_item.node {
169+
ast::NestedMetaItemKind::Literal(..) => false,
170+
ast::NestedMetaItemKind::MetaItem(..) => true,
171+
}
172+
}
152173
OverflowableItem::SegmentParam(seg) => match seg {
153174
SegmentParam::Type(ty) => can_be_overflowed_type(context, ty, len),
154175
_ => false,
@@ -158,6 +179,14 @@ impl<'a> OverflowableItem<'a> {
158179
_ => false,
159180
}
160181
}
182+
183+
fn whitelist(&self) -> &'static [(&'static str, usize)] {
184+
match self {
185+
OverflowableItem::MacroArg(..) => SPECIAL_MACRO_WHITELIST,
186+
OverflowableItem::NestedMetaItem(..) => SPECIAL_ATTR_WHITELIST,
187+
_ => &[],
188+
}
189+
}
161190
}
162191

163192
pub trait IntoOverflowableItem<'a>: Rewrite + Spanned {
@@ -201,7 +230,7 @@ macro impl_into_overflowable_item_for_rustfmt_types {
201230
}
202231
}
203232

204-
impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, StructField, Ty);
233+
impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, StructField, Ty);
205234
impl_into_overflowable_item_for_rustfmt_types!([MacroArg], [SegmentParam, TuplePatField]);
206235

207236
pub fn into_overflowable_list<'a, T>(
@@ -655,7 +684,7 @@ fn last_item_shape(
655684
shape: Shape,
656685
args_max_width: usize,
657686
) -> Option<Shape> {
658-
if items.len() == 1 && !lists.iter().next()?.is_nested_call() {
687+
if items.len() == 1 && !lists.get(0)?.is_nested_call() {
659688
return Some(shape);
660689
}
661690
let offset = items.iter().rev().skip(1).fold(0, |acc, i| {

src/patterns.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ impl Rewrite for FieldPat {
264264
}
265265
}
266266

267+
#[derive(Debug)]
267268
pub enum TuplePatField<'a> {
268269
Pat(&'a ptr::P<ast::Pat>),
269270
Dotdot(Span),

src/spanned.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::{ast, ptr, source_map::Span};
11+
use syntax::{
12+
ast, ptr,
13+
source_map::{self, Span},
14+
};
1215

1316
use macros::MacroArg;
1417
use utils::{mk_sp, outer_attributes};
@@ -26,6 +29,12 @@ impl<T: Spanned> Spanned for ptr::P<T> {
2629
}
2730
}
2831

32+
impl<T> Spanned for source_map::Spanned<T> {
33+
fn span(&self) -> Span {
34+
self.span
35+
}
36+
}
37+
2938
macro_rules! span_with_attrs_lo_hi {
3039
($this:ident, $lo:expr, $hi:expr) => {{
3140
let attrs = outer_attributes(&$this.attrs);

0 commit comments

Comments
 (0)