Merge pull request #191 from myromeu/is_empty

Fixes nrc/rustfmt#187
This commit is contained in:
cassiersg 2015-08-25 00:13:31 +02:00
commit ca39467cde
6 changed files with 34 additions and 34 deletions

View File

@ -216,7 +216,7 @@ impl Rewrite for ast::Block {
// Extract comment between unsafe and block start. // Extract comment between unsafe and block start.
let trimmed = &snippet[6..open_pos].trim(); let trimmed = &snippet[6..open_pos].trim();
if trimmed.len() > 0 { if !trimmed.is_empty() {
// 9 = "unsafe {".len(), 7 = "unsafe ".len() // 9 = "unsafe {".len(), 7 = "unsafe ".len()
format!("unsafe {} ", rewrite_comment(trimmed, true, width - 9, offset + 7)) format!("unsafe {} ", rewrite_comment(trimmed, true, width - 9, offset + 7))
} else { } else {
@ -733,7 +733,7 @@ fn rewrite_call(context: &RewriteContext,
let callee_str = try_opt!(callee.rewrite(context, max_callee_width, offset)); let callee_str = try_opt!(callee.rewrite(context, max_callee_width, offset));
debug!("rewrite_call, callee_str: `{}`", callee_str); debug!("rewrite_call, callee_str: `{}`", callee_str);
if args.len() == 0 { if args.is_empty() {
return Some(format!("{}()", callee_str)); return Some(format!("{}()", callee_str));
} }
@ -799,7 +799,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
offset: usize) offset: usize)
-> Option<String> { -> Option<String> {
debug!("rewrite_struct_lit: width {}, offset {}", width, offset); debug!("rewrite_struct_lit: width {}, offset {}", width, offset);
assert!(fields.len() > 0 || base.is_some()); assert!(!fields.is_empty() || base.is_some());
enum StructLitField<'a> { enum StructLitField<'a> {
Regular(&'a ast::Field), Regular(&'a ast::Field),
@ -1008,7 +1008,7 @@ fn rewrite_unary_op(context: &RewriteContext,
ast::UnOp::UnNeg => "-", ast::UnOp::UnNeg => "-",
}; };
let subexpr = try_opt!(expr.rewrite(context, width - operator_str.len(), offset)); let subexpr = try_opt!(expr.rewrite(context, try_opt!(width.checked_sub(operator_str.len())), offset));
Some(format!("{}{}", operator_str, subexpr)) Some(format!("{}{}", operator_str, subexpr))
} }

View File

@ -51,13 +51,13 @@ impl Rewrite for ast::ViewPath {
fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem) -> String { fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem) -> String {
if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node { if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
if path_str.len() == 0 { if path_str.is_empty() {
name.to_string() name.to_string()
} else { } else {
format!("{}::{}", path_str, name) format!("{}::{}", path_str, name)
} }
} else { } else {
if path_str.len() != 0 { if !path_str.is_empty() {
path_str path_str
} else { } else {
// This catches the import: use {self}, which is a compiler error, so we just // This catches the import: use {self}, which is a compiler error, so we just
@ -86,7 +86,7 @@ pub fn rewrite_use_list(width: usize,
} }
// 2 = :: // 2 = ::
let path_separation_w = if path_str.len() > 0 { let path_separation_w = if !path_str.is_empty() {
2 2
} else { } else {
0 0
@ -151,7 +151,7 @@ pub fn rewrite_use_list(width: usize,
let list = write_list(&items[first_index..], &fmt); let list = write_list(&items[first_index..], &fmt);
Some(if path_str.len() == 0 { Some(if path_str.is_empty() {
format!("{{{}}}", list) format!("{{{}}}", list)
} else { } else {
format!("{}::{{{}}}", path_str, list) format!("{}::{{{}}}", path_str, list)

View File

@ -178,7 +178,7 @@ impl<'a> FmtVisitor<'a> {
result.push(')'); result.push(')');
// Return type. // Return type.
if ret_str.len() > 0 { if !ret_str.is_empty() {
// If we've already gone multi-line, or the return type would push // If we've already gone multi-line, or the return type would push
// over the max width, then put the return type on a new line. // over the max width, then put the return type on a new line.
if result.contains("\n") || if result.contains("\n") ||
@ -200,11 +200,11 @@ impl<'a> FmtVisitor<'a> {
// Comment between return type and the end of the decl. // Comment between return type and the end of the decl.
let snippet_lo = fd.output.span().hi; let snippet_lo = fd.output.span().hi;
if where_clause.predicates.len() == 0 { if where_clause.predicates.is_empty() {
let snippet_hi = span.hi; let snippet_hi = span.hi;
let snippet = self.snippet(codemap::mk_sp(snippet_lo, snippet_hi)); let snippet = self.snippet(codemap::mk_sp(snippet_lo, snippet_hi));
let snippet = snippet.trim(); let snippet = snippet.trim();
if snippet.len() > 0 { if !snippet.is_empty() {
result.push(' '); result.push(' ');
result.push_str(snippet); result.push_str(snippet);
} }
@ -342,7 +342,7 @@ impl<'a> FmtVisitor<'a> {
fn newline_for_brace(&self, where_clause: &ast::WhereClause) -> bool { fn newline_for_brace(&self, where_clause: &ast::WhereClause) -> bool {
match self.config.fn_brace_style { match self.config.fn_brace_style {
BraceStyle::AlwaysNextLine => true, BraceStyle::AlwaysNextLine => true,
BraceStyle::SameLineWhere if where_clause.predicates.len() > 0 => true, BraceStyle::SameLineWhere if !where_clause.predicates.is_empty() => true,
_ => false, _ => false,
} }
} }
@ -399,7 +399,7 @@ impl<'a> FmtVisitor<'a> {
let mut result = String::new(); let mut result = String::new();
if types.len() > 0 { if !types.is_empty() {
let items = itemize_list(self.codemap, let items = itemize_list(self.codemap,
types.iter(), types.iter(),
")", ")",
@ -482,7 +482,7 @@ impl<'a> FmtVisitor<'a> {
let header_str = self.format_header(item_name, ident, vis); let header_str = self.format_header(item_name, ident, vis);
result.push_str(&header_str); result.push_str(&header_str);
if struct_def.fields.len() == 0 { if struct_def.fields.is_empty() {
result.push(';'); result.push(';');
return result; return result;
} }
@ -513,7 +513,7 @@ impl<'a> FmtVisitor<'a> {
terminator, terminator,
|field| { |field| {
// Include attributes and doc comments, if present // Include attributes and doc comments, if present
if field.node.attrs.len() > 0 { if !field.node.attrs.is_empty() {
field.node.attrs[0].span.lo field.node.attrs[0].span.lo
} else { } else {
field.span.lo field.span.lo
@ -602,7 +602,7 @@ impl<'a> FmtVisitor<'a> {
-> String { -> String {
let mut result = self.rewrite_generics(generics, offset, span); let mut result = self.rewrite_generics(generics, offset, span);
if generics.where_clause.predicates.len() > 0 || result.contains('\n') { if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
result.push_str(&self.rewrite_where_clause(&generics.where_clause, result.push_str(&self.rewrite_where_clause(&generics.where_clause,
self.config, self.config,
self.block_indent, self.block_indent,
@ -635,7 +635,7 @@ impl<'a> FmtVisitor<'a> {
let indent = self.block_indent + self.config.tab_spaces; let indent = self.block_indent + self.config.tab_spaces;
let mut attr_str = self.rewrite_attrs(&field.node.attrs, indent); let mut attr_str = self.rewrite_attrs(&field.node.attrs, indent);
if attr_str.len() > 0 { if !attr_str.is_empty() {
attr_str.push('\n'); attr_str.push('\n');
attr_str.push_str(&make_indent(indent)); attr_str.push_str(&make_indent(indent));
} }
@ -651,7 +651,7 @@ impl<'a> FmtVisitor<'a> {
// there is a where clause at all. // there is a where clause at all.
let lifetimes: &[_] = &generics.lifetimes; let lifetimes: &[_] = &generics.lifetimes;
let tys: &[_] = &generics.ty_params; let tys: &[_] = &generics.ty_params;
if lifetimes.len() + tys.len() == 0 { if lifetimes.is_empty() && tys.is_empty() {
return String::new(); return String::new();
} }
@ -671,7 +671,7 @@ impl<'a> FmtVisitor<'a> {
// Extract comments between generics. // Extract comments between generics.
let lt_spans = lifetimes.iter().map(|l| { let lt_spans = lifetimes.iter().map(|l| {
let hi = if l.bounds.len() == 0 { let hi = if l.bounds.is_empty() {
l.lifetime.span.hi l.lifetime.span.hi
} else { } else {
l.bounds[l.bounds.len() - 1].span.hi l.bounds[l.bounds.len() - 1].span.hi
@ -705,7 +705,7 @@ impl<'a> FmtVisitor<'a> {
indent: usize, indent: usize,
span_end: BytePos) span_end: BytePos)
-> String { -> String {
if where_clause.predicates.len() == 0 { if where_clause.predicates.is_empty() {
return String::new(); return String::new();
} }
@ -833,7 +833,7 @@ fn span_for_ty_param(ty: &ast::TyParam) -> Span {
if let Some(ref def) = ty.default { if let Some(ref def) = ty.default {
return codemap::mk_sp(lo, def.span.hi); return codemap::mk_sp(lo, def.span.hi);
} }
if ty.bounds.len() == 0 { if ty.bounds.is_empty() {
return ty.span; return ty.span;
} }
let hi = match ty.bounds[ty.bounds.len() - 1] { let hi = match ty.bounds[ty.bounds.len() - 1] {

View File

@ -92,7 +92,7 @@ impl ListItem {
// FIXME: this has grown into a monstrosity // FIXME: this has grown into a monstrosity
// TODO: add unit tests // TODO: add unit tests
pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> String { pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> String {
if items.len() == 0 { if items.is_empty() {
return String::new(); return String::new();
} }
@ -270,7 +270,7 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
(self.get_lo)(&item))) (self.get_lo)(&item)))
.unwrap(); .unwrap();
let pre_snippet = pre_snippet.trim(); let pre_snippet = pre_snippet.trim();
let pre_comment = if pre_snippet.len() > 0 { let pre_comment = if !pre_snippet.is_empty() {
Some(pre_snippet.to_owned()) Some(pre_snippet.to_owned())
} else { } else {
None None
@ -328,7 +328,7 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
post_snippet = post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space); post_snippet = post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space);
} }
let post_comment = if post_snippet.len() > 0 { let post_comment = if !post_snippet.is_empty() {
Some(post_snippet.to_owned()) Some(post_snippet.to_owned())
} else { } else {
None None

View File

@ -190,9 +190,9 @@ fn rewrite_segment(segment: &ast::PathSegment,
let offset = offset + ident_len; let offset = offset + ident_len;
let params = match segment.parameters { let params = match segment.parameters {
ast::PathParameters::AngleBracketedParameters(ref data) if data.lifetimes.len() > 0 || ast::PathParameters::AngleBracketedParameters(ref data) if !data.lifetimes.is_empty() ||
data.types.len() > 0 || !data.types.is_empty() ||
data.bindings.len() > 0 => { !data.bindings.is_empty() => {
let param_list = data.lifetimes.iter() let param_list = data.lifetimes.iter()
.map(SegmentParam::LifeTime) .map(SegmentParam::LifeTime)
.chain(data.types.iter() .chain(data.types.iter()
@ -267,7 +267,7 @@ impl Rewrite for ast::WherePredicate {
ref bounded_ty, ref bounded_ty,
ref bounds, ref bounds,
..}) => { ..}) => {
if bound_lifetimes.len() > 0 { if !bound_lifetimes.is_empty() {
let lifetime_str = bound_lifetimes.iter().map(|lt| { let lifetime_str = bound_lifetimes.iter().map(|lt| {
lt.rewrite(context, width, offset).unwrap() lt.rewrite(context, width, offset).unwrap()
}).collect::<Vec<_>>().join(", "); }).collect::<Vec<_>>().join(", ");
@ -319,7 +319,7 @@ impl Rewrite for ast::WherePredicate {
impl Rewrite for ast::LifetimeDef { impl Rewrite for ast::LifetimeDef {
fn rewrite(&self, _: &RewriteContext, _: usize, _: usize) -> Option<String> { fn rewrite(&self, _: &RewriteContext, _: usize, _: usize) -> Option<String> {
if self.bounds.len() == 0 { if self.bounds.is_empty() {
Some(pprust::lifetime_to_string(&self.lifetime)) Some(pprust::lifetime_to_string(&self.lifetime))
} else { } else {
Some(format!("{}: {}", Some(format!("{}: {}",
@ -351,7 +351,7 @@ impl Rewrite for ast::TyParam {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> { fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
let mut result = String::with_capacity(128); let mut result = String::with_capacity(128);
result.push_str(&self.ident.to_string()); result.push_str(&self.ident.to_string());
if self.bounds.len() > 0 { if !self.bounds.is_empty() {
result.push_str(": "); result.push_str(": ");
let bounds = self.bounds.iter().map(|ty_bound| { let bounds = self.bounds.iter().map(|ty_bound| {
@ -372,7 +372,7 @@ impl Rewrite for ast::TyParam {
// FIXME: this assumes everything will fit on one line // FIXME: this assumes everything will fit on one line
impl Rewrite for ast::PolyTraitRef { impl Rewrite for ast::PolyTraitRef {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> { fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
if self.bound_lifetimes.len() > 0 { if !self.bound_lifetimes.is_empty() {
let lifetime_str = self.bound_lifetimes.iter().map(|lt| { let lifetime_str = self.bound_lifetimes.iter().map(|lt| {
lt.rewrite(context, width, offset).unwrap() lt.rewrite(context, width, offset).unwrap()
}).collect::<Vec<_>>().join(", "); }).collect::<Vec<_>>().join(", ");

View File

@ -267,7 +267,7 @@ impl<'a> FmtVisitor<'a> {
// Returns true if we should skip the following item. // Returns true if we should skip the following item.
pub fn visit_attrs(&mut self, attrs: &[ast::Attribute]) -> bool { pub fn visit_attrs(&mut self, attrs: &[ast::Attribute]) -> bool {
if attrs.len() == 0 { if attrs.is_empty() {
return false; return false;
} }
@ -299,7 +299,7 @@ impl<'a> FmtVisitor<'a> {
// with the usual doc comment token. // with the usual doc comment token.
let multi_line = a_str.starts_with("//") && comment.matches('\n').count() > 1; let multi_line = a_str.starts_with("//") && comment.matches('\n').count() > 1;
let comment = comment.trim(); let comment = comment.trim();
if comment.len() > 0 { if !comment.is_empty() {
result.push_str(&indent); result.push_str(&indent);
result.push_str(comment); result.push_str(comment);
result.push('\n'); result.push('\n');
@ -355,7 +355,7 @@ impl<'a> FmtVisitor<'a> {
}; };
// 1 = ";" // 1 = ";"
match vp.rewrite(&context, self.config.max_width - offset - 1, offset) { match vp.rewrite(&context, self.config.max_width - offset - 1, offset) {
Some(ref s) if s.len() == 0 => { Some(ref s) if s.is_empty() => {
// Format up to last newline // Format up to last newline
let prev_span = codemap::mk_sp(self.last_pos, span.lo); let prev_span = codemap::mk_sp(self.last_pos, span.lo);
let span_end = match self.snippet(prev_span).rfind('\n') { let span_end = match self.snippet(prev_span).rfind('\n') {