bevyengine / bevyengine/bevy

Remove redundant check in `Reflect::downcast` method

Open
#5,120 5 comments 0 reactions 0 assignees View on GitHub
A-Reflection C-Code-Quality
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

When attempting to downcast to a type, we check if the type is correct [here](https://github.com/bevyengine/bevy/blob/9eb69282efe57b9e3922e66ade887e6239bff175/crates/bevy_reflect/src/reflect.rs#L218). However, the [`Any::downcast`](https://docs.rs/downcast/latest/downcast/trait.Any.html) method used inside this call does this anyways.

We should remove this check, and then match on the returned `Result` from the `Any` version.

Spotted by @cart in #5010 [here](https://github.com/bevyengine/bevy/pull/5010#discussion_r907776272).

Contributor guide

Open the contributing guide

Research direction

Start in crates/bevy_reflect/src/reflect.rs around line 218, where Reflect::downcast performs the redundant type check. Read the surrounding method and the linked Any::downcast behavior, then remove the duplicate check and match on the returned Result. Done means Reflect::downcast relies on the Any result without performing the check twice.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.