mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 15:14:29 +00:00
Account for expect
being used to unwrap Option
This commit is contained in:
parent
be1bc571c3
commit
0e5ba2f0e7
3 changed files with 32 additions and 6 deletions
|
@ -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"
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue