diff --git a/clippy_lints/src/option_env_unwrap.rs b/clippy_lints/src/option_env_unwrap.rs index e0e8a95acd5..e84a7dd2db0 100644 --- a/clippy_lints/src/option_env_unwrap.rs +++ b/clippy_lints/src/option_env_unwrap.rs @@ -38,7 +38,8 @@ impl EarlyLintPass for OptionEnvUnwrap { if_chain! { if !in_external_macro(cx.sess, expr.span); if let ExprKind::MethodCall(path_segment, args) = &expr.kind; - if path_segment.ident.as_str() == "unwrap"; + let method_name = path_segment.ident.as_str(); + if method_name == "expect" || method_name == "unwrap"; if let ExprKind::Call(caller, _) = &args[0].kind; if is_direct_expn_of(caller.span, "option_env").is_some(); then { @@ -46,7 +47,7 @@ impl EarlyLintPass for OptionEnvUnwrap { cx, OPTION_ENV_UNWRAP, expr.span, - "this will panic at run-time if the environment variable doesn't exist", + "this will panic at run-time if the environment variable doesn't exist at compile-time", "consider using the `env!` macro instead" ); } diff --git a/tests/ui/option_env_unwrap.rs b/tests/ui/option_env_unwrap.rs index 2830669ff66..c8488da505c 100644 --- a/tests/ui/option_env_unwrap.rs +++ b/tests/ui/option_env_unwrap.rs @@ -4,9 +4,14 @@ macro_rules! option_env_unwrap { ($env: expr) => { option_env!($env).unwrap() }; + ($env: expr, $message: expr) => { + option_env!($env).expect($message) + }; } fn main() { let _ = option_env!("HOME").unwrap(); + let _ = option_env!("HOME").expect("environment variable HOME isn't set"); let _ = option_env_unwrap!("HOME"); + let _ = option_env_unwrap!("HOME", "environment variable HOME isn't set"); } diff --git a/tests/ui/option_env_unwrap.stderr b/tests/ui/option_env_unwrap.stderr index b7d5ecd6321..2230cc3d859 100644 --- a/tests/ui/option_env_unwrap.stderr +++ b/tests/ui/option_env_unwrap.stderr @@ -1,5 +1,5 @@ -error: this will panic at run-time if the environment variable doesn't exist - --> $DIR/option_env_unwrap.rs:10:13 +error: this will panic at run-time if the environment variable doesn't exist at compile-time + --> $DIR/option_env_unwrap.rs:13:13 | LL | let _ = option_env!("HOME").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -7,7 +7,15 @@ LL | let _ = option_env!("HOME").unwrap(); = note: `-D clippy::option-env-unwrap` implied by `-D warnings` = help: consider using the `env!` macro instead -error: this will panic at run-time if the environment variable doesn't exist +error: this will panic at run-time if the environment variable doesn't exist at compile-time + --> $DIR/option_env_unwrap.rs:14:13 + | +LL | let _ = option_env!("HOME").expect("environment variable HOME isn't set"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider using the `env!` macro instead + +error: this will panic at run-time if the environment variable doesn't exist at compile-time --> $DIR/option_env_unwrap.rs:5:9 | LL | option_env!($env).unwrap() @@ -19,5 +27,17 @@ LL | let _ = option_env_unwrap!("HOME"); = help: consider using the `env!` macro instead = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 2 previous errors +error: this will panic at run-time if the environment variable doesn't exist at compile-time + --> $DIR/option_env_unwrap.rs:8:9 + | +LL | option_env!($env).expect($message) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | let _ = option_env_unwrap!("HOME", "environment variable HOME isn't set"); + | ----------------------------------------------------------------- in this macro invocation + | + = help: consider using the `env!` macro instead + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 4 previous errors