Skip to content

Commit d078595

Browse files
phanschtopecongiro
authored andcommitted
Fix trim_right/trim_left deprecation warnings (rust-lang#3252)
1 parent 16c292d commit d078595

13 files changed

+68
-63
lines changed

build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() {
3636
fn commit_info() -> String {
3737
match (channel(), commit_hash(), commit_date()) {
3838
(channel, Some(hash), Some(date)) => {
39-
format!("{} ({} {})", channel, hash.trim_right(), date)
39+
format!("{} ({} {})", channel, hash.trim_end(), date)
4040
}
4141
_ => String::new(),
4242
}

src/comment.rs

+39-34
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ fn identify_comment(
291291
let mut hbl = false;
292292

293293
for line in orig.lines() {
294-
let trimmed_line = line.trim_left();
294+
let trimmed_line = line.trim_start();
295295
if trimmed_line.is_empty() {
296296
hbl = true;
297297
break;
@@ -308,22 +308,22 @@ fn identify_comment(
308308

309309
let (has_bare_lines, first_group_ending) = match style {
310310
CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
311-
let line_start = style.line_start().trim_left();
311+
let line_start = style.line_start().trim_start();
312312
consume_same_line_comments(style, orig, line_start)
313313
}
314314
CommentStyle::Custom(opener) => {
315-
let trimmed_opener = opener.trim_right();
315+
let trimmed_opener = opener.trim_end();
316316
consume_same_line_comments(style, orig, trimmed_opener)
317317
}
318318
// for a block comment, search for the closing symbol
319319
CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
320-
let closer = style.closer().trim_left();
320+
let closer = style.closer().trim_start();
321321
let mut closing_symbol_offset = 0;
322322
let mut hbl = false;
323323
let mut first = true;
324324
for line in orig.lines() {
325325
closing_symbol_offset += compute_len(&orig[closing_symbol_offset..], line);
326-
let mut trimmed_line = line.trim_left();
326+
let mut trimmed_line = line.trim_start();
327327
if !trimmed_line.starts_with('*')
328328
&& !trimmed_line.starts_with("//")
329329
&& !trimmed_line.starts_with("/*")
@@ -333,7 +333,7 @@ fn identify_comment(
333333

334334
// Remove opener from consideration when searching for closer
335335
if first {
336-
let opener = style.opener().trim_right();
336+
let opener = style.opener().trim_end();
337337
trimmed_line = &trimmed_line[opener.len()..];
338338
first = false;
339339
}
@@ -367,22 +367,27 @@ fn identify_comment(
367367
if rest.is_empty() {
368368
Some(rewritten_first_group)
369369
} else {
370-
identify_comment(rest.trim_left(), block_style, shape, config, is_doc_comment).map(
371-
|rest_str| {
372-
format!(
373-
"{}\n{}{}{}",
374-
rewritten_first_group,
375-
// insert back the blank line
376-
if has_bare_lines && style.is_line_comment() {
377-
"\n"
378-
} else {
379-
""
380-
},
381-
shape.indent.to_string(config),
382-
rest_str
383-
)
384-
},
370+
identify_comment(
371+
rest.trim_start(),
372+
block_style,
373+
shape,
374+
config,
375+
is_doc_comment,
385376
)
377+
.map(|rest_str| {
378+
format!(
379+
"{}\n{}{}{}",
380+
rewritten_first_group,
381+
// insert back the blank line
382+
if has_bare_lines && style.is_line_comment() {
383+
"\n"
384+
} else {
385+
""
386+
},
387+
shape.indent.to_string(config),
388+
rest_str
389+
)
390+
})
386391
}
387392
}
388393

@@ -427,7 +432,7 @@ struct ItemizedBlock {
427432
impl ItemizedBlock {
428433
/// Returns true if the line is formatted as an item
429434
fn is_itemized_line(line: &str) -> bool {
430-
let trimmed = line.trim_left();
435+
let trimmed = line.trim_start();
431436
trimmed.starts_with("* ") || trimmed.starts_with("- ")
432437
}
433438

@@ -537,7 +542,7 @@ impl<'a> CommentRewrite<'a> {
537542
while let Some(line) = iter.next() {
538543
result.push_str(line);
539544
result.push_str(match iter.peek() {
540-
Some(next_line) if next_line.is_empty() => sep.trim_right(),
545+
Some(next_line) if next_line.is_empty() => sep.trim_end(),
541546
Some(..) => &sep,
542547
None => "",
543548
});
@@ -757,15 +762,15 @@ fn rewrite_comment_inner(
757762
) -> Option<String> {
758763
let mut rewriter = CommentRewrite::new(orig, block_style, shape, config);
759764

760-
let line_breaks = count_newlines(orig.trim_right());
765+
let line_breaks = count_newlines(orig.trim_end());
761766
let lines = orig
762767
.lines()
763768
.enumerate()
764769
.map(|(i, mut line)| {
765-
line = trim_right_unless_two_whitespaces(line.trim_left(), is_doc_comment);
770+
line = trim_end_unless_two_whitespaces(line.trim_start(), is_doc_comment);
766771
// Drop old closer.
767772
if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
768-
line = line[..(line.len() - 2)].trim_right();
773+
line = line[..(line.len() - 2)].trim_end();
769774
}
770775

771776
line
@@ -774,7 +779,7 @@ fn rewrite_comment_inner(
774779
.map(|(line, has_leading_whitespace)| {
775780
if orig.starts_with("/*") && line_breaks == 0 {
776781
(
777-
line.trim_left(),
782+
line.trim_start(),
778783
has_leading_whitespace || config.normalize_comments(),
779784
)
780785
} else {
@@ -794,7 +799,7 @@ fn rewrite_comment_inner(
794799
const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
795800

796801
fn hide_sharp_behind_comment(s: &str) -> Cow<str> {
797-
if s.trim_left().starts_with("# ") {
802+
if s.trim_start().starts_with("# ") {
798803
Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
799804
} else {
800805
Cow::from(s)
@@ -804,9 +809,9 @@ fn hide_sharp_behind_comment(s: &str) -> Cow<str> {
804809
fn trim_custom_comment_prefix(s: &str) -> String {
805810
s.lines()
806811
.map(|line| {
807-
let left_trimmed = line.trim_left();
812+
let left_trimmed = line.trim_start();
808813
if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
809-
left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
814+
left_trimmed.trim_start_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
810815
} else {
811816
line
812817
}
@@ -866,11 +871,11 @@ pub fn recover_missing_comment_in_span(
866871
}
867872

868873
/// Trim trailing whitespaces unless they consist of two or more whitespaces.
869-
fn trim_right_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
874+
fn trim_end_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
870875
if is_doc_comment && s.ends_with(" ") {
871876
s
872877
} else {
873-
s.trim_right()
878+
s.trim_end()
874879
}
875880
}
876881

@@ -898,7 +903,7 @@ fn light_rewrite_comment(
898903
""
899904
};
900905
// Preserve markdown's double-space line break syntax in doc comment.
901-
trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
906+
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment)
902907
})
903908
.collect();
904909
lines.join(&format!("\n{}", offset.to_string(config)))
@@ -918,7 +923,7 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> (&'a str,
918923
if line.starts_with(opener) {
919924
(&line[opener.len()..], true)
920925
} else {
921-
(&line[opener.trim_right().len()..], false)
926+
(&line[opener.trim_end().len()..], false)
922927
}
923928
} else if line.starts_with("/* ")
924929
|| line.starts_with("// ")

src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1252,12 +1252,12 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
12521252
format!(
12531253
"{}{}",
12541254
new_indent.to_string(context.config),
1255-
line.trim_left()
1255+
line.trim_start()
12561256
)
12571257
})
12581258
.collect::<Vec<_>>()
12591259
.join("\n")
1260-
.trim_left(),
1260+
.trim_start(),
12611261
);
12621262
return wrap_str(indented_string_lit, context.config.max_width(), shape);
12631263
} else {

src/imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'a> FmtVisitor<'a> {
5050
Some(ref s) if s.is_empty() => {
5151
// Format up to last newline
5252
let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
53-
let trimmed_snippet = self.snippet(prev_span).trim_right();
53+
let trimmed_snippet = self.snippet(prev_span).trim_end();
5454
let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32);
5555
self.format_missing(span_end);
5656
// We have an excessive newline from the removed import.

src/items.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ pub fn format_struct_struct(
12491249
{
12501250
result.push('\n');
12511251
result.push_str(&offset.to_string(context.config));
1252-
result.push_str(generics_str.trim_left());
1252+
result.push_str(generics_str.trim_start());
12531253
} else {
12541254
result.push_str(&generics_str);
12551255
}
@@ -1493,7 +1493,7 @@ fn rewrite_type_item<R: Rewrite>(
14931493
result.push_str(suffix);
14941494
} else {
14951495
result.push_str(&indent.to_string_with_newline(context.config));
1496-
result.push_str(suffix.trim_left());
1496+
result.push_str(suffix.trim_start());
14971497
}
14981498

14991499
// 1 = ";"
@@ -1619,7 +1619,7 @@ pub fn rewrite_struct_field(
16191619
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, shape)?;
16201620
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
16211621
let field_str = if is_prefix_empty {
1622-
field_str.trim_left()
1622+
field_str.trim_start()
16231623
} else {
16241624
&field_str
16251625
};
@@ -1986,7 +1986,7 @@ fn rewrite_fn_base(
19861986
let snuggle_angle_bracket = generics_str
19871987
.lines()
19881988
.last()
1989-
.map_or(false, |l| l.trim_left().len() == 1);
1989+
.map_or(false, |l| l.trim_start().len() == 1);
19901990

19911991
// Note that the width and indent don't really matter, we'll re-layout the
19921992
// return type later anyway.

src/lists.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ impl ListItem {
177177
pub fn has_single_line_comment(&self) -> bool {
178178
self.pre_comment
179179
.as_ref()
180-
.map_or(false, |comment| comment.trim_left().starts_with("//"))
180+
.map_or(false, |comment| comment.trim_start().starts_with("//"))
181181
|| self
182182
.post_comment
183183
.as_ref()
184-
.map_or(false, |comment| comment.trim_left().starts_with("//"))
184+
.map_or(false, |comment| comment.trim_start().starts_with("//"))
185185
}
186186

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

465465
rewrite_comment(
466-
comment.trim_left(),
466+
comment.trim_start(),
467467
block_style,
468468
comment_shape,
469469
formatting.config,

src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ impl MacroBranch {
12901290

12911291
// Indent the body since it is in a block.
12921292
let indent_str = body_indent.to_string(&config);
1293-
let mut new_body = LineClasses::new(new_body.trim_right())
1293+
let mut new_body = LineClasses::new(new_body.trim_end())
12941294
.enumerate()
12951295
.fold(
12961296
(String::new(), true),

src/missed_spans.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'a> FmtVisitor<'a> {
6363
pub fn format_missing_with_indent(&mut self, end: BytePos) {
6464
let config = self.config;
6565
self.format_missing_inner(end, |this, last_snippet, snippet| {
66-
this.push_str(last_snippet.trim_right());
66+
this.push_str(last_snippet.trim_end());
6767
if last_snippet == snippet && !this.output_at_start() {
6868
// No new lines in the snippet.
6969
this.push_str("\n");
@@ -75,7 +75,7 @@ impl<'a> FmtVisitor<'a> {
7575

7676
pub fn format_missing_no_indent(&mut self, end: BytePos) {
7777
self.format_missing_inner(end, |this, last_snippet, _| {
78-
this.push_str(last_snippet.trim_right());
78+
this.push_str(last_snippet.trim_end());
7979
})
8080
}
8181

src/pairs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ where
184184
{
185185
let tab_spaces = context.config.tab_spaces();
186186
let lhs_overhead = match separator_place {
187-
SeparatorPlace::Back => shape.used_width() + pp.prefix.len() + pp.infix.trim_right().len(),
187+
SeparatorPlace::Back => shape.used_width() + pp.prefix.len() + pp.infix.trim_end().len(),
188188
SeparatorPlace::Front => shape.used_width(),
189189
};
190190
let lhs_shape = Shape {
@@ -238,8 +238,8 @@ where
238238
}
239239
};
240240
let infix = match separator_place {
241-
SeparatorPlace::Back => pp.infix.trim_right(),
242-
SeparatorPlace::Front => pp.infix.trim_left(),
241+
SeparatorPlace::Back => pp.infix.trim_end(),
242+
SeparatorPlace::Front => pp.infix.trim_start(),
243243
};
244244
if separator_place == SeparatorPlace::Front {
245245
rhs_shape = rhs_shape.offset_left(infix.len())?;

src/string.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn rewrite_string<'a>(
107107
for (i, grapheme) in graphemes[cur_start..].iter().enumerate() {
108108
if is_line_feed(grapheme) {
109109
// take care of blank lines
110-
result = trim_right_but_line_feed(fmt.trim_end, result);
110+
result = trim_end_but_line_feed(fmt.trim_end, result);
111111
result.push_str("\n");
112112
if !is_bareline_ok && cur_start + i + 1 < graphemes.len() {
113113
result.push_str(&indent_without_newline);
@@ -117,7 +117,7 @@ pub fn rewrite_string<'a>(
117117
result.push_str(grapheme);
118118
}
119119
}
120-
result = trim_right_but_line_feed(fmt.trim_end, result);
120+
result = trim_end_but_line_feed(fmt.trim_end, result);
121121
break;
122122
}
123123

@@ -138,7 +138,7 @@ pub fn rewrite_string<'a>(
138138
}
139139
SnippetState::EndWithLineFeed(line, len) => {
140140
if line == "\n" && fmt.trim_end {
141-
result = result.trim_right().to_string();
141+
result = result.trim_end().to_string();
142142
}
143143
result.push_str(&line);
144144
if is_bareline_ok {
@@ -188,11 +188,11 @@ fn detect_url(s: &[&str], index: usize) -> Option<usize> {
188188
}
189189

190190
/// Trims whitespaces to the right except for the line feed character.
191-
fn trim_right_but_line_feed(trim_end: bool, result: String) -> String {
191+
fn trim_end_but_line_feed(trim_end: bool, result: String) -> String {
192192
let whitespace_except_line_feed = |c: char| c.is_whitespace() && c != '\n';
193193
if trim_end && result.ends_with(whitespace_except_line_feed) {
194194
result
195-
.trim_right_matches(whitespace_except_line_feed)
195+
.trim_end_matches(whitespace_except_line_feed)
196196
.to_string()
197197
} else {
198198
result
@@ -244,7 +244,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
244244
if i <= index_minus_ws {
245245
let mut line = &input[0..i].concat()[..];
246246
if trim_end {
247-
line = line.trim_right();
247+
line = line.trim_end();
248248
}
249249
return SnippetState::EndWithLineFeed(format!("{}\n", line), i + 1);
250250
}

src/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ where
399399
"{}\n{}{}",
400400
args,
401401
list_shape.indent.to_string(context.config),
402-
output.trim_left()
402+
output.trim_start()
403403
))
404404
}
405405
}
@@ -422,7 +422,7 @@ impl Rewrite for ast::WherePredicate {
422422
..
423423
}) => {
424424
let type_str = bounded_ty.rewrite(context, shape)?;
425-
let colon = type_bound_colon(context).trim_right();
425+
let colon = type_bound_colon(context).trim_end();
426426
let lhs = if let Some(lifetime_str) =
427427
rewrite_lifetime_param(context, shape, bound_generic_params)
428428
{

0 commit comments

Comments
 (0)