B018 does not trigger on dictionary subscript
- Dominant language
- Rust
- Stars
- 49.6k
- Forks
- 2.4k
- Avg merge
- 2d 1h
- Merged PRs (30d)
- 458
Description
### Summary
I would have expected useless subscripts to get flagged the same way as useless attribute access, but it currently isn't the case:
```py
x.attr # B018
x["item"] # no error
```
In case you're wondering, this would have prevented a real bug. In a codebase, I found some code that looks like:
```py
my_value = my_dictionary["some_long_key"]["another_long_key"]
+my_dictionary["yet_another_long_key"]
-my_dictionary["again_another_key"]
```
The intention was clearly to assign the result of the operations to `my_value`, but, due to missing parentheses, only the first term is assigned and the following 2 lines are useless expressions.
I do realize there could be some corner cases where `__getitem__` has a side-effect, though I believe the [caveats in the B018 documentation](https://docs.astral.sh/ruff/rules/useless-expression/#known-problems) apply here as well and an explicit ignore/assignment is warranted. The same could be said about `__pos__` and `__neg__`, though they don't prevent an expression from being flagged:
```py
+x # B018
-x # B018
```
### Version
ruff 0.15.6 (e4c7f3577 2026-03-12)
Contributor guide
Research direction
Start with Ruff's B018 rule and the linked useless-expression documentation, comparing the reported attribute-access behavior with dictionary subscripts and the shown Python examples. Done means the intended useless dictionary-subscript expressions are flagged consistently while the documented side-effect caveats remain applicable.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100