Skip to content

Commit 4757321

Browse files
committed
Don't index a string with chars().count()
1 parent 5516223 commit 4757321

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl Rewrite for ast::NestedMetaItem {
167167

168168
fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
169169
// Look at before and after comment and see if there are any empty lines.
170-
let comment_begin = comment.chars().position(|c| c == '/');
170+
let comment_begin = comment.find('/');
171171
let len = comment_begin.unwrap_or_else(|| comment.len());
172172
let mlb = count_newlines(&comment[..len]) > 1;
173173
let mla = if comment_begin.is_none() {

src/comment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn combine_strs_with_missing_comments(
187187
// expression and the second expression or the missing comment. We will preserve the original
188188
// layout whenever possible.
189189
let original_snippet = context.snippet(span);
190-
let prefer_same_line = if let Some(pos) = original_snippet.chars().position(|c| c == '/') {
190+
let prefer_same_line = if let Some(pos) = original_snippet.find('/') {
191191
!original_snippet[..pos].contains('\n')
192192
} else {
193193
!original_snippet.contains('\n')
@@ -523,7 +523,7 @@ pub fn recover_missing_comment_in_span(
523523
Some(String::new())
524524
} else {
525525
let missing_snippet = context.snippet(span);
526-
let pos = missing_snippet.chars().position(|c| c == '/').unwrap_or(0);
526+
let pos = missing_snippet.find('/').unwrap_or(0);
527527
// 1 = ` `
528528
let total_width = missing_comment.len() + used_width + 1;
529529
let force_new_line_before_comment =

src/issues.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,13 @@ fn find_unnumbered_issue() {
227227
let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
228228
assert_eq!(
229229
Some(failing_pos),
230-
text.chars().position(|c| seeker.inspect(c).is_some())
230+
text.find(|c| seeker.inspect(c).is_some())
231231
);
232232
}
233233

234234
fn check_pass(text: &str) {
235235
let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
236-
assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
236+
assert_eq!(None, text.find(|c| seeker.inspect(c).is_some()));
237237
}
238238

239239
check_fail("TODO\n", 4);

src/items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
977977
result.push_str(&where_clause_str);
978978
} else {
979979
let item_snippet = context.snippet(item.span);
980-
if let Some(lo) = item_snippet.chars().position(|c| c == '/') {
980+
if let Some(lo) = item_snippet.find('/') {
981981
// 1 = `{`
982982
let comment_hi = body_lo - BytePos(1);
983983
let comment_lo = item.span.lo() + BytePos(lo as u32);

src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,14 @@ impl FormattingError {
153153
match self.kind {
154154
ErrorKind::LineOverflow(found, max) => (max, found - max),
155155
ErrorKind::TrailingWhitespace => {
156-
let trailing_ws_len = self.line_buffer
157-
.chars()
158-
.rev()
159-
.take_while(|c| c.is_whitespace())
160-
.count();
161-
(self.line_buffer.len() - trailing_ws_len, trailing_ws_len)
156+
let trailing_ws_start = self.line_buffer
157+
.rfind(|c: char| !c.is_whitespace())
158+
.map(|pos| pos + 1)
159+
.unwrap_or(0);
160+
(
161+
trailing_ws_start,
162+
self.line_buffer.len() - trailing_ws_start,
163+
)
162164
}
163165
_ => unreachable!(),
164166
}

0 commit comments

Comments
 (0)