Skip to content
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

Skip all bracketed expressions when locating comparison ops #7740

Merged
merged 1 commit into from
Oct 1, 2023
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
4 changes: 4 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyflakes/F632.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@

{2 is
not ''}

# Regression test for
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for ?

if values[1is not None ] is not '-':
pass
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ F632.py:26:2: F632 [*] Use `!=` to compare constant literals
| __^
27 | | not ''}
| |_______^ F632
28 |
29 | # Regression test for
|
= help: Replace `is not` with `!=`

Expand All @@ -181,5 +183,42 @@ F632.py:26:2: F632 [*] Use `!=` to compare constant literals
26 |-{2 is
27 |- not ''}
26 |+{2 != ''}
28 27 |
29 28 | # Regression test for
30 29 | if values[1is not None ] is not '-':

F632.py:30:4: F632 [*] Use `!=` to compare constant literals
|
29 | # Regression test for
30 | if values[1is not None ] is not '-':
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ F632
31 | pass
|
= help: Replace `is not` with `!=`

ℹ Fix
27 27 | not ''}
28 28 |
29 29 | # Regression test for
30 |-if values[1is not None ] is not '-':
30 |+if values[1is not None ] != '-':
31 31 | pass

F632.py:30:11: F632 [*] Use `!=` to compare constant literals
|
29 | # Regression test for
30 | if values[1is not None ] is not '-':
| ^^^^^^^^^^^^ F632
31 | pass
|
= help: Replace `is not` with `!=`

ℹ Fix
27 27 | not ''}
28 28 |
29 29 | # Regression test for
30 |-if values[1is not None ] is not '-':
30 |+if values[1!= None ] is not '-':
31 31 | pass


123 changes: 71 additions & 52 deletions crates/ruff_python_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,66 +180,85 @@ pub fn locate_cmp_ops(expr: &Expr, source: &str) -> Vec<LocatedCmpOp> {
.peekable();

let mut ops: Vec<LocatedCmpOp> = vec![];
let mut count = 0u32;

// Track the bracket depth.
let mut par_count = 0u32;
let mut sqb_count = 0u32;
let mut brace_count = 0u32;

loop {
let Some((tok, range)) = tok_iter.next() else {
break;
};
if matches!(tok, Tok::Lpar) {
count = count.saturating_add(1);
continue;
} else if matches!(tok, Tok::Rpar) {
count = count.saturating_sub(1);

match tok {
Tok::Lpar => {
par_count = par_count.saturating_add(1);
}
Tok::Rpar => {
par_count = par_count.saturating_sub(1);
}
Tok::Lsqb => {
sqb_count = sqb_count.saturating_add(1);
}
Tok::Rsqb => {
sqb_count = sqb_count.saturating_sub(1);
}
Tok::Lbrace => {
brace_count = brace_count.saturating_add(1);
}
Tok::Rbrace => {
brace_count = brace_count.saturating_sub(1);
}
_ => {}
}

if par_count > 0 || sqb_count > 0 || brace_count > 0 {
continue;
}
if count == 0 {
match tok {
Tok::Not => {
if let Some((_, next_range)) =
tok_iter.next_if(|(tok, _)| matches!(tok, Tok::In))
{
ops.push(LocatedCmpOp::new(
TextRange::new(range.start(), next_range.end()),
CmpOp::NotIn,
));
}
}
Tok::In => {
ops.push(LocatedCmpOp::new(range, CmpOp::In));
}
Tok::Is => {
let op = if let Some((_, next_range)) =
tok_iter.next_if(|(tok, _)| matches!(tok, Tok::Not))
{
LocatedCmpOp::new(
TextRange::new(range.start(), next_range.end()),
CmpOp::IsNot,
)
} else {
LocatedCmpOp::new(range, CmpOp::Is)
};
ops.push(op);
}
Tok::NotEqual => {
ops.push(LocatedCmpOp::new(range, CmpOp::NotEq));
}
Tok::EqEqual => {
ops.push(LocatedCmpOp::new(range, CmpOp::Eq));
}
Tok::GreaterEqual => {
ops.push(LocatedCmpOp::new(range, CmpOp::GtE));
}
Tok::Greater => {
ops.push(LocatedCmpOp::new(range, CmpOp::Gt));
}
Tok::LessEqual => {
ops.push(LocatedCmpOp::new(range, CmpOp::LtE));
}
Tok::Less => {
ops.push(LocatedCmpOp::new(range, CmpOp::Lt));

match tok {
Tok::Not => {
if let Some((_, next_range)) = tok_iter.next_if(|(tok, _)| tok.is_in()) {
ops.push(LocatedCmpOp::new(
TextRange::new(range.start(), next_range.end()),
CmpOp::NotIn,
));
}
_ => {}
}
Tok::In => {
ops.push(LocatedCmpOp::new(range, CmpOp::In));
}
Tok::Is => {
let op = if let Some((_, next_range)) = tok_iter.next_if(|(tok, _)| tok.is_not()) {
LocatedCmpOp::new(
TextRange::new(range.start(), next_range.end()),
CmpOp::IsNot,
)
} else {
LocatedCmpOp::new(range, CmpOp::Is)
};
ops.push(op);
}
Tok::NotEqual => {
ops.push(LocatedCmpOp::new(range, CmpOp::NotEq));
}
Tok::EqEqual => {
ops.push(LocatedCmpOp::new(range, CmpOp::Eq));
}
Tok::GreaterEqual => {
ops.push(LocatedCmpOp::new(range, CmpOp::GtE));
}
Tok::Greater => {
ops.push(LocatedCmpOp::new(range, CmpOp::Gt));
}
Tok::LessEqual => {
ops.push(LocatedCmpOp::new(range, CmpOp::LtE));
}
Tok::Less => {
ops.push(LocatedCmpOp::new(range, CmpOp::Lt));
}
_ => {}
}
}
ops
Expand Down
Loading