---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---
RUF019 [*] Unnecessary key check before dictionary access
 --> RUF019.py:3:4
  |
1 | d = {}
2 | # RUF019
3 | if "k" in d and d["k"]:
  |    ^^^^^^^^^^^^^^^^^^^
4 |     pass
  |
help: Replace with `dict.get`
  |
2 | # RUF019
  - if "k" in d and d["k"]:
3 + if d.get("k"):
4 |     pass
  |

RUF019 [*] Unnecessary key check before dictionary access
 --> RUF019.py:7:4
  |
6 | k = "k"
7 | if k in d and d[k]:
  |    ^^^^^^^^^^^^^^^
8 |     pass
  |
help: Replace with `dict.get`
  |
6 | k = "k"
  - if k in d and d[k]:
7 + if d.get(k):
8 |     pass
  |

RUF019 [*] Unnecessary key check before dictionary access
  --> RUF019.py:10:4
   |
 8 |     pass
 9 |
10 | if (k) in d and d[k]:
   |    ^^^^^^^^^^^^^^^^^
11 |     pass
   |
help: Replace with `dict.get`
   |
9  |
   - if (k) in d and d[k]:
10 + if d.get(k):
11 |     pass
   |

RUF019 [*] Unnecessary key check before dictionary access
  --> RUF019.py:13:4
   |
11 |     pass
12 |
13 | if k in d and d[(k)]:
   |    ^^^^^^^^^^^^^^^^^
14 |     pass
   |
help: Replace with `dict.get`
   |
12 |
   - if k in d and d[(k)]:
13 + if d.get((k)):
14 |     pass
   |

RUF019 [*] Unnecessary key check before dictionary access
  --> RUF019.py:16:6
   |
14 |     pass
15 |
16 | not ("key" in dct and dct["key"])
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
17 |
18 | bool("key" in dct and dct["key"])
   |
help: Replace with `dict.get`
   |
15 |
   - not ("key" in dct and dct["key"])
16 + not (dct.get("key"))
17 |
   |

RUF019 [*] Unnecessary key check before dictionary access
  --> RUF019.py:18:6
   |
16 | not ("key" in dct and dct["key"])
17 |
18 | bool("key" in dct and dct["key"])
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
19 |
20 | # OK
   |
help: Replace with `dict.get`
   |
17 |
   - bool("key" in dct and dct["key"])
18 + bool(dct.get("key"))
19 |
   |

RUF019 [*] Unnecessary key check before dictionary access
  --> RUF019.py:28:9
   |
27 |   if (
28 | /         "key" in d
29 | |         and  # text
30 | |         d ["key"]
   | |_________________^
31 |   ):
32 |       ...
   |
help: Replace with `dict.get`
   |
27 | if (
   -         "key" in d
   -         and  # text
   -         d ["key"]
28 +         d.get("key")
29 | ):
   |
note: This is an unsafe fix and may change runtime behavior

RUF019 [*] Unnecessary key check before dictionary access
  --> RUF019.py:42:4
   |
41 | c = Formatter()
42 | if f"{c}" in d and d[f"{c}"]:
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^
43 |     pass
   |
help: Replace with `dict.get`
   |
41 | c = Formatter()
   - if f"{c}" in d and d[f"{c}"]:
42 + if d.get(f"{c}"):
43 |     pass
   |
note: This is an unsafe fix and may change runtime behavior

RUF019 [*] Unnecessary key check before dictionary access
  --> RUF019.py:46:4
   |
45 | # F-string with only literal interpolation — safe fix
46 | if f"{1}" in d and d[f"{1}"]:
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^
47 |     pass
   |
help: Replace with `dict.get`
   |
45 | # F-string with only literal interpolation — safe fix
   - if f"{1}" in d and d[f"{1}"]:
46 + if d.get(f"{1}"):
47 |     pass
   |

RUF019 [*] Unnecessary key check before dictionary access
  --> RUF019.py:50:4
   |
49 | # Plain f-string without interpolation — safe fix
50 | if f"key" in d and d[f"key"]:
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^
51 |     pass
   |
help: Replace with `dict.get`
   |
49 | # Plain f-string without interpolation — safe fix
   - if f"key" in d and d[f"key"]:
50 + if d.get(f"key"):
51 |     pass
   |
