Skip to content

Commit be1bc57

Browse files
Add option-env-unwrap lint
1 parent 4ad6fb3 commit be1bc57

File tree

7 files changed

+105
-2
lines changed

7 files changed

+105
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,7 @@ Released 2018-09-13
12761276
[`op_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#op_ref
12771277
[`option_and_then_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_and_then_some
12781278
[`option_as_ref_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_as_ref_deref
1279+
[`option_env_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_env_unwrap
12791280
[`option_expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_expect_used
12801281
[`option_map_or_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_or_none
12811282
[`option_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 354 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 355 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ pub mod no_effect;
267267
pub mod non_copy_const;
268268
pub mod non_expressive_names;
269269
pub mod open_options;
270+
pub mod option_env_unwrap;
270271
pub mod overflow_check_conditional;
271272
pub mod panic_unimplemented;
272273
pub mod partialeq_ne_impl;
@@ -713,6 +714,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
713714
&non_expressive_names::MANY_SINGLE_CHAR_NAMES,
714715
&non_expressive_names::SIMILAR_NAMES,
715716
&open_options::NONSENSICAL_OPEN_OPTIONS,
717+
&option_env_unwrap::OPTION_ENV_UNWRAP,
716718
&overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL,
717719
&panic_unimplemented::PANIC,
718720
&panic_unimplemented::PANIC_PARAMS,
@@ -1003,6 +1005,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10031005
let max_fn_params_bools = conf.max_fn_params_bools;
10041006
let max_struct_bools = conf.max_struct_bools;
10051007
store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools));
1008+
store.register_early_pass(|| box option_env_unwrap::OptionEnvUnwrap);
10061009

10071010
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10081011
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1285,6 +1288,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12851288
LintId::of(&non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
12861289
LintId::of(&non_expressive_names::MANY_SINGLE_CHAR_NAMES),
12871290
LintId::of(&open_options::NONSENSICAL_OPEN_OPTIONS),
1291+
LintId::of(&option_env_unwrap::OPTION_ENV_UNWRAP),
12881292
LintId::of(&overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
12891293
LintId::of(&panic_unimplemented::PANIC_PARAMS),
12901294
LintId::of(&partialeq_ne_impl::PARTIALEQ_NE_IMPL),
@@ -1590,6 +1594,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15901594
LintId::of(&non_copy_const::BORROW_INTERIOR_MUTABLE_CONST),
15911595
LintId::of(&non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST),
15921596
LintId::of(&open_options::NONSENSICAL_OPEN_OPTIONS),
1597+
LintId::of(&option_env_unwrap::OPTION_ENV_UNWRAP),
15931598
LintId::of(&ptr::MUT_FROM_REF),
15941599
LintId::of(&regex::INVALID_REGEX),
15951600
LintId::of(&serde_api::SERDE_API_MISUSE),

clippy_lints/src/option_env_unwrap.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use crate::utils::{is_direct_expn_of, span_lint_and_help};
2+
use if_chain::if_chain;
3+
use rustc::lint::in_external_macro;
4+
use rustc_lint::{EarlyContext, EarlyLintPass};
5+
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
use syntax::ast::*;
7+
8+
declare_clippy_lint! {
9+
/// **What it does:** Checks for usage of `option_env!(...).unwrap()` and
10+
/// suggests usage of the `env!` macro.
11+
///
12+
/// **Why is this bad?** Unwrapping the result of `option_env!` will panic
13+
/// at run-time if the environment variable doesn't exist, whereas `env!`
14+
/// catches it at compile-time.
15+
///
16+
/// **Known problems:** None.
17+
///
18+
/// **Example:**
19+
///
20+
/// ```rust,no_run
21+
/// let _ = option_env!("HOME").unwrap();
22+
/// ```
23+
///
24+
/// Is better expressed as:
25+
///
26+
/// ```rust,no_run
27+
/// let _ = env!("HOME");
28+
/// ```
29+
pub OPTION_ENV_UNWRAP,
30+
correctness,
31+
"using `option_env!(...).unwrap()` to get environment variable"
32+
}
33+
34+
declare_lint_pass!(OptionEnvUnwrap => [OPTION_ENV_UNWRAP]);
35+
36+
impl EarlyLintPass for OptionEnvUnwrap {
37+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
38+
if_chain! {
39+
if !in_external_macro(cx.sess, expr.span);
40+
if let ExprKind::MethodCall(path_segment, args) = &expr.kind;
41+
if path_segment.ident.as_str() == "unwrap";
42+
if let ExprKind::Call(caller, _) = &args[0].kind;
43+
if is_direct_expn_of(caller.span, "option_env").is_some();
44+
then {
45+
span_lint_and_help(
46+
cx,
47+
OPTION_ENV_UNWRAP,
48+
expr.span,
49+
"this will panic at run-time if the environment variable doesn't exist",
50+
"consider using the `env!` macro instead"
51+
);
52+
}
53+
}
54+
}
55+
}

src/lintlist/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 354] = [
9+
pub const ALL_LINTS: [Lint; 355] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1498,6 +1498,13 @@ pub const ALL_LINTS: [Lint; 354] = [
14981498
deprecation: None,
14991499
module: "methods",
15001500
},
1501+
Lint {
1502+
name: "option_env_unwrap",
1503+
group: "correctness",
1504+
desc: "using `option_env!(...).unwrap()` to get environment variable",
1505+
deprecation: None,
1506+
module: "option_env_unwrap",
1507+
},
15011508
Lint {
15021509
name: "option_expect_used",
15031510
group: "restriction",

tests/ui/option_env_unwrap.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![warn(clippy::option_env_unwrap)]
2+
3+
macro_rules! option_env_unwrap {
4+
($env: expr) => {
5+
option_env!($env).unwrap()
6+
};
7+
}
8+
9+
fn main() {
10+
let _ = option_env!("HOME").unwrap();
11+
let _ = option_env_unwrap!("HOME");
12+
}

tests/ui/option_env_unwrap.stderr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: this will panic at run-time if the environment variable doesn't exist
2+
--> $DIR/option_env_unwrap.rs:10:13
3+
|
4+
LL | let _ = option_env!("HOME").unwrap();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::option-env-unwrap` implied by `-D warnings`
8+
= help: consider using the `env!` macro instead
9+
10+
error: this will panic at run-time if the environment variable doesn't exist
11+
--> $DIR/option_env_unwrap.rs:5:9
12+
|
13+
LL | option_env!($env).unwrap()
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
...
16+
LL | let _ = option_env_unwrap!("HOME");
17+
| -------------------------- in this macro invocation
18+
|
19+
= help: consider using the `env!` macro instead
20+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
21+
22+
error: aborting due to 2 previous errors
23+

0 commit comments

Comments
 (0)