Allow option-env-unwrap
within external macros
This commit is contained in:
parent
0e5ba2f0e7
commit
a3a1587a1c
@ -1,6 +1,5 @@
|
||||
use crate::utils::{is_direct_expn_of, span_lint_and_help};
|
||||
use if_chain::if_chain;
|
||||
use rustc::lint::in_external_macro;
|
||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use syntax::ast::*;
|
||||
@ -36,7 +35,6 @@ declare_lint_pass!(OptionEnvUnwrap => [OPTION_ENV_UNWRAP]);
|
||||
impl EarlyLintPass for OptionEnvUnwrap {
|
||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||
if_chain! {
|
||||
if !in_external_macro(cx.sess, expr.span);
|
||||
if let ExprKind::MethodCall(path_segment, args) = &expr.kind;
|
||||
let method_name = path_segment.ident.as_str();
|
||||
if method_name == "expect" || method_name == "unwrap";
|
||||
|
@ -46,3 +46,13 @@ macro_rules! take_external {
|
||||
std::mem::replace($s, Default::default())
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! option_env_unwrap_external {
|
||||
($env: expr) => {
|
||||
option_env!($env).unwrap()
|
||||
};
|
||||
($env: expr, $message: expr) => {
|
||||
option_env!($env).expect($message)
|
||||
};
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
// aux-build:macro_rules.rs
|
||||
#![warn(clippy::option_env_unwrap)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate macro_rules;
|
||||
|
||||
macro_rules! option_env_unwrap {
|
||||
($env: expr) => {
|
||||
option_env!($env).unwrap()
|
||||
@ -14,4 +18,6 @@ fn main() {
|
||||
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");
|
||||
let _ = option_env_unwrap_external!("HOME");
|
||||
let _ = option_env_unwrap_external!("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 at compile-time
|
||||
--> $DIR/option_env_unwrap.rs:13:13
|
||||
--> $DIR/option_env_unwrap.rs:17:13
|
||||
|
|
||||
LL | let _ = option_env!("HOME").unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -8,7 +8,7 @@ LL | let _ = option_env!("HOME").unwrap();
|
||||
= 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:14:13
|
||||
--> $DIR/option_env_unwrap.rs:18:13
|
||||
|
|
||||
LL | let _ = option_env!("HOME").expect("environment variable HOME isn't set");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -16,7 +16,7 @@ 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
|
||||
--> $DIR/option_env_unwrap.rs:9:9
|
||||
|
|
||||
LL | option_env!($env).unwrap()
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -28,7 +28,7 @@ LL | let _ = option_env_unwrap!("HOME");
|
||||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
||||
--> $DIR/option_env_unwrap.rs:8:9
|
||||
--> $DIR/option_env_unwrap.rs:12:9
|
||||
|
|
||||
LL | option_env!($env).expect($message)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -39,5 +39,23 @@ LL | let _ = option_env_unwrap!("HOME", "environment variable HOME isn't set
|
||||
= 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
|
||||
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
||||
--> $DIR/option_env_unwrap.rs:21:13
|
||||
|
|
||||
LL | let _ = option_env_unwrap_external!("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: this will panic at run-time if the environment variable doesn't exist at compile-time
|
||||
--> $DIR/option_env_unwrap.rs:22:13
|
||||
|
|
||||
LL | let _ = option_env_unwrap_external!("HOME", "environment variable HOME isn't set");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= 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 6 previous errors
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user