Skip to content

Fix trim_right/trim_left deprecation warnings #3252

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

Merged
merged 2 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
fn commit_info() -> String {
match (channel(), commit_hash(), commit_date()) {
(channel, Some(hash), Some(date)) => {
format!("{} ({} {})", channel, hash.trim_right(), date)
format!("{} ({} {})", channel, hash.trim_end(), date)
}
_ => String::new(),
}
Expand Down
73 changes: 39 additions & 34 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ fn identify_comment(
let mut hbl = false;

for line in orig.lines() {
let trimmed_line = line.trim_left();
let trimmed_line = line.trim_start();
if trimmed_line.is_empty() {
hbl = true;
break;
Expand All @@ -308,22 +308,22 @@ fn identify_comment(

let (has_bare_lines, first_group_ending) = match style {
CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
let line_start = style.line_start().trim_left();
let line_start = style.line_start().trim_start();
consume_same_line_comments(style, orig, line_start)
}
CommentStyle::Custom(opener) => {
let trimmed_opener = opener.trim_right();
let trimmed_opener = opener.trim_end();
consume_same_line_comments(style, orig, trimmed_opener)
}
// for a block comment, search for the closing symbol
CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
let closer = style.closer().trim_left();
let closer = style.closer().trim_start();
let mut closing_symbol_offset = 0;
let mut hbl = false;
let mut first = true;
for line in orig.lines() {
closing_symbol_offset += compute_len(&orig[closing_symbol_offset..], line);
let mut trimmed_line = line.trim_left();
let mut trimmed_line = line.trim_start();
if !trimmed_line.starts_with('*')
&& !trimmed_line.starts_with("//")
&& !trimmed_line.starts_with("/*")
Expand All @@ -333,7 +333,7 @@ fn identify_comment(

// Remove opener from consideration when searching for closer
if first {
let opener = style.opener().trim_right();
let opener = style.opener().trim_end();
trimmed_line = &trimmed_line[opener.len()..];
first = false;
}
Expand Down Expand Up @@ -367,22 +367,27 @@ fn identify_comment(
if rest.is_empty() {
Some(rewritten_first_group)
} else {
identify_comment(rest.trim_left(), block_style, shape, config, is_doc_comment).map(
|rest_str| {
format!(
"{}\n{}{}{}",
rewritten_first_group,
// insert back the blank line
if has_bare_lines && style.is_line_comment() {
"\n"
} else {
""
},
shape.indent.to_string(config),
rest_str
)
},
identify_comment(
rest.trim_start(),
block_style,
shape,
config,
is_doc_comment,
)
.map(|rest_str| {
format!(
"{}\n{}{}{}",
rewritten_first_group,
// insert back the blank line
if has_bare_lines && style.is_line_comment() {
"\n"
} else {
""
},
shape.indent.to_string(config),
rest_str
)
})
}
}

Expand Down Expand Up @@ -427,7 +432,7 @@ struct ItemizedBlock {
impl ItemizedBlock {
/// Returns true if the line is formatted as an item
fn is_itemized_line(line: &str) -> bool {
let trimmed = line.trim_left();
let trimmed = line.trim_start();
trimmed.starts_with("* ") || trimmed.starts_with("- ")
}

Expand Down Expand Up @@ -537,7 +542,7 @@ impl<'a> CommentRewrite<'a> {
while let Some(line) = iter.next() {
result.push_str(line);
result.push_str(match iter.peek() {
Some(next_line) if next_line.is_empty() => sep.trim_right(),
Some(next_line) if next_line.is_empty() => sep.trim_end(),
Some(..) => &sep,
None => "",
});
Expand Down Expand Up @@ -757,15 +762,15 @@ fn rewrite_comment_inner(
) -> Option<String> {
let mut rewriter = CommentRewrite::new(orig, block_style, shape, config);

let line_breaks = count_newlines(orig.trim_right());
let line_breaks = count_newlines(orig.trim_end());
let lines = orig
.lines()
.enumerate()
.map(|(i, mut line)| {
line = trim_right_unless_two_whitespaces(line.trim_left(), is_doc_comment);
line = trim_end_unless_two_whitespaces(line.trim_start(), is_doc_comment);
// Drop old closer.
if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
line = line[..(line.len() - 2)].trim_right();
line = line[..(line.len() - 2)].trim_end();
}

line
Expand All @@ -774,7 +779,7 @@ fn rewrite_comment_inner(
.map(|(line, has_leading_whitespace)| {
if orig.starts_with("/*") && line_breaks == 0 {
(
line.trim_left(),
line.trim_start(),
has_leading_whitespace || config.normalize_comments(),
)
} else {
Expand All @@ -794,7 +799,7 @@ fn rewrite_comment_inner(
const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";

fn hide_sharp_behind_comment(s: &str) -> Cow<str> {
if s.trim_left().starts_with("# ") {
if s.trim_start().starts_with("# ") {
Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
} else {
Cow::from(s)
Expand All @@ -804,9 +809,9 @@ fn hide_sharp_behind_comment(s: &str) -> Cow<str> {
fn trim_custom_comment_prefix(s: &str) -> String {
s.lines()
.map(|line| {
let left_trimmed = line.trim_left();
let left_trimmed = line.trim_start();
if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
left_trimmed.trim_start_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
} else {
line
}
Expand Down Expand Up @@ -866,11 +871,11 @@ pub fn recover_missing_comment_in_span(
}

/// Trim trailing whitespaces unless they consist of two or more whitespaces.
fn trim_right_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
fn trim_end_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
if is_doc_comment && s.ends_with(" ") {
s
} else {
s.trim_right()
s.trim_end()
}
}

Expand Down Expand Up @@ -898,7 +903,7 @@ fn light_rewrite_comment(
""
};
// Preserve markdown's double-space line break syntax in doc comment.
trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment)
})
.collect();
lines.join(&format!("\n{}", offset.to_string(config)))
Expand All @@ -918,7 +923,7 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> (&'a str,
if line.starts_with(opener) {
(&line[opener.len()..], true)
} else {
(&line[opener.trim_right().len()..], false)
(&line[opener.trim_end().len()..], false)
}
} else if line.starts_with("/* ")
|| line.starts_with("// ")
Expand Down
4 changes: 2 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,12 +1252,12 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
format!(
"{}{}",
new_indent.to_string(context.config),
line.trim_left()
line.trim_start()
)
})
.collect::<Vec<_>>()
.join("\n")
.trim_left(),
.trim_start(),
);
return wrap_str(indented_string_lit, context.config.max_width(), shape);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'a> FmtVisitor<'a> {
Some(ref s) if s.is_empty() => {
// Format up to last newline
let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
let trimmed_snippet = self.snippet(prev_span).trim_right();
let trimmed_snippet = self.snippet(prev_span).trim_end();
let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32);
self.format_missing(span_end);
// We have an excessive newline from the removed import.
Expand Down
8 changes: 4 additions & 4 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ pub fn format_struct_struct(
{
result.push('\n');
result.push_str(&offset.to_string(context.config));
result.push_str(generics_str.trim_left());
result.push_str(generics_str.trim_start());
} else {
result.push_str(&generics_str);
}
Expand Down Expand Up @@ -1493,7 +1493,7 @@ fn rewrite_type_item<R: Rewrite>(
result.push_str(suffix);
} else {
result.push_str(&indent.to_string_with_newline(context.config));
result.push_str(suffix.trim_left());
result.push_str(suffix.trim_start());
}

// 1 = ";"
Expand Down Expand Up @@ -1619,7 +1619,7 @@ pub fn rewrite_struct_field(
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, shape)?;
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
let field_str = if is_prefix_empty {
field_str.trim_left()
field_str.trim_start()
} else {
&field_str
};
Expand Down Expand Up @@ -1986,7 +1986,7 @@ fn rewrite_fn_base(
let snuggle_angle_bracket = generics_str
.lines()
.last()
.map_or(false, |l| l.trim_left().len() == 1);
.map_or(false, |l| l.trim_start().len() == 1);

// Note that the width and indent don't really matter, we'll re-layout the
// return type later anyway.
Expand Down
6 changes: 3 additions & 3 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ impl ListItem {
pub fn has_single_line_comment(&self) -> bool {
self.pre_comment
.as_ref()
.map_or(false, |comment| comment.trim_left().starts_with("//"))
.map_or(false, |comment| comment.trim_start().starts_with("//"))
|| self
.post_comment
.as_ref()
.map_or(false, |comment| comment.trim_left().starts_with("//"))
.map_or(false, |comment| comment.trim_start().starts_with("//"))
}

pub fn has_comment(&self) -> bool {
Expand Down Expand Up @@ -463,7 +463,7 @@ where
|| comment.trim().len() > width;

rewrite_comment(
comment.trim_left(),
comment.trim_start(),
block_style,
comment_shape,
formatting.config,
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ impl MacroBranch {

// Indent the body since it is in a block.
let indent_str = body_indent.to_string(&config);
let mut new_body = LineClasses::new(new_body.trim_right())
let mut new_body = LineClasses::new(new_body.trim_end())
.enumerate()
.fold(
(String::new(), true),
Expand Down
4 changes: 2 additions & 2 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'a> FmtVisitor<'a> {
pub fn format_missing_with_indent(&mut self, end: BytePos) {
let config = self.config;
self.format_missing_inner(end, |this, last_snippet, snippet| {
this.push_str(last_snippet.trim_right());
this.push_str(last_snippet.trim_end());
if last_snippet == snippet && !this.output_at_start() {
// No new lines in the snippet.
this.push_str("\n");
Expand All @@ -75,7 +75,7 @@ impl<'a> FmtVisitor<'a> {

pub fn format_missing_no_indent(&mut self, end: BytePos) {
self.format_missing_inner(end, |this, last_snippet, _| {
this.push_str(last_snippet.trim_right());
this.push_str(last_snippet.trim_end());
})
}

Expand Down
6 changes: 3 additions & 3 deletions src/pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ where
{
let tab_spaces = context.config.tab_spaces();
let lhs_overhead = match separator_place {
SeparatorPlace::Back => shape.used_width() + pp.prefix.len() + pp.infix.trim_right().len(),
SeparatorPlace::Back => shape.used_width() + pp.prefix.len() + pp.infix.trim_end().len(),
SeparatorPlace::Front => shape.used_width(),
};
let lhs_shape = Shape {
Expand Down Expand Up @@ -238,8 +238,8 @@ where
}
};
let infix = match separator_place {
SeparatorPlace::Back => pp.infix.trim_right(),
SeparatorPlace::Front => pp.infix.trim_left(),
SeparatorPlace::Back => pp.infix.trim_end(),
SeparatorPlace::Front => pp.infix.trim_start(),
};
if separator_place == SeparatorPlace::Front {
rhs_shape = rhs_shape.offset_left(infix.len())?;
Expand Down
12 changes: 6 additions & 6 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn rewrite_string<'a>(
for (i, grapheme) in graphemes[cur_start..].iter().enumerate() {
if is_line_feed(grapheme) {
// take care of blank lines
result = trim_right_but_line_feed(fmt.trim_end, result);
result = trim_end_but_line_feed(fmt.trim_end, result);
result.push_str("\n");
if !is_bareline_ok && cur_start + i + 1 < graphemes.len() {
result.push_str(&indent_without_newline);
Expand All @@ -117,7 +117,7 @@ pub fn rewrite_string<'a>(
result.push_str(grapheme);
}
}
result = trim_right_but_line_feed(fmt.trim_end, result);
result = trim_end_but_line_feed(fmt.trim_end, result);
break;
}

Expand All @@ -138,7 +138,7 @@ pub fn rewrite_string<'a>(
}
SnippetState::EndWithLineFeed(line, len) => {
if line == "\n" && fmt.trim_end {
result = result.trim_right().to_string();
result = result.trim_end().to_string();
}
result.push_str(&line);
if is_bareline_ok {
Expand Down Expand Up @@ -188,11 +188,11 @@ fn detect_url(s: &[&str], index: usize) -> Option<usize> {
}

/// Trims whitespaces to the right except for the line feed character.
fn trim_right_but_line_feed(trim_end: bool, result: String) -> String {
fn trim_end_but_line_feed(trim_end: bool, result: String) -> String {
let whitespace_except_line_feed = |c: char| c.is_whitespace() && c != '\n';
if trim_end && result.ends_with(whitespace_except_line_feed) {
result
.trim_right_matches(whitespace_except_line_feed)
.trim_end_matches(whitespace_except_line_feed)
.to_string()
} else {
result
Expand Down Expand Up @@ -244,7 +244,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
if i <= index_minus_ws {
let mut line = &input[0..i].concat()[..];
if trim_end {
line = line.trim_right();
line = line.trim_end();
}
return SnippetState::EndWithLineFeed(format!("{}\n", line), i + 1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ where
"{}\n{}{}",
args,
list_shape.indent.to_string(context.config),
output.trim_left()
output.trim_start()
))
}
}
Expand All @@ -422,7 +422,7 @@ impl Rewrite for ast::WherePredicate {
..
}) => {
let type_str = bounded_ty.rewrite(context, shape)?;
let colon = type_bound_colon(context).trim_right();
let colon = type_bound_colon(context).trim_end();
let lhs = if let Some(lifetime_str) =
rewrite_lifetime_param(context, shape, bound_generic_params)
{
Expand Down
Loading