mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 10:22:24 +00:00
Respect 'is not' operators split across newlines (#4977)
This commit is contained in:
parent
d647105e97
commit
16d1e63a5e
3 changed files with 33 additions and 2 deletions
|
@ -19,3 +19,6 @@ if ("123" != x) is 3:
|
|||
|
||||
if "123" != (x is 3):
|
||||
pass
|
||||
|
||||
{2 is
|
||||
not ''}
|
||||
|
|
|
@ -136,5 +136,26 @@ F632.py:20:14: F632 [*] Use `==` to compare constant literals
|
|||
20 |-if "123" != (x is 3):
|
||||
20 |+if "123" != (x == 3):
|
||||
21 21 | pass
|
||||
22 22 |
|
||||
23 23 | {2 is
|
||||
|
||||
F632.py:23:2: F632 [*] Use `!=` to compare constant literals
|
||||
|
|
||||
23 | pass
|
||||
24 |
|
||||
25 | {2 is
|
||||
| __^
|
||||
26 | | not ''}
|
||||
| |______^ F632
|
||||
|
|
||||
= help: Replace `is not` with `!=`
|
||||
|
||||
ℹ Suggested fix
|
||||
20 20 | if "123" != (x is 3):
|
||||
21 21 | pass
|
||||
22 22 |
|
||||
23 |-{2 is
|
||||
24 |-not ''}
|
||||
23 |+{2 != ''}
|
||||
|
||||
|
||||
|
|
|
@ -1440,14 +1440,21 @@ impl LocatedCmpop {
|
|||
}
|
||||
}
|
||||
|
||||
/// Extract all [`Cmpop`] operators from a source code snippet, with appropriate
|
||||
/// Extract all [`Cmpop`] operators from an expression snippet, with appropriate
|
||||
/// ranges.
|
||||
///
|
||||
/// `RustPython` doesn't include line and column information on [`Cmpop`] nodes.
|
||||
/// `CPython` doesn't either. This method iterates over the token stream and
|
||||
/// re-identifies [`Cmpop`] nodes, annotating them with valid ranges.
|
||||
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::Expression)
|
||||
.flatten()
|
||||
.filter(|(tok, _)|
|
||||
// Skip whitespace, including Tok::Newline. It should be sufficient to skip
|
||||
// Tok::NonLogicalNewline, but the lexer doesn't know that we're within an
|
||||
// expression, and so it may confusion logical and non-logical newlines.
|
||||
!matches!(tok, Tok::Newline | Tok::NonLogicalNewline | Tok::Comment(_)))
|
||||
.peekable();
|
||||
let mut ops: Vec<LocatedCmpop> = vec![];
|
||||
let mut count = 0u32;
|
||||
loop {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue