Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Account for expect being used to unwrap Option
  • Loading branch information
krishna-veerareddy committed Feb 9, 2020
commit 0e5ba2f0e7c1ec47ad1300e7065b97d5bc603c7c
5 changes: 3 additions & 2 deletions clippy_lints/src/option_env_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ 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 {
span_lint_and_help(
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"
);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/option_env_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
28 changes: 24 additions & 4 deletions tests/ui/option_env_unwrap.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
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();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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()
Expand All @@ -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