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

User Story: Using literals in function call #29

Open
Emilgardis opened this issue Sep 29, 2022 · 2 comments
Open

User Story: Using literals in function call #29

Emilgardis opened this issue Sep 29, 2022 · 2 comments
Labels
A-user-story Area: A user story or a related issue

Comments

@Emilgardis
Copy link

Lint explanation

In cross there is a function which when called with a literal is most often wrong.

Example code

pub fn bool_from_envvar(envvar: &str) -> bool {
    if let Ok(value) = bool::from_str(envvar) {
        value
    } else if let Ok(value) = i32::from_str(envvar) {
        value != 0
    } else {
        !envvar.is_empty()
    }
}

// correct
bool_from_envvar(std::env::var("FOO")?);

// incorrect
bool_from_envvar("FOO");

Notes

Being able to say "this parameter on this function should not be from a literal" would be helpful here.

@Emilgardis Emilgardis added the A-user-story Area: A user story or a related issue label Sep 29, 2022
@Emilgardis Emilgardis changed the title User Story: Using literals User Story: Using literals in function call Sep 29, 2022
@Alexhuszagh
Copy link

Alexhuszagh commented Sep 29, 2022

It might also be worth mentioning that for certain cases, only 0, 1, true, false are allowed, or another set of accepted values (we couldn't do that in our application due to backwards compatibility concerns).

@xFrednet
Copy link
Member

Thank you for the user story. Testing for literals and allowing specific literals is definitely planned :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-user-story Area: A user story or a related issue
Projects
None yet
Development

No branches or pull requests

3 participants