-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add option-env-unwrap
lint
#5148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors
merged 4 commits into
rust-lang:master
from
krishna-veerareddy:issue-5147-option-env-unwrap
Feb 10, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
be1bc57
Add `option-env-unwrap` lint
krishna-veerareddy 0e5ba2f
Account for `expect` being used to unwrap `Option`
krishna-veerareddy a3a1587
Allow `option-env-unwrap` within external macros
krishna-veerareddy b48b221
Use `PATH` environment variable for testing
krishna-veerareddy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use crate::utils::{is_direct_expn_of, span_lint_and_help}; | ||
use if_chain::if_chain; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use syntax::ast::*; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for usage of `option_env!(...).unwrap()` and | ||
/// suggests usage of the `env!` macro. | ||
/// | ||
/// **Why is this bad?** Unwrapping the result of `option_env!` will panic | ||
/// at run-time if the environment variable doesn't exist, whereas `env!` | ||
/// catches it at compile-time. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust,no_run | ||
/// let _ = option_env!("HOME").unwrap(); | ||
/// ``` | ||
/// | ||
/// Is better expressed as: | ||
/// | ||
/// ```rust,no_run | ||
/// let _ = env!("HOME"); | ||
/// ``` | ||
pub OPTION_ENV_UNWRAP, | ||
correctness, | ||
"using `option_env!(...).unwrap()` to get environment variable" | ||
} | ||
|
||
declare_lint_pass!(OptionEnvUnwrap => [OPTION_ENV_UNWRAP]); | ||
|
||
impl EarlyLintPass for OptionEnvUnwrap { | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { | ||
if_chain! { | ||
if let ExprKind::MethodCall(path_segment, args) = &expr.kind; | ||
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 { | ||
span_lint_and_help( | ||
cx, | ||
OPTION_ENV_UNWRAP, | ||
expr.span, | ||
"this will panic at run-time if the environment variable doesn't exist at compile-time", | ||
"consider using the `env!` macro instead" | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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() | ||
}; | ||
($env: expr, $message: expr) => { | ||
option_env!($env).expect($message) | ||
}; | ||
} | ||
|
||
fn main() { | ||
let _ = option_env!("PATH").unwrap(); | ||
let _ = option_env!("PATH").expect("environment variable PATH isn't set"); | ||
let _ = option_env_unwrap!("PATH"); | ||
let _ = option_env_unwrap!("PATH", "environment variable PATH isn't set"); | ||
let _ = option_env_unwrap_external!("PATH"); | ||
let _ = option_env_unwrap_external!("PATH", "environment variable PATH isn't set"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
error: this will panic at run-time if the environment variable doesn't exist at compile-time | ||
--> $DIR/option_env_unwrap.rs:17:13 | ||
| | ||
LL | let _ = option_env!("PATH").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 at compile-time | ||
--> $DIR/option_env_unwrap.rs:18:13 | ||
| | ||
LL | let _ = option_env!("PATH").expect("environment variable PATH 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:9:9 | ||
| | ||
LL | option_env!($env).unwrap() | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
... | ||
LL | let _ = option_env_unwrap!("PATH"); | ||
| -------------------------- 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: this will panic at run-time if the environment variable doesn't exist at compile-time | ||
--> $DIR/option_env_unwrap.rs:12:9 | ||
| | ||
LL | option_env!($env).expect($message) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
... | ||
LL | let _ = option_env_unwrap!("PATH", "environment variable PATH 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: 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!("PATH"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= 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!("PATH", "environment variable PATH 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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.