mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Add required space when fixing SIM118 (#7150)
This commit is contained in:
parent
7a83fd9926
commit
e428099e4c
3 changed files with 76 additions and 1 deletions
|
@ -42,3 +42,9 @@ class Foo:
|
||||||
|
|
||||||
def __contains__(self, key: object) -> bool:
|
def __contains__(self, key: object) -> bool:
|
||||||
return key in self.keys() # OK
|
return key in self.keys() # OK
|
||||||
|
|
||||||
|
|
||||||
|
# Regression test for: https://github.com/astral-sh/ruff/issues/7124
|
||||||
|
key in obj.keys()and foo
|
||||||
|
(key in obj.keys())and foo
|
||||||
|
key in (obj.keys())and foo
|
||||||
|
|
|
@ -116,8 +116,24 @@ fn key_in_dict(
|
||||||
TextRange::new(left_range.start(), right_range.end()),
|
TextRange::new(left_range.start(), right_range.end()),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
|
// If the `.keys()` is followed by (e.g.) a keyword, we need to insert a space,
|
||||||
|
// since we're removing parentheses, which could lead to invalid syntax, as in:
|
||||||
|
// ```python
|
||||||
|
// if key in foo.keys()and bar:
|
||||||
|
// ```
|
||||||
|
let requires_space = checker
|
||||||
|
.locator()
|
||||||
|
.after(right_range.end())
|
||||||
|
.chars()
|
||||||
|
.next()
|
||||||
|
.is_some_and(|char| char.is_ascii_alphabetic());
|
||||||
|
|
||||||
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
|
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
|
||||||
value_content.to_string(),
|
if requires_space {
|
||||||
|
format!("{value_content} ")
|
||||||
|
} else {
|
||||||
|
value_content.to_string()
|
||||||
|
},
|
||||||
right_range,
|
right_range,
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -309,4 +309,57 @@ SIM118.py:34:1: SIM118 [*] Use `(key) in (obj or {})` instead of `(key) in (obj
|
||||||
36 36 | from typing import KeysView
|
36 36 | from typing import KeysView
|
||||||
37 37 |
|
37 37 |
|
||||||
|
|
||||||
|
SIM118.py:48:1: SIM118 [*] Use `key in obj` instead of `key in obj.keys()`
|
||||||
|
|
|
||||||
|
47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
|
||||||
|
48 | key in obj.keys()and foo
|
||||||
|
| ^^^^^^^^^^^^^^^^^ SIM118
|
||||||
|
49 | (key in obj.keys())and foo
|
||||||
|
50 | key in (obj.keys())and foo
|
||||||
|
|
|
||||||
|
= help: Convert to `key in obj`
|
||||||
|
|
||||||
|
ℹ Suggested fix
|
||||||
|
45 45 |
|
||||||
|
46 46 |
|
||||||
|
47 47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
|
||||||
|
48 |-key in obj.keys()and foo
|
||||||
|
48 |+key in obj and foo
|
||||||
|
49 49 | (key in obj.keys())and foo
|
||||||
|
50 50 | key in (obj.keys())and foo
|
||||||
|
|
||||||
|
SIM118.py:49:2: SIM118 [*] Use `key in obj` instead of `key in obj.keys()`
|
||||||
|
|
|
||||||
|
47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
|
||||||
|
48 | key in obj.keys()and foo
|
||||||
|
49 | (key in obj.keys())and foo
|
||||||
|
| ^^^^^^^^^^^^^^^^^ SIM118
|
||||||
|
50 | key in (obj.keys())and foo
|
||||||
|
|
|
||||||
|
= help: Convert to `key in obj`
|
||||||
|
|
||||||
|
ℹ Suggested fix
|
||||||
|
46 46 |
|
||||||
|
47 47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
|
||||||
|
48 48 | key in obj.keys()and foo
|
||||||
|
49 |-(key in obj.keys())and foo
|
||||||
|
49 |+(key in obj)and foo
|
||||||
|
50 50 | key in (obj.keys())and foo
|
||||||
|
|
||||||
|
SIM118.py:50:1: SIM118 [*] Use `key in obj` instead of `key in obj.keys()`
|
||||||
|
|
|
||||||
|
48 | key in obj.keys()and foo
|
||||||
|
49 | (key in obj.keys())and foo
|
||||||
|
50 | key in (obj.keys())and foo
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^ SIM118
|
||||||
|
|
|
||||||
|
= help: Convert to `key in obj`
|
||||||
|
|
||||||
|
ℹ Suggested fix
|
||||||
|
47 47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
|
||||||
|
48 48 | key in obj.keys()and foo
|
||||||
|
49 49 | (key in obj.keys())and foo
|
||||||
|
50 |-key in (obj.keys())and foo
|
||||||
|
50 |+key in obj and foo
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue