mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 10:22:24 +00:00
Use saturating_sub in more token-walking methods (#4773)
This commit is contained in:
parent
0099f9720f
commit
ab26f2dc9d
12 changed files with 49 additions and 50 deletions
|
@ -104,7 +104,7 @@ pub(crate) fn remove_argument(
|
||||||
|
|
||||||
if n_arguments == 1 {
|
if n_arguments == 1 {
|
||||||
// Case 1: there is only one argument.
|
// Case 1: there is only one argument.
|
||||||
let mut count: usize = 0;
|
let mut count = 0u32;
|
||||||
for (tok, range) in lexer::lex_starts_at(contents, Mode::Module, call_at).flatten() {
|
for (tok, range) in lexer::lex_starts_at(contents, Mode::Module, call_at).flatten() {
|
||||||
if matches!(tok, Tok::Lpar) {
|
if matches!(tok, Tok::Lpar) {
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
|
@ -114,11 +114,11 @@ pub(crate) fn remove_argument(
|
||||||
range.start() + TextSize::from(1)
|
range.start() + TextSize::from(1)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
count += 1;
|
count = count.saturating_add(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches!(tok, Tok::Rpar) {
|
if matches!(tok, Tok::Rpar) {
|
||||||
count -= 1;
|
count = count.saturating_sub(1);
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
fix_end = Some(if remove_parentheses {
|
fix_end = Some(if remove_parentheses {
|
||||||
range.end()
|
range.end()
|
||||||
|
|
|
@ -79,7 +79,7 @@ impl StateMachine {
|
||||||
}
|
}
|
||||||
|
|
||||||
Tok::Lpar | Tok::Lbrace | Tok::Lsqb => {
|
Tok::Lpar | Tok::Lbrace | Tok::Lsqb => {
|
||||||
self.bracket_count += 1;
|
self.bracket_count = self.bracket_count.saturating_add(1);
|
||||||
if matches!(
|
if matches!(
|
||||||
self.state,
|
self.state,
|
||||||
State::ExpectModuleDocstring
|
State::ExpectModuleDocstring
|
||||||
|
@ -92,7 +92,7 @@ impl StateMachine {
|
||||||
}
|
}
|
||||||
|
|
||||||
Tok::Rpar | Tok::Rbrace | Tok::Rsqb => {
|
Tok::Rpar | Tok::Rbrace | Tok::Rsqb => {
|
||||||
self.bracket_count -= 1;
|
self.bracket_count = self.bracket_count.saturating_sub(1);
|
||||||
if matches!(
|
if matches!(
|
||||||
self.state,
|
self.state,
|
||||||
State::ExpectModuleDocstring
|
State::ExpectModuleDocstring
|
||||||
|
|
|
@ -16,7 +16,7 @@ pub(crate) fn add_return_annotation(
|
||||||
// Find the colon (following the `def` keyword).
|
// Find the colon (following the `def` keyword).
|
||||||
let mut seen_lpar = false;
|
let mut seen_lpar = false;
|
||||||
let mut seen_rpar = false;
|
let mut seen_rpar = false;
|
||||||
let mut count: usize = 0;
|
let mut count = 0u32;
|
||||||
for (tok, range) in lexer::lex_starts_at(contents, Mode::Module, stmt.start()).flatten() {
|
for (tok, range) in lexer::lex_starts_at(contents, Mode::Module, stmt.start()).flatten() {
|
||||||
if seen_lpar && seen_rpar {
|
if seen_lpar && seen_rpar {
|
||||||
if matches!(tok, Tok::Colon) {
|
if matches!(tok, Tok::Colon) {
|
||||||
|
@ -28,10 +28,10 @@ pub(crate) fn add_return_annotation(
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
seen_lpar = true;
|
seen_lpar = true;
|
||||||
}
|
}
|
||||||
count += 1;
|
count = count.saturating_add(1);
|
||||||
}
|
}
|
||||||
if matches!(tok, Tok::Rpar) {
|
if matches!(tok, Tok::Rpar) {
|
||||||
count -= 1;
|
count = count.saturating_sub(1);
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
seen_rpar = true;
|
seen_rpar = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,14 +10,14 @@ use crate::rules::isort::types::TrailingComma;
|
||||||
/// trailing comma.
|
/// trailing comma.
|
||||||
pub(crate) fn trailing_comma(stmt: &Stmt, locator: &Locator) -> TrailingComma {
|
pub(crate) fn trailing_comma(stmt: &Stmt, locator: &Locator) -> TrailingComma {
|
||||||
let contents = locator.slice(stmt.range());
|
let contents = locator.slice(stmt.range());
|
||||||
let mut count: usize = 0;
|
let mut count = 0u32;
|
||||||
let mut trailing_comma = TrailingComma::Absent;
|
let mut trailing_comma = TrailingComma::Absent;
|
||||||
for (tok, _) in lexer::lex_starts_at(contents, Mode::Module, stmt.start()).flatten() {
|
for (tok, _) in lexer::lex_starts_at(contents, Mode::Module, stmt.start()).flatten() {
|
||||||
if matches!(tok, Tok::Lpar) {
|
if matches!(tok, Tok::Lpar) {
|
||||||
count += 1;
|
count = count.saturating_add(1);
|
||||||
}
|
}
|
||||||
if matches!(tok, Tok::Rpar) {
|
if matches!(tok, Tok::Rpar) {
|
||||||
count -= 1;
|
count = count.saturating_sub(1);
|
||||||
}
|
}
|
||||||
if count == 1 {
|
if count == 1 {
|
||||||
if matches!(
|
if matches!(
|
||||||
|
|
|
@ -123,29 +123,29 @@ pub(crate) fn compound_statements(lxr: &[LexResult], settings: &Settings) -> Vec
|
||||||
let mut allow_ellipsis = false;
|
let mut allow_ellipsis = false;
|
||||||
|
|
||||||
// Track the bracket depth.
|
// Track the bracket depth.
|
||||||
let mut par_count = 0;
|
let mut par_count = 0u32;
|
||||||
let mut sqb_count = 0;
|
let mut sqb_count = 0u32;
|
||||||
let mut brace_count = 0;
|
let mut brace_count = 0u32;
|
||||||
|
|
||||||
for &(ref tok, range) in lxr.iter().flatten() {
|
for &(ref tok, range) in lxr.iter().flatten() {
|
||||||
match tok {
|
match tok {
|
||||||
Tok::Lpar => {
|
Tok::Lpar => {
|
||||||
par_count += 1;
|
par_count = par_count.saturating_add(1);
|
||||||
}
|
}
|
||||||
Tok::Rpar => {
|
Tok::Rpar => {
|
||||||
par_count -= 1;
|
par_count = par_count.saturating_sub(1);
|
||||||
}
|
}
|
||||||
Tok::Lsqb => {
|
Tok::Lsqb => {
|
||||||
sqb_count += 1;
|
sqb_count = sqb_count.saturating_add(1);
|
||||||
}
|
}
|
||||||
Tok::Rsqb => {
|
Tok::Rsqb => {
|
||||||
sqb_count -= 1;
|
sqb_count = sqb_count.saturating_sub(1);
|
||||||
}
|
}
|
||||||
Tok::Lbrace => {
|
Tok::Lbrace => {
|
||||||
brace_count += 1;
|
brace_count = brace_count.saturating_add(1);
|
||||||
}
|
}
|
||||||
Tok::Rbrace => {
|
Tok::Rbrace => {
|
||||||
brace_count -= 1;
|
brace_count = brace_count.saturating_sub(1);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,11 +53,11 @@ pub(crate) fn missing_whitespace(
|
||||||
let kind = token.kind();
|
let kind = token.kind();
|
||||||
match kind {
|
match kind {
|
||||||
TokenKind::Lsqb => {
|
TokenKind::Lsqb => {
|
||||||
open_parentheses += 1;
|
open_parentheses = open_parentheses.saturating_add(1);
|
||||||
prev_lsqb = token.start();
|
prev_lsqb = token.start();
|
||||||
}
|
}
|
||||||
TokenKind::Rsqb => {
|
TokenKind::Rsqb => {
|
||||||
open_parentheses -= 1;
|
open_parentheses = open_parentheses.saturating_sub(1);
|
||||||
}
|
}
|
||||||
TokenKind::Lbrace => {
|
TokenKind::Lbrace => {
|
||||||
prev_lbrace = token.start();
|
prev_lbrace = token.start();
|
||||||
|
|
|
@ -87,7 +87,7 @@ impl<'a> LogicalLines<'a> {
|
||||||
assert!(u32::try_from(tokens.len()).is_ok());
|
assert!(u32::try_from(tokens.len()).is_ok());
|
||||||
|
|
||||||
let mut builder = LogicalLinesBuilder::with_capacity(tokens.len());
|
let mut builder = LogicalLinesBuilder::with_capacity(tokens.len());
|
||||||
let mut parens: u32 = 0;
|
let mut parens = 0u32;
|
||||||
|
|
||||||
for (token, range) in tokens.iter().flatten() {
|
for (token, range) in tokens.iter().flatten() {
|
||||||
let token_kind = TokenKind::from_token(token);
|
let token_kind = TokenKind::from_token(token);
|
||||||
|
@ -95,10 +95,10 @@ impl<'a> LogicalLines<'a> {
|
||||||
|
|
||||||
match token_kind {
|
match token_kind {
|
||||||
TokenKind::Lbrace | TokenKind::Lpar | TokenKind::Lsqb => {
|
TokenKind::Lbrace | TokenKind::Lpar | TokenKind::Lsqb => {
|
||||||
parens += 1;
|
parens = parens.saturating_add(1);
|
||||||
}
|
}
|
||||||
TokenKind::Rbrace | TokenKind::Rpar | TokenKind::Rsqb => {
|
TokenKind::Rbrace | TokenKind::Rpar | TokenKind::Rsqb => {
|
||||||
parens -= 1;
|
parens = parens.saturating_sub(1);
|
||||||
}
|
}
|
||||||
TokenKind::Newline | TokenKind::NonLogicalNewline if parens == 0 => {
|
TokenKind::Newline | TokenKind::NonLogicalNewline if parens == 0 => {
|
||||||
builder.finish_line();
|
builder.finish_line();
|
||||||
|
|
|
@ -60,11 +60,10 @@ pub(crate) fn whitespace_around_named_parameter_equals(
|
||||||
|
|
||||||
match kind {
|
match kind {
|
||||||
TokenKind::Lpar | TokenKind::Lsqb => {
|
TokenKind::Lpar | TokenKind::Lsqb => {
|
||||||
parens += 1;
|
parens = parens.saturating_add(1);
|
||||||
}
|
}
|
||||||
TokenKind::Rpar | TokenKind::Rsqb => {
|
TokenKind::Rpar | TokenKind::Rsqb => {
|
||||||
parens -= 1;
|
parens = parens.saturating_sub(1);
|
||||||
|
|
||||||
if parens == 0 {
|
if parens == 0 {
|
||||||
annotated_func_arg = false;
|
annotated_func_arg = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,9 +73,9 @@ where
|
||||||
let contents = locator.after(located.start());
|
let contents = locator.after(located.start());
|
||||||
|
|
||||||
// Track the bracket depth.
|
// Track the bracket depth.
|
||||||
let mut par_count = 0;
|
let mut par_count = 0u32;
|
||||||
let mut sqb_count = 0;
|
let mut sqb_count = 0u32;
|
||||||
let mut brace_count = 0;
|
let mut brace_count = 0u32;
|
||||||
|
|
||||||
for ((tok, _), (_, range)) in lexer::lex_starts_at(contents, Mode::Module, located.start())
|
for ((tok, _), (_, range)) in lexer::lex_starts_at(contents, Mode::Module, located.start())
|
||||||
.flatten()
|
.flatten()
|
||||||
|
@ -83,30 +83,30 @@ where
|
||||||
{
|
{
|
||||||
match tok {
|
match tok {
|
||||||
Tok::Lpar => {
|
Tok::Lpar => {
|
||||||
par_count += 1;
|
par_count = par_count.saturating_add(1);
|
||||||
}
|
}
|
||||||
Tok::Lsqb => {
|
Tok::Lsqb => {
|
||||||
sqb_count += 1;
|
sqb_count = sqb_count.saturating_add(1);
|
||||||
}
|
}
|
||||||
Tok::Lbrace => {
|
Tok::Lbrace => {
|
||||||
brace_count += 1;
|
brace_count = brace_count.saturating_add(1);
|
||||||
}
|
}
|
||||||
Tok::Rpar => {
|
Tok::Rpar => {
|
||||||
par_count -= 1;
|
par_count = par_count.saturating_sub(1);
|
||||||
// If this is a closing bracket, continue.
|
// If this is a closing bracket, continue.
|
||||||
if par_count == 0 {
|
if par_count == 0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Tok::Rsqb => {
|
Tok::Rsqb => {
|
||||||
sqb_count -= 1;
|
sqb_count = sqb_count.saturating_sub(1);
|
||||||
// If this is a closing bracket, continue.
|
// If this is a closing bracket, continue.
|
||||||
if sqb_count == 0 {
|
if sqb_count == 0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Tok::Rbrace => {
|
Tok::Rbrace => {
|
||||||
brace_count -= 1;
|
brace_count = brace_count.saturating_sub(1);
|
||||||
// If this is a closing bracket, continue.
|
// If this is a closing bracket, continue.
|
||||||
if brace_count == 0 {
|
if brace_count == 0 {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -51,7 +51,7 @@ fn match_extraneous_parentheses(tokens: &[LexResult], mut i: usize) -> Option<(u
|
||||||
let start = i;
|
let start = i;
|
||||||
|
|
||||||
// Verify that we're not in a tuple or coroutine.
|
// Verify that we're not in a tuple or coroutine.
|
||||||
let mut depth = 1;
|
let mut depth = 1u32;
|
||||||
while depth > 0 {
|
while depth > 0 {
|
||||||
i += 1;
|
i += 1;
|
||||||
if i >= tokens.len() {
|
if i >= tokens.len() {
|
||||||
|
@ -65,9 +65,9 @@ fn match_extraneous_parentheses(tokens: &[LexResult], mut i: usize) -> Option<(u
|
||||||
if depth == 1 && matches!(tok, Tok::Comma | Tok::Yield) {
|
if depth == 1 && matches!(tok, Tok::Comma | Tok::Yield) {
|
||||||
return None;
|
return None;
|
||||||
} else if matches!(tok, Tok::Lpar | Tok::Lbrace | Tok::Lsqb) {
|
} else if matches!(tok, Tok::Lpar | Tok::Lbrace | Tok::Lsqb) {
|
||||||
depth += 1;
|
depth = depth.saturating_add(1);
|
||||||
} else if matches!(tok, Tok::Rpar | Tok::Rbrace | Tok::Rsqb) {
|
} else if matches!(tok, Tok::Rpar | Tok::Rbrace | Tok::Rsqb) {
|
||||||
depth -= 1;
|
depth = depth.saturating_sub(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1026,7 +1026,7 @@ pub fn match_parens(start: TextSize, locator: &Locator) -> Option<TextRange> {
|
||||||
|
|
||||||
let mut fix_start = None;
|
let mut fix_start = None;
|
||||||
let mut fix_end = 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() {
|
for (tok, range) in lexer::lex_starts_at(contents, Mode::Module, start).flatten() {
|
||||||
match tok {
|
match tok {
|
||||||
|
@ -1034,10 +1034,10 @@ pub fn match_parens(start: TextSize, locator: &Locator) -> Option<TextRange> {
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
fix_start = Some(range.start());
|
fix_start = Some(range.start());
|
||||||
}
|
}
|
||||||
count += 1;
|
count = count.saturating_add(1);
|
||||||
}
|
}
|
||||||
Tok::Rpar => {
|
Tok::Rpar => {
|
||||||
count -= 1;
|
count = count.saturating_sub(1);
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
fix_end = Some(range.end());
|
fix_end = Some(range.end());
|
||||||
break;
|
break;
|
||||||
|
@ -1433,16 +1433,16 @@ impl LocatedCmpop {
|
||||||
pub fn locate_cmpops(contents: &str) -> Vec<LocatedCmpop> {
|
pub fn locate_cmpops(contents: &str) -> Vec<LocatedCmpop> {
|
||||||
let mut tok_iter = lexer::lex(contents, Mode::Module).flatten().peekable();
|
let mut tok_iter = lexer::lex(contents, Mode::Module).flatten().peekable();
|
||||||
let mut ops: Vec<LocatedCmpop> = vec![];
|
let mut ops: Vec<LocatedCmpop> = vec![];
|
||||||
let mut count: usize = 0;
|
let mut count = 0u32;
|
||||||
loop {
|
loop {
|
||||||
let Some((tok, range)) = tok_iter.next() else {
|
let Some((tok, range)) = tok_iter.next() else {
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
if matches!(tok, Tok::Lpar) {
|
if matches!(tok, Tok::Lpar) {
|
||||||
count += 1;
|
count = count.saturating_add(1);
|
||||||
continue;
|
continue;
|
||||||
} else if matches!(tok, Tok::Rpar) {
|
} else if matches!(tok, Tok::Rpar) {
|
||||||
count -= 1;
|
count = count.saturating_sub(1);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
|
|
|
@ -132,11 +132,11 @@ impl<'a> Generator<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn body<U>(&mut self, stmts: &[Stmt<U>]) {
|
fn body<U>(&mut self, stmts: &[Stmt<U>]) {
|
||||||
self.indent_depth += 1;
|
self.indent_depth = self.indent_depth.saturating_add(1);
|
||||||
for stmt in stmts {
|
for stmt in stmts {
|
||||||
self.unparse_stmt(stmt);
|
self.unparse_stmt(stmt);
|
||||||
}
|
}
|
||||||
self.indent_depth -= 1;
|
self.indent_depth = self.indent_depth.saturating_sub(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn p(&mut self, s: &str) {
|
fn p(&mut self, s: &str) {
|
||||||
|
@ -531,11 +531,11 @@ impl<'a> Generator<'a> {
|
||||||
self.p(":");
|
self.p(":");
|
||||||
});
|
});
|
||||||
for case in cases {
|
for case in cases {
|
||||||
self.indent_depth += 1;
|
self.indent_depth = self.indent_depth.saturating_add(1);
|
||||||
statement!({
|
statement!({
|
||||||
self.unparse_match_case(case);
|
self.unparse_match_case(case);
|
||||||
});
|
});
|
||||||
self.indent_depth -= 1;
|
self.indent_depth = self.indent_depth.saturating_sub(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::Raise(ast::StmtRaise {
|
Stmt::Raise(ast::StmtRaise {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue