Skip to content

Commit 2e8c975

Browse files
authored
Merge pull request #2809 from nrc/formatting
Some formatting fixes
2 parents d325323 + 3abebf9 commit 2e8c975

File tree

10 files changed

+410
-168
lines changed

10 files changed

+410
-168
lines changed

src/attr.rs

Lines changed: 268 additions & 153 deletions
Large diffs are not rendered by default.

src/lists.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ where
533533
let pre_snippet = self
534534
.snippet_provider
535535
.span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
536-
.unwrap();
536+
.unwrap_or("");
537537
let trimmed_pre_snippet = pre_snippet.trim();
538538
let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
539539
let has_block_comment = trimmed_pre_snippet.starts_with("/*");
@@ -572,7 +572,7 @@ where
572572
let post_snippet = self
573573
.snippet_provider
574574
.span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
575-
.unwrap();
575+
.unwrap_or("");
576576

577577
let comment_end = match self.inner.peek() {
578578
Some(..) => {

src/matches.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,13 @@ fn rewrite_match_pattern(
289289
let pats_str = rewrite_multiple_patterns(context, pats, pat_shape)?;
290290

291291
// Guard
292-
let guard_str = rewrite_guard(context, guard, shape, trimmed_last_line_width(&pats_str))?;
292+
let guard_str = rewrite_guard(
293+
context,
294+
guard,
295+
shape,
296+
trimmed_last_line_width(&pats_str),
297+
pats_str.contains("\n"),
298+
)?;
293299

294300
Some(format!("{}{}", pats_str, guard_str))
295301
}
@@ -450,17 +456,20 @@ fn rewrite_guard(
450456
// The amount of space used up on this line for the pattern in
451457
// the arm (excludes offset).
452458
pattern_width: usize,
459+
multiline_pattern: bool,
453460
) -> Option<String> {
454461
if let Some(ref guard) = *guard {
455462
// First try to fit the guard string on the same line as the pattern.
456463
// 4 = ` if `, 5 = ` => {`
457464
let cond_shape = shape
458465
.offset_left(pattern_width + 4)
459466
.and_then(|s| s.sub_width(5));
460-
if let Some(cond_shape) = cond_shape {
461-
if let Some(cond_str) = guard.rewrite(context, cond_shape) {
462-
if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() {
463-
return Some(format!(" if {}", cond_str));
467+
if !multiline_pattern {
468+
if let Some(cond_shape) = cond_shape {
469+
if let Some(cond_str) = guard.rewrite(context, cond_shape) {
470+
if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() {
471+
return Some(format!(" if {}", cond_str));
472+
}
464473
}
465474
}
466475
}

src/shape.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct Indent {
2727
const INDENT_BUFFER_LEN: usize = 80;
2828
const INDENT_BUFFER: &str =
2929
"\n ";
30+
3031
impl Indent {
3132
pub fn new(block_indent: usize, alignment: usize) -> Indent {
3233
Indent {

tests/source/attrib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ fn attributes_on_statements() {
151151
foo!();
152152
}
153153

154-
// Large derive
154+
// Large derives
155+
#[derive(Add, Sub, Mul, Div, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Serialize, Mul)]
156+
157+
158+
/// Foo bar baz
159+
160+
155161
#[derive(Add, Sub, Mul, Div, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Serialize, Deserialize)]
156162
pub struct HP(pub u8);
157163

@@ -168,3 +174,22 @@ pub fn foo() {}
168174
#[clippy::bar(a, b, c)]
169175
pub fn foo() {}
170176

177+
mod issue_2620 {
178+
#[derive(Debug, StructOpt)]
179+
#[structopt(about = "Display information about the character on FF Logs")]
180+
pub struct Params {
181+
#[structopt(help = "The server the character is on")]
182+
server: String,
183+
#[structopt(help = "The character's first name")]
184+
first_name: String,
185+
#[structopt(help = "The character's last name")]
186+
last_name: String,
187+
#[structopt(
188+
short = "j",
189+
long = "job",
190+
help = "The job to look at",
191+
parse(try_from_str)
192+
)]
193+
job: Option<Job>
194+
}
195+
}

tests/source/match.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,19 @@ fn issue_2621() {
491491
| Foo::I => println!("With comment"), // Comment after line
492492
}
493493
}
494+
495+
fn issue_2377() {
496+
match tok {
497+
Tok::Not
498+
| Tok::BNot
499+
| Tok::Plus
500+
| Tok::Minus
501+
| Tok::PlusPlus
502+
| Tok::MinusMinus
503+
| Tok::Void
504+
| Tok::Delete if prec <= 16 => {
505+
// code here...
506+
}
507+
Tok::TypeOf if prec <= 16 => {}
508+
}
509+
}

tests/target/attrib.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ struct Foo {
7676
// #1668
7777

7878
/// Default path (*nix)
79-
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
79+
#[cfg(
80+
all(
81+
unix,
82+
not(target_os = "macos"),
83+
not(target_os = "ios"),
84+
not(target_os = "android")
85+
)
86+
)]
8087
fn foo() {
8188
#[cfg(target_os = "freertos")]
8289
match port_id {
@@ -155,9 +162,29 @@ fn attributes_on_statements() {
155162
foo!();
156163
}
157164

158-
// Large derive
159-
#[derive(Add, Sub, Mul, Div, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Serialize,
160-
Deserialize)]
165+
// Large derives
166+
#[derive(
167+
Add, Sub, Mul, Div, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Serialize, Mul,
168+
)]
169+
170+
/// Foo bar baz
171+
172+
#[derive(
173+
Add,
174+
Sub,
175+
Mul,
176+
Div,
177+
Clone,
178+
Copy,
179+
Eq,
180+
PartialEq,
181+
Ord,
182+
PartialOrd,
183+
Debug,
184+
Hash,
185+
Serialize,
186+
Deserialize,
187+
)]
161188
pub struct HP(pub u8);
162189

163190
// Long `#[doc = "..."]`
@@ -177,3 +204,23 @@ pub fn foo() {}
177204
#[clippy::bar=foo]
178205
#[clippy::bar(a, b, c)]
179206
pub fn foo() {}
207+
208+
mod issue_2620 {
209+
#[derive(Debug, StructOpt)]
210+
#[structopt(about = "Display information about the character on FF Logs")]
211+
pub struct Params {
212+
#[structopt(help = "The server the character is on")]
213+
server: String,
214+
#[structopt(help = "The character's first name")]
215+
first_name: String,
216+
#[structopt(help = "The character's last name")]
217+
last_name: String,
218+
#[structopt(
219+
short = "j",
220+
long = "job",
221+
help = "The job to look at",
222+
parse(try_from_str)
223+
)]
224+
job: Option<Job>,
225+
}
226+
}

tests/target/enum.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,12 @@ pub enum QlError {
255255
#[fail(display = "Translation error: from {} to {}", 0, 1)]
256256
TranslationError(String, String),
257257
// (kind, input, expected)
258-
#[fail(display = "Could not find {}: Found: {}, expected: {:?}", 0, 1, 2)]
258+
#[fail(
259+
display = "Could not find {}: Found: {}, expected: {:?}",
260+
0,
261+
1,
262+
2
263+
)]
259264
ResolveError(&'static str, String, Option<String>),
260265
}
261266

