Skip to content

Commit

Permalink
Allow option-env-unwrap within external macros
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna-veerareddy committed Feb 9, 2020
1 parent 0e5ba2f commit a3a1587
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
2 changes: 0 additions & 2 deletions clippy_lints/src/option_env_unwrap.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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";
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/auxiliary/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}
6 changes: 6 additions & 0 deletions tests/ui/option_env_unwrap.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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");
}
28 changes: 23 additions & 5 deletions tests/ui/option_env_unwrap.stderr
Original file line number Diff line number Diff line change
@@ -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();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -8,15 +8,15 @@ 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");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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

0 comments on commit a3a1587

Please sign in to comment.