From 0e5ba2f0e7c1ec47ad1300e7065b97d5bc603c7c Mon Sep 17 00:00:00 2001 From: Krishna Sai Veera Reddy Date: Sun, 9 Feb 2020 08:54:23 -0800 Subject: [PATCH] Account for `expect` being used to unwrap `Option` --- clippy_lints/src/option_env_unwrap.rs | 5 +++-- tests/ui/option_env_unwrap.rs | 5 +++++ tests/ui/option_env_unwrap.stderr | 28 +++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/option_env_unwrap.rs b/clippy_lints/src/option_env_unwrap.rs index e0e8a95ac..e84a7dd2d 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 2830669ff..c8488da50 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 b7d5ecd63..2230cc3d8 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