@@ -3,7 +3,7 @@ use clippy_utils::is_direct_expn_of;
3
3
use rustc_ast:: ast:: { Expr , ExprKind , MethodCall } ;
4
4
use rustc_lint:: { EarlyContext , EarlyLintPass } ;
5
5
use rustc_session:: declare_lint_pass;
6
- use rustc_span:: { sym, Span } ;
6
+ use rustc_span:: sym;
7
7
8
8
declare_clippy_lint ! {
9
9
/// ### What it does
@@ -35,30 +35,18 @@ declare_lint_pass!(OptionEnvUnwrap => [OPTION_ENV_UNWRAP]);
35
35
36
36
impl EarlyLintPass for OptionEnvUnwrap {
37
37
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
+ {
39
42
span_lint_and_help (
40
43
cx,
41
44
OPTION_ENV_UNWRAP ,
42
- span,
45
+ expr . span ,
43
46
"this will panic at run-time if the environment variable doesn't exist at compile-time" ,
44
47
None ,
45
48
"consider using the `env!` macro instead" ,
46
49
) ;
47
50
}
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
- }
63
51
}
64
52
}
0 commit comments