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

prohibit the use of unwrap() #12228

Closed
rxdiscovery opened this issue Feb 5, 2024 · 2 comments
Closed

prohibit the use of unwrap() #12228

rxdiscovery opened this issue Feb 5, 2024 · 2 comments
Labels
A-lint Area: New lints

Comments

@rxdiscovery
Copy link

rxdiscovery commented Feb 5, 2024

What it does

using unwrap() in code exposes it to two risks:

  • uncontrolled exceptions (application crash)
  • exposure of confidential data (such as the project path, which generally contains the OS user's name), since unwrap() integrates the complete path of the source file into the final executable.

Advantage

  1. more stable code
  2. improved confidentiality
  3. the final executable will be smaller, since it won't include the path strings for panic.

Drawbacks

No response

Example

        let str = "1000".to_string();
        let number = str.parse::<i32>().unwrap(); //<------ risk of uncontrolled exception if str does not contain a number

Could be written as:

       let number = match str.parse::<i32>(){
            Ok(n) => n,
            Err(_) => todo!(),
        };

thank you in advance :

@rxdiscovery rxdiscovery added the A-lint Area: New lints label Feb 5, 2024
@kpreid
Copy link
Contributor

kpreid commented Feb 5, 2024

This lint is already available as clippy::unwrap_used.

@rxdiscovery
Copy link
Author

This lint is already available as clippy::unwrap_used.

Thank you for this information, I didn't know it existed. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants