Skip to content

Commit 3bfccac

Browse files
committed
Add comments + Very minor Refactor
1 parent 4e1db44 commit 3bfccac

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

clippy_lints/src/option_env_unwrap.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ impl EarlyLintPass for OptionEnvUnwrap {
4848

4949
if let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &expr.kind &&
5050
matches!(seg.ident.name, sym::expect | sym::unwrap) {
51-
if let ExprKind::Call(caller, _) = &receiver.kind && is_direct_expn_of(caller.span, "option_env").is_some() {
51+
if let ExprKind::Call(caller, _) = &receiver.kind &&
52+
// If it exists, it will be ::core::option::Option::Some("<env var>").unwrap() (A method call in the HIR)
53+
is_direct_expn_of(caller.span, "option_env").is_some() {
5254
lint(cx, expr.span);
53-
} else if let ExprKind::Path(_, caller) = &receiver.kind && is_direct_expn_of(caller.span, "option_env").is_some() {
55+
} else if let ExprKind::Path(_, caller) = &receiver.kind && // If it doesn't exist, it will be ::core::option::Option::None::<&'static str>.unwrap() (A path in the HIR)
56+
is_direct_expn_of(caller.span, "option_env").is_some() {
5457
lint(cx, expr.span);
5558
}
5659
}

tests/ui/option_env_unwrap.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ LL | let _ = option_env!("PATH").expect("environment variable PATH isn't set
1818
error: this will panic at run-time if the environment variable doesn't exist at compile-time
1919
--> $DIR/option_env_unwrap.rs:12:13
2020
|
21-
LL | let _ = option_env!("Y").unwrap(); // This test only works if you don't have a __Y__do_not_use env variable in your environment.
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
21+
LL | let _ = option_env!("__Y__do_not_use").unwrap(); // This test only works if you don't have a __Y__do_not_use env variable in your env...
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323
|
2424
= help: consider using the `env!` macro instead
2525

0 commit comments

Comments
 (0)