1
1
use clippy_utils:: { diagnostics:: span_lint_and_then, match_def_path, paths, source:: snippet} ;
2
2
use rustc_errors:: Applicability ;
3
- use rustc_hir:: * ;
3
+ use rustc_hir:: { Expr , ExprKind , PatKind , Stmt , StmtKind , UnOp } ;
4
4
use rustc_lint:: LateContext ;
5
5
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
6
6
use rustc_span:: { symbol:: Ident , Span } ;
@@ -36,13 +36,7 @@ declare_clippy_lint! {
36
36
}
37
37
declare_lint_pass ! ( WhilePopUnwrap => [ WHILE_POP_UNWRAP ] ) ;
38
38
39
- fn report_lint < ' tcx > (
40
- cx : & LateContext < ' tcx > ,
41
- pop_span : Span ,
42
- ident : Option < Ident > ,
43
- loop_span : Span ,
44
- receiver_span : Span ,
45
- ) {
39
+ fn report_lint ( cx : & LateContext < ' _ > , pop_span : Span , ident : Option < Ident > , loop_span : Span , receiver_span : Span ) {
46
40
span_lint_and_then (
47
41
cx,
48
42
WHILE_POP_UNWRAP ,
@@ -54,7 +48,7 @@ fn report_lint<'tcx>(
54
48
"try" ,
55
49
format ! (
56
50
"while let Some({}) = {}.pop()" ,
57
- ident. as_ref( ) . map ( Ident :: as_str) . unwrap_or ( "element" ) ,
51
+ ident. as_ref( ) . map_or ( "element" , Ident :: as_str) ,
58
52
snippet( cx, receiver_span, ".." )
59
53
) ,
60
54
Applicability :: MaybeIncorrect ,
@@ -75,11 +69,11 @@ fn match_method_call(cx: &LateContext<'_>, expr: &Expr<'_>, method: &[&str]) ->
75
69
}
76
70
}
77
71
78
- fn is_vec_pop < ' tcx > ( cx : & LateContext < ' tcx > , expr : & Expr < ' _ > ) -> bool {
72
+ fn is_vec_pop ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
79
73
match_method_call ( cx, expr, & paths:: VEC_POP )
80
74
}
81
75
82
- fn is_vec_pop_unwrap < ' tcx > ( cx : & LateContext < ' tcx > , expr : & Expr < ' _ > ) -> bool {
76
+ fn is_vec_pop_unwrap ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
83
77
if let ExprKind :: MethodCall ( _, inner, ..) = expr. kind
84
78
&& ( match_method_call ( cx, expr, & paths:: OPTION_UNWRAP ) || match_method_call ( cx, expr, & paths:: OPTION_EXPECT ) )
85
79
&& is_vec_pop ( cx, inner)
@@ -90,7 +84,7 @@ fn is_vec_pop_unwrap<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> bool {
90
84
}
91
85
}
92
86
93
- fn check_local < ' tcx > ( cx : & LateContext < ' tcx > , stmt : & Stmt < ' _ > , loop_span : Span , recv_span : Span ) {
87
+ fn check_local ( cx : & LateContext < ' _ > , stmt : & Stmt < ' _ > , loop_span : Span , recv_span : Span ) {
94
88
if let StmtKind :: Local ( local) = stmt. kind
95
89
&& let PatKind :: Binding ( .., ident, _) = local. pat . kind
96
90
&& let Some ( init) = local. init
@@ -101,10 +95,12 @@ fn check_local<'tcx>(cx: &LateContext<'tcx>, stmt: &Stmt<'_>, loop_span: Span, r
101
95
}
102
96
}
103
97
104
- fn check_call_arguments < ' tcx > ( cx : & LateContext < ' tcx > , stmt : & Stmt < ' _ > , loop_span : Span , recv_span : Span ) {
98
+ fn check_call_arguments ( cx : & LateContext < ' _ > , stmt : & Stmt < ' _ > , loop_span : Span , recv_span : Span ) {
105
99
if let StmtKind :: Semi ( expr) | StmtKind :: Expr ( expr) = stmt. kind {
106
100
if let ExprKind :: MethodCall ( _, _, args, _) | ExprKind :: Call ( _, args) = expr. kind {
107
- let offending_arg = args. iter ( ) . find_map ( |arg| is_vec_pop_unwrap ( cx, arg) . then ( || arg. span ) ) ;
101
+ let offending_arg = args
102
+ . iter ( )
103
+ . find_map ( |arg| is_vec_pop_unwrap ( cx, arg) . then_some ( arg. span ) ) ;
108
104
109
105
if let Some ( offending_arg) = offending_arg {
110
106
report_lint ( cx, offending_arg, None , loop_span, recv_span) ;
0 commit comments