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

chore(clippy): no unwrap in result function #349

Merged
merged 1 commit into from
Jun 1, 2022

Conversation

h-a-n-a
Copy link
Collaborator

@h-a-n-a h-a-n-a commented Jun 1, 2022

New rule added:

For those who curious about solving lint issues:

  1. how to deal with Result: use anyhow as Result, and for those errors that implements Error, anyhow would convert it to anyhow::Error
struct MyError {}

impl Error for MyError {} // implementing `Error` trait from std

fn () -> anyhow::Result<()> { // basically a wrapper around std Result, with `anyhow::Error` set to the error section 
   some_function()?; // this function would return `Result<(), MyError>`, since the error implements Error trait, anyhow could utilize this to convert it directly

   // you can also provide the context to `anyhow`
   some_function().context("more informations about this error can be stated here")?; // it's the identical function as above.
}

// also, map_err is available for those error types that don't implement std Error

some_result.map_err(|e| { 
  // you may convert it to your own error type, or use `anyhow::anyhow!()` to create an anyhow Error type.
})
  1. how to deal with Option
some_option_type.ok_or_else(|e| {  
   // you can convert the error here like above.
})

For more information, you may checkout the doc provided by anyhow: https://docs.rs/anyhow/latest/anyhow/

More information about rust-clippy

Clippy is not currently supporting workspace lint rules, according to EmbarkStudios/rust-ecosystem#68, this can be achieved by adding rustflags to compiler.

@h-a-n-a h-a-n-a requested review from hyf0 and hardfist June 1, 2022 10:33
@h-a-n-a h-a-n-a changed the title feat: no unwrap in result function chore(clippy): no unwrap in result function Jun 1, 2022
@h-a-n-a h-a-n-a removed the request for review from hyf0 June 1, 2022 10:39
@h-a-n-a h-a-n-a enabled auto-merge (squash) June 1, 2022 10:48
@IWANABETHATGUY
Copy link
Contributor

What about Option unwrap, which may also raise a panic

@h-a-n-a
Copy link
Collaborator Author

h-a-n-a commented Jun 1, 2022

Rspack Benchmark Result

Fixture

Cold start(Decrease detected ✓)

Arco Design

Metric Main Pull Request Diff
Cold Start Diff 11.9s 11.8s -54ms

@h-a-n-a
Copy link
Collaborator Author

h-a-n-a commented Jun 1, 2022

What about Option unwrap, which may also raise a panic

If Option exists in a function that returns Result, you can use ok_or_else to map None to anyhow::Error, and clippy will complain if you call unwrap() on Option directly in this function

@h-a-n-a h-a-n-a merged commit ca9d9f3 into main Jun 1, 2022
@h-a-n-a h-a-n-a deleted the chore/no-unwrap-in-result branch June 1, 2022 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants