Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hooks in the expression of a match statement and if statement #2902

Merged
merged 3 commits into from
Sep 7, 2024

Conversation

ealmloff
Copy link
Member

@ealmloff ealmloff commented Aug 29, 2024

Fixes dx check for this code snippet:

if use_signal(|| true) {
     println!("123")
}

match use_signal(|| true) {
    true => println!("false"),
    false => println!("true")
}

And report errors for this code snippet:

use_hook(|| use_signal(|| 0));
spawn(async move { use_signal(|| 0) });

@ealmloff ealmloff added bug Something isn't working check Related to the dioxus-check crate labels Aug 29, 2024
@DogeDark
Copy link
Member

Does Rust evaluate every condition in an if statement? E.g.

let some_value = false;

// Would the use_signal still get checked(ran) despite the conditional already failing?
if some_value && use_signal(|| true) {
    println!("123");
}

@ealmloff
Copy link
Member Author

Does Rust evaluate every condition in an if statement? E.g.

let some_value = false;

// Would the use_signal still get checked(ran) despite the conditional already failing?
if some_value && use_signal(|| true) {
    println!("123");
}

No, that case is still unchecked. Compared to if statements and closures, I think it is more rare, but we should lint for that eventually as well. Note it isn't specific to if statements, this code doesn't print anything either:

fn main() {
    let val = false && returns_true();
    assert!(!val)
}

fn returns_true() -> bool {
    panic!("this will not be run")
}

Copy link
Contributor

@matthunz matthunz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I never thought of using hooks like that but I'm sure I'll give it a go now. I posted #2940 to eventually add specific lints for && and || in if and match

@ealmloff ealmloff merged commit c11f2ed into DioxusLabs:main Sep 7, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working check Related to the dioxus-check crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants