Skip to content

Commit a3a1587

Browse files
Allow option-env-unwrap within external macros
1 parent 0e5ba2f commit a3a1587

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

clippy_lints/src/option_env_unwrap.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::utils::{is_direct_expn_of, span_lint_and_help};
22
use if_chain::if_chain;
3-
use rustc::lint::in_external_macro;
43
use rustc_lint::{EarlyContext, EarlyLintPass};
54
use rustc_session::{declare_lint_pass, declare_tool_lint};
65
use syntax::ast::*;
@@ -36,7 +35,6 @@ declare_lint_pass!(OptionEnvUnwrap => [OPTION_ENV_UNWRAP]);
3635
impl EarlyLintPass for OptionEnvUnwrap {
3736
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
3837
if_chain! {
39-
if !in_external_macro(cx.sess, expr.span);
4038
if let ExprKind::MethodCall(path_segment, args) = &expr.kind;
4139
let method_name = path_segment.ident.as_str();
4240
if method_name == "expect" || method_name == "unwrap";

tests/ui/auxiliary/macro_rules.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ macro_rules! take_external {
4646
std::mem::replace($s, Default::default())
4747
};
4848
}
49+
50+
#[macro_export]
51+
macro_rules! option_env_unwrap_external {
52+
($env: expr) => {
53+
option_env!($env).unwrap()
54+
};
55+
($env: expr, $message: expr) => {
56+
option_env!($env).expect($message)
57+
};
58+
}

tests/ui/option_env_unwrap.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// aux-build:macro_rules.rs
12
#![warn(clippy::option_env_unwrap)]
23

4+
#[macro_use]
5+
extern crate macro_rules;
6+
37
macro_rules! option_env_unwrap {
48
($env: expr) => {
59
option_env!($env).unwrap()
@@ -14,4 +18,6 @@ fn main() {
1418
let _ = option_env!("HOME").expect("environment variable HOME isn't set");
1519
let _ = option_env_unwrap!("HOME");
1620
let _ = option_env_unwrap!("HOME", "environment variable HOME isn't set");
21+
let _ = option_env_unwrap_external!("HOME");
22+
let _ = option_env_unwrap_external!("HOME", "environment variable HOME isn't set");
1723
}

tests/ui/option_env_unwrap.stderr

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this will panic at run-time if the environment variable doesn't exist at compile-time
2-
--> $DIR/option_env_unwrap.rs:13:13
2+
--> $DIR/option_env_unwrap.rs:17:13
33
|
44
LL | let _ = option_env!("HOME").unwrap();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,15 +8,15 @@ LL | let _ = option_env!("HOME").unwrap();
88
= help: consider using the `env!` macro instead
99

1010
error: this will panic at run-time if the environment variable doesn't exist at compile-time
11-
--> $DIR/option_env_unwrap.rs:14:13
11+
--> $DIR/option_env_unwrap.rs:18:13
1212
|
1313
LL | let _ = option_env!("HOME").expect("environment variable HOME isn't set");
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
1616
= help: consider using the `env!` macro instead
1717

1818
error: this will panic at run-time if the environment variable doesn't exist at compile-time
19-
--> $DIR/option_env_unwrap.rs:5:9
19+
--> $DIR/option_env_unwrap.rs:9:9
2020
|
2121
LL | option_env!($env).unwrap()
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -28,7 +28,7 @@ LL | let _ = option_env_unwrap!("HOME");
2828
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
2929

3030
error: this will panic at run-time if the environment variable doesn't exist at compile-time
31-
--> $DIR/option_env_unwrap.rs:8:9
31+
--> $DIR/option_env_unwrap.rs:12:9
3232
|
3333
LL | option_env!($env).expect($message)
3434
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -39,5 +39,23 @@ LL | let _ = option_env_unwrap!("HOME", "environment variable HOME isn't set
3939
= help: consider using the `env!` macro instead
4040
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
4141

42-
error: aborting due to 4 previous errors
42+
error: this will panic at run-time if the environment variable doesn't exist at compile-time
43+
--> $DIR/option_env_unwrap.rs:21:13
44+
|
45+
LL | let _ = option_env_unwrap_external!("HOME");
46+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
|
48+
= help: consider using the `env!` macro instead
49+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
50+
51+
error: this will panic at run-time if the environment variable doesn't exist at compile-time
52+
--> $DIR/option_env_unwrap.rs:22:13
53+
|
54+
LL | let _ = option_env_unwrap_external!("HOME", "environment variable HOME isn't set");
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
|
57+
= help: consider using the `env!` macro instead
58+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
59+
60+
error: aborting due to 6 previous errors
4361

0 commit comments

Comments
 (0)