-
Notifications
You must be signed in to change notification settings - Fork 931
Single line if's for 1 stmt block #6031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
cf18df7
50c1c83
2b9164e
fa1d45e
0c1f56d
dfba1ab
defb290
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1102,8 +1102,18 @@ impl<'a> Rewrite for ControlFlow<'a> { | |
}; | ||
let block_str = { | ||
let old_val = context.is_if_else_block.replace(self.else_block.is_some()); | ||
let result = | ||
rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true); | ||
let result = if self.keyword == "if" { | ||
format_if( | ||
context, | ||
shape, | ||
self.block, | ||
&cond_str, | ||
used_width, | ||
block_shape, | ||
) | ||
} else { | ||
rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true) | ||
}; | ||
context.is_if_else_block.replace(old_val); | ||
result? | ||
}; | ||
|
@@ -1158,6 +1168,72 @@ impl<'a> Rewrite for ControlFlow<'a> { | |
} | ||
} | ||
|
||
fn format_if( | ||
context: &RewriteContext<'_>, | ||
shape: Shape, | ||
block: &ast::Block, | ||
cond_str: &str, | ||
used_width: usize, | ||
block_shape: Shape, | ||
) -> Option<String> { | ||
let max_width = if context.config.single_line_if() { | ||
std::cmp::min( | ||
shape.width, | ||
context.config.single_line_simple_if_max_width(), | ||
) | ||
} else { | ||
shape.width | ||
}; | ||
let available_space = max_width.saturating_sub(used_width); | ||
let allow_single_line = context.config.single_line_if() | ||
&& available_space > 0 | ||
&& allow_single_line_if(&cond_str, block); | ||
|
||
let result = if allow_single_line { | ||
let mut single_line_attempt = | ||
rewrite_block_inner(block, None, None, true, context, block_shape)?; | ||
if !single_line_attempt.contains('\n') && single_line_attempt.len() > available_space { | ||
single_line_attempt = | ||
rewrite_block_inner(block, None, None, false, context, block_shape)?; | ||
} | ||
Some(single_line_attempt) | ||
} else { | ||
rewrite_block_with_visitor(context, "", block, None, None, block_shape, true) | ||
}; | ||
result | ||
} | ||
|
||
fn allow_single_line_if(result: &str, block: &ast::Block) -> bool { | ||
if result.contains('\n') { | ||
return false; | ||
} | ||
Comment on lines
+1207
to
+1209
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does fn main() {
#[allow(unused)]
if true {
println!("case 1");
}
#[allow(unused)]
// What about if there's a comment here?
if true {
println!("case 2 with comment");
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also don't want to allow single lining if the block contains a comment. I think the block rewriting handles that but just a call out that we'd need a test for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ask because I overlooked this case when adding |
||
if block.stmts.len() == 0 { | ||
return true; | ||
} | ||
if block.stmts.len() == 1 { | ||
return is_simple_control_flow_stmt(&block.stmts[0]); | ||
} | ||
false | ||
} | ||
|
||
fn is_simple_control_flow_stmt(stmt: &ast::Stmt) -> bool { | ||
match stmt.kind { | ||
ast::StmtKind::Expr(ref expr) => match expr.kind { | ||
Sjael marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ast::ExprKind::Continue(..) => true, | ||
ast::ExprKind::Break(_, ref opt_expr) | ast::ExprKind::Ret(ref opt_expr) => { | ||
if let Some(_) = *opt_expr { | ||
// Do not allow single line if block has `return/break` with a returned value | ||
false | ||
} else { | ||
true | ||
} | ||
} | ||
_ => false, | ||
}, | ||
_ => false, | ||
} | ||
} | ||
|
||
fn rewrite_label(opt_label: Option<ast::Label>) -> Cow<'static, str> { | ||
match opt_label { | ||
Some(label) => Cow::from(format!("{}: ", label.ident)), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// rustfmt-single_line_if: false | ||
// rustfmt-single_line_if_else_max_width: 0 | ||
|
||
fn main() { | ||
let x = if true { 1 } else { 2 }; | ||
|
||
funk(if test() { 1 } else { 2 }, arg2); | ||
|
||
if true { 1 } else { 2 } | ||
|
||
if test() { 1 } else { 2 } | ||
|
||
if let Some(a) = b { return } | ||
|
||
if let Some(a) = b { do_something(); return } | ||
|
||
if let Some(a) = b { return } else { continue } | ||
|
||
let a = if 1 > 2 { | ||
unreachable!() | ||
} else { | ||
10 | ||
}; | ||
|
||
let a = if x { 1 } else if y { 2 } else { 3 }; | ||
|
||
if true { continue } | ||
|
||
if true { | ||
continue | ||
} | ||
|
||
if width == is_49_characters____long { continue } | ||
if width == is_50_characters_____long { continue } | ||
if width == is_51_characters______long { continue } | ||
|
||
if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return } | ||
|
||
if true { return } else { break } | ||
|
||
let x = if true { return } else { break }; | ||
|
||
let b = if cond() { | ||
5 | ||
} else { | ||
// Brief comment. | ||
10 | ||
}; | ||
|
||
let c = if cond() { | ||
statement(); | ||
|
||
5 | ||
} else { | ||
10 | ||
}; | ||
|
||
if cond() { statement(); } else { other_statement(); } | ||
} |
Uh oh!
There was an error while loading. Please reload this page.