Remove extra braces in html_nested macro (#2169)

* removed unused braces from html_nested marco

* allow specifying test name

* also fix for html! macro

* also fix for html! macro

* remove misplaced #[allow(unused_braces)]
This commit is contained in:
Matt 2021-11-21 23:09:30 +08:00 committed by GitHub
parent 7e2542cbf8
commit 4c3d6934a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 25 deletions

View File

@ -2,7 +2,8 @@
clear = true
toolchain = "1.51"
command = "cargo"
args = ["test"]
# test target can be optionally specified like `cargo make test html_macro`,
args = ["test", "${@}"]
[tasks.test-lint]
clear = true

View File

@ -120,8 +120,8 @@ impl ToTokens for HtmlComponent {
tokens.extend(quote_spanned! {ty.span()=>
{
#[allow(clippy::unit_arg)]
::yew::virtual_dom::VChild::<#ty>::new(#build_props, #node_ref, #key)
let __yew_props = #build_props;
::yew::virtual_dom::VChild::<#ty>::new(__yew_props, #node_ref, #key)
}
});
}

View File

@ -346,7 +346,6 @@ impl ToTokens for HtmlElement {
}
}
TagName::Expr(name) => {
#[allow(unused_braces)]
let vtag = Ident::new("__yew_vtag", name.span());
let expr = &name.expr;
let vtag_name = Ident::new("__yew_vtag_name", expr.span());
@ -362,6 +361,10 @@ impl ToTokens for HtmlElement {
// this way we get a nice error message (with the correct span) when the expression
// doesn't return a valid value
quote_spanned! {expr.span()=> {
#[allow(unused_braces)]
// e.g. html!{<@{"div"}/>} will set `#expr` to `{"div"}`
// (note the extra braces). Hence the need for the `allow`.
// Anyways to remove the braces?
let mut #vtag_name = ::std::convert::Into::<
::std::borrow::Cow::<'static, ::std::primitive::str>
>::into(#expr);

View File

@ -171,7 +171,7 @@ impl ToTokens for HtmlRootVNode {
fn to_tokens(&self, tokens: &mut TokenStream) {
let new_tokens = self.0.to_token_stream();
tokens.extend(quote! {{
#[allow(clippy::useless_conversion, unused_braces)]
#[allow(clippy::useless_conversion)]
<::yew::virtual_dom::VNode as ::std::convert::From<_>>::from(#new_tokens)
}});
}

View File

@ -1,5 +1,5 @@
error[E0412]: cannot find type `INVALID` in this scope
--> $DIR/generic-props-fail.rs:25:19
--> tests/function_component_attr/generic-props-fail.rs:25:19
|
20 | fn compile_fail() {
| - help: you might be missing a type parameter: `<INVALID>`
@ -8,7 +8,7 @@ error[E0412]: cannot find type `INVALID` in this scope
| ^^^^^^^ not found in this scope
error[E0599]: no method named `build` found for struct `PropsBuilder<PropsBuilderStep_missing_required_prop_a>` in the current scope
--> $DIR/generic-props-fail.rs:22:14
--> tests/function_component_attr/generic-props-fail.rs:22:14
|
3 | #[derive(Clone, Properties, PartialEq)]
| ---------- method `build` not found for this
@ -16,8 +16,16 @@ error[E0599]: no method named `build` found for struct `PropsBuilder<PropsBuilde
22 | html! { <Comp<Props> /> };
| ^^^^ method not found in `PropsBuilder<PropsBuilderStep_missing_required_prop_a>`
error[E0277]: the trait bound `MissingTypeBounds: yew::Properties` is not satisfied
--> tests/function_component_attr/generic-props-fail.rs:27:14
|
27 | html! { <Comp<MissingTypeBounds> /> };
| ^^^^ the trait `yew::Properties` is not implemented for `MissingTypeBounds`
|
= note: required because of the requirements on the impl of `FunctionProvider` for `comp<MissingTypeBounds>`
error[E0599]: the function or associated item `new` exists for struct `VChild<FunctionComponent<comp<MissingTypeBounds>>>`, but its trait bounds were not satisfied
--> $DIR/generic-props-fail.rs:27:14
--> tests/function_component_attr/generic-props-fail.rs:27:14
|
27 | html! { <Comp<MissingTypeBounds> /> };
| ^^^^ function or associated item cannot be called on `VChild<FunctionComponent<comp<MissingTypeBounds>>>` due to unsatisfied trait bounds
@ -30,22 +38,14 @@ error[E0599]: the function or associated item `new` exists for struct `VChild<Fu
= note: the following trait bounds were not satisfied:
`FunctionComponent<comp<MissingTypeBounds>>: yew::Component`
error[E0277]: the trait bound `MissingTypeBounds: yew::Properties` is not satisfied
--> $DIR/generic-props-fail.rs:27:14
|
27 | html! { <Comp<MissingTypeBounds> /> };
| ^^^^ the trait `yew::Properties` is not implemented for `MissingTypeBounds`
|
= note: required because of the requirements on the impl of `FunctionProvider` for `comp<MissingTypeBounds>`
error[E0107]: missing generics for type alias `Comp`
--> $DIR/generic-props-fail.rs:30:14
--> tests/function_component_attr/generic-props-fail.rs:30:14
|
30 | html! { <Comp /> };
| ^^^^ expected 1 type argument
|
note: type alias defined here, with 1 type parameter: `P`
--> $DIR/generic-props-fail.rs:8:22
--> tests/function_component_attr/generic-props-fail.rs:8:22
|
8 | #[function_component(Comp)]
| ^^^^

View File

@ -0,0 +1,63 @@
#![no_implicit_prelude]
// Shadow primitives
#[allow(non_camel_case_types)]
pub struct bool;
#[allow(non_camel_case_types)]
pub struct char;
#[allow(non_camel_case_types)]
pub struct f32;
#[allow(non_camel_case_types)]
pub struct f64;
#[allow(non_camel_case_types)]
pub struct i128;
#[allow(non_camel_case_types)]
pub struct i16;
#[allow(non_camel_case_types)]
pub struct i32;
#[allow(non_camel_case_types)]
pub struct i64;
#[allow(non_camel_case_types)]
pub struct i8;
#[allow(non_camel_case_types)]
pub struct isize;
#[allow(non_camel_case_types)]
pub struct str;
#[allow(non_camel_case_types)]
pub struct u128;
#[allow(non_camel_case_types)]
pub struct u16;
#[allow(non_camel_case_types)]
pub struct u32;
#[allow(non_camel_case_types)]
pub struct u64;
#[allow(non_camel_case_types)]
pub struct u8;
#[allow(non_camel_case_types)]
pub struct usize;
pub struct MyComponent;
impl ::yew::Component for MyComponent {
type Message = ();
type Properties = ();
fn create(_ctx: &::yew::Context<Self>) -> Self {
::std::unimplemented!()
}
fn view(&self, _ctx: &::yew::Context<Self>) -> ::yew::Html {
::std::unimplemented!()
}
}
// can test "unused braces" warning inside the macro
// https://github.com/yewstack/yew/issues/2157
fn make_my_component()-> ::yew::virtual_dom::VChild<MyComponent>{
::yew::html_nested!{<MyComponent/>}
}
// can test "unused braces" warning inside the macro
// https://github.com/yewstack/yew/issues/2157
fn make_my_component_html()-> ::yew::Html{
::yew::html!{<MyComponent/>}
}
fn main(){}

View File

@ -1,5 +1,11 @@
error[E0277]: the trait bound `Unimplemented: yew::Component` is not satisfied
--> tests/html_macro/component-unimplemented-fail.rs:6:14
|
6 | html! { <Unimplemented /> };
| ^^^^^^^^^^^^^ the trait `yew::Component` is not implemented for `Unimplemented`
error[E0599]: the function or associated item `new` exists for struct `VChild<Unimplemented>`, but its trait bounds were not satisfied
--> $DIR/component-unimplemented-fail.rs:6:14
--> tests/html_macro/component-unimplemented-fail.rs:6:14
|
3 | struct Unimplemented;
| --------------------- doesn't satisfy `Unimplemented: yew::Component`
@ -9,9 +15,3 @@ error[E0599]: the function or associated item `new` exists for struct `VChild<Un
|
= note: the following trait bounds were not satisfied:
`Unimplemented: yew::Component`
error[E0277]: the trait bound `Unimplemented: yew::Component` is not satisfied
--> $DIR/component-unimplemented-fail.rs:6:14
|
6 | html! { <Unimplemented /> };
| ^^^^^^^^^^^^^ the trait `yew::Component` is not implemented for `Unimplemented`