Skip to content

Commit 15eac5a

Browse files
committed
Simplify option_env_unwrap.
1 parent ff28de2 commit 15eac5a

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

clippy_lints/src/option_env_unwrap.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::is_direct_expn_of;
33
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
44
use rustc_lint::{EarlyContext, EarlyLintPass};
55
use rustc_session::declare_lint_pass;
6-
use rustc_span::{sym, Span};
6+
use rustc_span::sym;
77

88
declare_clippy_lint! {
99
/// ### What it does
@@ -35,30 +35,18 @@ declare_lint_pass!(OptionEnvUnwrap => [OPTION_ENV_UNWRAP]);
3535

3636
impl EarlyLintPass for OptionEnvUnwrap {
3737
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
38-
fn lint(cx: &EarlyContext<'_>, span: Span) {
38+
if let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &expr.kind
39+
&& matches!(seg.ident.name, sym::expect | sym::unwrap)
40+
&& is_direct_expn_of(receiver.span, "option_env").is_some()
41+
{
3942
span_lint_and_help(
4043
cx,
4144
OPTION_ENV_UNWRAP,
42-
span,
45+
expr.span,
4346
"this will panic at run-time if the environment variable doesn't exist at compile-time",
4447
None,
4548
"consider using the `env!` macro instead",
4649
);
4750
}
48-
49-
if let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &expr.kind
50-
&& matches!(seg.ident.name, sym::expect | sym::unwrap)
51-
{
52-
if let ExprKind::Call(caller, _) = &receiver.kind &&
53-
// If it exists, it will be ::core::option::Option::Some("<env var>").unwrap() (A method call in the HIR)
54-
is_direct_expn_of(caller.span, "option_env").is_some()
55-
{
56-
lint(cx, expr.span);
57-
} else if let ExprKind::Path(_, caller) = &receiver.kind && // If it doesn't exist, it will be ::core::option::Option::None::<&'static str>.unwrap() (A path in the HIR)
58-
is_direct_expn_of(caller.span, "option_env").is_some()
59-
{
60-
lint(cx, expr.span);
61-
}
62-
}
6351
}
6452
}

0 commit comments

Comments
 (0)