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
Allow option-env-unwrap within external macros
  • Loading branch information
krishna-veerareddy committed Feb 9, 2020
commit a3a1587a1c5b7df413729e34923ada495f3fff94
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