tests/target/match.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,21 @@ fn issue_2621() {
518518
Foo::I => println!("With comment"), // Comment after line
519519
}
520520
}
521+
522+
fn issue_2377() {
523+
match tok {
524+
Tok::Not
525+
| Tok::BNot
526+
| Tok::Plus
527+
| Tok::Minus
528+
| Tok::PlusPlus
529+
| Tok::MinusMinus
530+
| Tok::Void
531+
| Tok::Delete
532+
if prec <= 16 =>
533+
{
534+
// code here...
535+
}
536+
Tok::TypeOf if prec <= 16 => {}
537+
}
538+
}

tests/target/struct-field-attributes.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ fn new_foo() -> Foo {
3939
// #2044
4040
pub enum State {
4141
Closure(
42-
#[cfg_attr(feature = "serde_derive", serde(state_with = "::serialization::closure"))]
42+
#[cfg_attr(
43+
feature = "serde_derive",
44+
serde(state_with = "::serialization::closure")
45+
)]
4346
GcPtr<ClosureData>,
4447
),
4548
}
4649

4750
struct Fields(
48-
#[cfg_attr(feature = "serde_derive", serde(state_with = "::base::serialization::shared"))]
51+
#[cfg_attr(
52+
feature = "serde_derive",
53+
serde(state_with = "::base::serialization::shared")
54+
)]
4955
Arc<Vec<InternedStr>>,
5056
);
5157

0 commit comments

Comments
 (0)