Skip to content

fix: add #[must_use] to Hook trait to warn on invalid placement#4052

Open
Madoshakalaka wants to merge 1 commit intomasterfrom
hook-must-use
Open

fix: add #[must_use] to Hook trait to warn on invalid placement#4052
Madoshakalaka wants to merge 1 commit intomasterfrom
hook-must-use

Conversation

@Madoshakalaka
Copy link
Member

@Madoshakalaka Madoshakalaka commented Mar 6, 2026

Description

Fixes the silent failure described in #4045: hooks called from plain functions (without #[hook] or #[component]) return an impl Hook value that gets dropped without ever executing. This is especially harmful for unit-returning hooks like use_effect_with, where there is no type mismatch to catch the mistake.

Adding #[must_use] to the Hook trait causes the compiler to emit a warning when a hook's return value is unused, with a message directing users to annotate their function with #[hook] or #[component].

error: unused implementer of `Hook` that must be used
 --> src/app.rs:6:5
  |
6 |     use_effect_with((), |_| {});
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: hooks do nothing unless called inside a `#[hook]` or `#[component]` function

Hooks inside #[hook] or #[component] are unaffected because the macro rewrites calls to Hook::run(...), which consumes the value.

Checklist

  • I have reviewed my own code
  • I have added tests

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

Benchmark - core

Yew Master

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.092 ns      │ 3.551 ns      │ 2.094 ns      │ 2.237 ns      │ 100     │ 1000000000

Pull Request

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.125 ns      │ 3.405 ns      │ 2.129 ns      │ 2.328 ns      │ 100     │ 1000000000

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

Visit the preview URL for this PR (updated for commit 119a329):

https://yew-rs-api--pr4052-hook-must-use-we8m29ma.web.app

(expires Fri, 13 Mar 2026 13:47:57 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

Size Comparison

Details
examples master (KB) pull request (KB) diff (KB) diff (%)
async_clock 100.272 100.272 0 0.000%
boids 168.099 168.099 0 0.000%
communication_child_to_parent 93.485 93.485 0 0.000%
communication_grandchild_with_grandparent 105.262 105.262 0 0.000%
communication_grandparent_to_grandchild 101.610 101.610 0 0.000%
communication_parent_to_child 90.899 90.899 0 0.000%
contexts 105.177 105.177 0 0.000%
counter 86.282 86.282 0 0.000%
counter_functional 88.276 88.276 0 0.000%
dyn_create_destroy_apps 90.322 90.322 0 0.000%
file_upload 99.377 99.377 0 0.000%
function_delayed_input 94.377 94.377 0 0.000%
function_memory_game 172.944 172.944 0 0.000%
function_router 406.599 406.599 0 0.000%
function_todomvc 164.163 164.163 0 0.000%
futures 235.191 235.191 0 0.000%
game_of_life 104.732 104.732 0 0.000%
immutable 255.891 255.891 0 0.000%
inner_html 80.803 80.803 0 0.000%
js_callback 109.401 109.401 0 0.000%
keyed_list 179.740 179.740 0 0.000%
mount_point 84.174 84.174 0 0.000%
nested_list 113.072 113.072 0 0.000%
node_refs 91.523 91.523 0 0.000%
password_strength 1729.185 1729.185 0 0.000%
portals 93.034 93.034 0 0.000%
router 377.158 377.158 0 0.000%
suspense 113.502 113.502 0 0.000%
timer 88.641 88.641 0 0.000%
timer_functional 98.881 98.881 0 0.000%
todomvc 142.114 142.114 0 0.000%
two_apps 86.176 86.176 0 0.000%
web_worker_fib 136.155 136.155 0 0.000%
web_worker_prime 187.383 187.383 0 0.000%
webgl 83.224 83.224 0 0.000%

✅ None of the examples has changed their size significantly.

@Madoshakalaka
Copy link
Member Author

@gjgiezeman thanks for the report.

@Madoshakalaka
Copy link
Member Author

Madoshakalaka commented Mar 6, 2026

@kirillsemyonkin I tried your suggestion of adding an invisible parameter but couldn't quite make it work.

Then I realized adding a must_use does the job too.

@Madoshakalaka Madoshakalaka marked this pull request as ready for review March 6, 2026 14:01
@github-actions
Copy link

github-actions bot commented Mar 6, 2026

Benchmark - SSR

Yew Master

Details
Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 310.670 312.709 311.132 0.587
Hello World 10 527.308 551.887 538.471 8.525
Function Router 10 33567.337 34673.750 34075.705 433.349
Concurrent Task 10 1006.165 1007.960 1007.217 0.701
Many Providers 10 1097.925 1121.988 1109.916 7.658

Pull Request

Details
Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 310.755 312.338 311.134 0.458
Hello World 10 464.889 483.549 468.722 6.596
Function Router 10 32873.689 34277.521 33416.856 458.096
Concurrent Task 10 1004.986 1007.617 1006.936 0.829
Many Providers 10 1062.664 1106.692 1080.457 15.222

@Madoshakalaka Madoshakalaka added the A-yew Area: The main yew crate label Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-yew Area: The main yew crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant