Skip to content

Commit 0e5ba2f

Browse files
Account for expect being used to unwrap Option
1 parent be1bc57 commit 0e5ba2f

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

clippy_lints/src/option_env_unwrap.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ impl EarlyLintPass for OptionEnvUnwrap {
3838
if_chain! {
3939
if !in_external_macro(cx.sess, expr.span);
4040
if let ExprKind::MethodCall(path_segment, args) = &expr.kind;
41-
if path_segment.ident.as_str() == "unwrap";
41+
let method_name = path_segment.ident.as_str();
42+
if method_name == "expect" || method_name == "unwrap";
4243
if let ExprKind::Call(caller, _) = &args[0].kind;
4344
if is_direct_expn_of(caller.span, "option_env").is_some();
4445
then {
4546
span_lint_and_help(
4647
cx,
4748
OPTION_ENV_UNWRAP,
4849
expr.span,
49-
"this will panic at run-time if the environment variable doesn't exist",
50+
"this will panic at run-time if the environment variable doesn't exist at compile-time",
5051
"consider using the `env!` macro instead"
5152
);
5253
}

tests/ui/option_env_unwrap.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ macro_rules! option_env_unwrap {
44
($env: expr) => {
55
option_env!($env).unwrap()
66
};
7+
($env: expr, $message: expr) => {
8+
option_env!($env).expect($message)
9+
};
710
}
811

912
fn main() {
1013
let _ = option_env!("HOME").unwrap();
14+
let _ = option_env!("HOME").expect("environment variable HOME isn't set");
1115
let _ = option_env_unwrap!("HOME");
16+
let _ = option_env_unwrap!("HOME", "environment variable HOME isn't set");
1217
}

tests/ui/option_env_unwrap.stderr

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
error: this will panic at run-time if the environment variable doesn't exist
2-
--> $DIR/option_env_unwrap.rs:10:13
1+
error: this will panic at run-time if the environment variable doesn't exist at compile-time
2+
--> $DIR/option_env_unwrap.rs:13:13
33
|
44
LL | let _ = option_env!("HOME").unwrap();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::option-env-unwrap` implied by `-D warnings`
88
= help: consider using the `env!` macro instead
99

10-
error: this will panic at run-time if the environment variable doesn't exist
10+
error: this will panic at run-time if the environment variable doesn't exist at compile-time
11+
--> $DIR/option_env_unwrap.rs:14:13
12+
|
13+
LL | let _ = option_env!("HOME").expect("environment variable HOME isn't set");
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= help: consider using the `env!` macro instead
17+
18+
error: this will panic at run-time if the environment variable doesn't exist at compile-time
1119
--> $DIR/option_env_unwrap.rs:5:9
1220
|
1321
LL | option_env!($env).unwrap()
@@ -19,5 +27,17 @@ LL | let _ = option_env_unwrap!("HOME");
1927
= help: consider using the `env!` macro instead
2028
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
2129

22-
error: aborting due to 2 previous errors
30+
error: this will panic at run-time if the environment variable doesn't exist at compile-time
31+
--> $DIR/option_env_unwrap.rs:8:9
32+
|
33+
LL | option_env!($env).expect($message)
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
...
36+
LL | let _ = option_env_unwrap!("HOME", "environment variable HOME isn't set");
37+
| ----------------------------------------------------------------- in this macro invocation
38+
|
39+
= help: consider using the `env!` macro instead
40+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
41+
42+
error: aborting due to 4 previous errors
2343

0 commit comments

Comments
 (0)