25 lines
663 B
Plaintext
25 lines
663 B
Plaintext
![]() |
== Why is this an issue?
|
||
|
|
||
|
Unwrapping the result of `option_env!` will panic at runtime if the environment variable doesn't exist, whereas `env!` catches it at compile-time, ensuring safer code execution.
|
||
|
|
||
|
=== Code examples
|
||
|
|
||
|
==== Noncompliant code example
|
||
|
|
||
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
||
|
---_
|
||
|
let _ = option_env!("HOME").unwrap(); // Noncompliant: Can panic at runtime.
|
||
|
---_
|
||
|
|
||
|
==== Compliant solution
|
||
|
|
||
|
[source,rust,diff-id=1,diff-type=compliant]
|
||
|
---_
|
||
|
let _ = env!("HOME"); // Compliant: Ensures compile-time checking.
|
||
|
---_
|
||
|
|
||
|
== Resources
|
||
|
=== Documentation
|
||
|
|
||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#option_env_unwrap
|