Use saturating_sub in more token-walking methods (#4773)

This commit is contained in:
Charlie Marsh 2023-06-01 17:16:32 -04:00 committed by GitHub
parent 0099f9720f
commit ab26f2dc9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 49 additions and 50 deletions

View file

@ -1026,7 +1026,7 @@ pub fn match_parens(start: TextSize, locator: &Locator) -> Option<TextRange> {
let mut fix_start = None;
let mut fix_end = None;
let mut count: usize = 0;
let mut count = 0u32;
for (tok, range) in lexer::lex_starts_at(contents, Mode::Module, start).flatten() {
match tok {
@ -1034,10 +1034,10 @@ pub fn match_parens(start: TextSize, locator: &Locator) -> Option<TextRange> {
if count == 0 {
fix_start = Some(range.start());
}
count += 1;
count = count.saturating_add(1);
}
Tok::Rpar => {
count -= 1;
count = count.saturating_sub(1);
if count == 0 {
fix_end = Some(range.end());
break;
@ -1433,16 +1433,16 @@ impl LocatedCmpop {
pub fn locate_cmpops(contents: &str) -> Vec<LocatedCmpop> {
let mut tok_iter = lexer::lex(contents, Mode::Module).flatten().peekable();
let mut ops: Vec<LocatedCmpop> = vec![];
let mut count: usize = 0;
let mut count = 0u32;
loop {
let Some((tok, range)) = tok_iter.next() else {
break;
};
if matches!(tok, Tok::Lpar) {
count += 1;
count = count.saturating_add(1);
continue;
} else if matches!(tok, Tok::Rpar) {
count -= 1;
count = count.saturating_sub(1);
continue;
}
if count == 0 {