File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -79,9 +79,40 @@ fn check_deprecated_html_call(expr: &Expr) -> TokenStream {
7979 }
8080 }
8181
82+ // Pattern 3: { if cond { html! { ... } } else { html! { ... } } }
83+ if let Expr :: If ( if_expr) = expr {
84+ if let Some ( span) = if_branch_html_macro_span ( if_expr) {
85+ return super :: deprecated_call (
86+ span,
87+ "`html!` is not needed inside `if`/`else` branches. Use bare elements directly" ,
88+ ) ;
89+ }
90+ }
91+
8292 TokenStream :: new ( )
8393}
8494
95+ /// Walk through an `if`/`else if`/`else` chain and return the span of the first tail `html!` call.
96+ fn if_branch_html_macro_span ( if_expr : & syn:: ExprIf ) -> Option < proc_macro2:: Span > {
97+ if let Some ( span) = if_expr
98+ . then_branch
99+ . stmts
100+ . last ( )
101+ . and_then ( stmt_tail_html_macro_span)
102+ {
103+ return Some ( span) ;
104+ }
105+ match if_expr. else_branch . as_ref ( ) . map ( |( _, expr) | expr. as_ref ( ) ) {
106+ Some ( Expr :: Block ( block_expr) ) => block_expr
107+ . block
108+ . stmts
109+ . last ( )
110+ . and_then ( stmt_tail_html_macro_span) ,
111+ Some ( Expr :: If ( nested) ) => if_branch_html_macro_span ( nested) ,
112+ _ => None ,
113+ }
114+ }
115+
85116/// Check if a statement is a tail `html!`/`html_nested!` macro call (no trailing semicolon).
86117fn stmt_tail_html_macro_span ( stmt : & Stmt ) -> Option < proc_macro2:: Span > {
87118 match stmt {
Original file line number Diff line number Diff line change @@ -81,6 +81,13 @@ fn nested_block_with_html_tail() {
8181 } ;
8282}
8383
84+ fn if_else_block_with_html_branches ( ) {
85+ let cond = true ;
86+ let _ = html ! {
87+ <div>{ if cond { html! { <span>{ "yes" } </span> } } else { html! { <span>{ "no" } </span> } } } </div>
88+ } ;
89+ }
90+
8491fn main ( ) {
8592 compile_error ! ( "This macro call exists to deliberately fail the compilation of the test so we can verify output of deprecation lints" ) ;
8693}
Original file line number Diff line number Diff line change 11error: This macro call exists to deliberately fail the compilation of the test so we can verify output of deprecation lints
2- --> tests/html_deprecation/fail.rs:85 :5
2+ --> tests/html_deprecation/fail.rs:92 :5
33 |
4- 85 | compile_error!("This macro call exists to deliberately fail the compilation of the test so we can verify output of deprecation lints...
4+ 92 | compile_error!("This macro call exists to deliberately fail the compilation of the test so we can verify output of deprecation lints...
55 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
77warning: use of deprecated function `root_level_fragment::__yew_deprecated`: unnecessary `<>...</>`. Children can be placed directly in the body
@@ -81,3 +81,9 @@ warning: use of deprecated function `nested_block_with_html_tail::__yew_deprecat
8181 |
828280 | <div>{{ let processed = item.to_uppercase(); html! { <span>{processed}</span> } }}</div>
8383 | ^^^^
84+
85+ warning: use of deprecated function `if_else_block_with_html_branches::__yew_deprecated`: `html!` is not needed inside `if`/`else` branches. Use bare elements directly
86+ --> tests/html_deprecation/fail.rs:87:26
87+ |
88+ 87 | <div>{ if cond { html! { <span>{"yes"}</span> } } else { html! { <span>{"no"}</span> } } }</div>
89+ | ^^^^
You can’t perform that action at this time.
0 commit comments