yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications

Home Page:https://yew.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong `clone` method position when displaying a help message.

wiseaidev opened this issue · comments

Current Behavior

When consuming a value, if the user doesn't clone the initial variable, a help message will be displayed as follows:

help: you can `clone` the value and consume it, but this might not be your desired behavior
   |
32 |                         src={clone().feature.icon}
   |                              ++++++++

As you can see, the clone method is in the wrong position.

Expected Behavior

The clone method should be placed after the variable name, like so:

help: you can `clone` the value and consume it, but this might not be your desired behavior
  |
32 |                         src={feature.clone().icon}
  |                                       ++++++++

Code to Reproduce

use yew::prelude::*;

#[derive(Clone, Default, PartialEq, Debug)]
pub struct Feature {
  pub id: u32,
  pub title: String,
  pub description: String,
  pub icon: String,
}

#[function_component(App)]
fn app() -> Html {
    let features = vec![
    Feature {
        id: 1,
        title: "foo".to_string(),
        description: "bar".to_string(),
        icon: "baz".to_string(),
    },];
    html! {
        <div class="section-features-main" id="features">
          <div class="wrapper">
            <div class="features">
            {features
            .iter()
            .map(|feature| {
                html! {
                  <div class="features-item" key={feature.id}>
                    <div class="features-decor"></div>
                    <div class="features-icon">
                      <img
                        src={feature.icon}
                        alt=""
                        loading="lazy"
                      />
                    </div>
                    <div class="features-title">{feature.title}</div>
                    <div class="features-text">
                    {feature.description}
                    </div>

            </div>
                }
            })
            .collect::<Vec<_>>()
            }
            </div>
          </div>
        </div>
    }
}

fn main() {
    yew::Renderer::<App>::new().render();
}

Edit

I have found another edge case when you pass the variable as a property tag like the following:

<a {class} href="#">

You will get a help message like the following:

   | |_____- `class` moved due to this method call
   |
help: you can `clone` the value and consume it, but this might not be your desired behavior
   |
37 |     clone().html! {
   |     ++++++++

It should be corrected to:

   | |_____- `class` moved due to this method call
   |
help: you can `clone` the value and consume it, but this might not be your desired behavior
   |
37 |    <a class={class.clone()} href="#">
   |                  ++++++++

using this pattern: variable={variable.clone()}.

looks like a typo of rustc, similar case here rust-lang/rust#111016

commented

This is a rustc bug that has already been fixed