1
- use crate :: utils:: { is_direct_expn_of , is_expn_of, match_function_call , paths , span_lint} ;
1
+ use crate :: utils:: { is_expn_of, match_panic_call , span_lint} ;
2
2
use if_chain:: if_chain;
3
- use rustc_ast:: ast:: LitKind ;
4
- use rustc_hir:: { Expr , ExprKind } ;
3
+ use rustc_hir:: Expr ;
5
4
use rustc_lint:: { LateContext , LateLintPass } ;
6
5
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
7
6
use rustc_span:: Span ;
8
7
9
- declare_clippy_lint ! {
10
- /// **What it does:** Checks for missing parameters in `panic!`.
11
- ///
12
- /// **Why is this bad?** Contrary to the `format!` family of macros, there are
13
- /// two forms of `panic!`: if there are no parameters given, the first argument
14
- /// is not a format string and used literally. So while `format!("{}")` will
15
- /// fail to compile, `panic!("{}")` will not.
16
- ///
17
- /// **Known problems:** None.
18
- ///
19
- /// **Example:**
20
- /// ```no_run
21
- /// panic!("This `panic!` is probably missing a parameter there: {}");
22
- /// ```
23
- pub PANIC_PARAMS ,
24
- style,
25
- "missing parameters in `panic!` calls"
26
- }
27
-
28
8
declare_clippy_lint ! {
29
9
/// **What it does:** Checks for usage of `panic!`.
30
10
///
@@ -89,31 +69,30 @@ declare_clippy_lint! {
89
69
"`unreachable!` should not be present in production code"
90
70
}
91
71
92
- declare_lint_pass ! ( PanicUnimplemented => [ PANIC_PARAMS , UNIMPLEMENTED , UNREACHABLE , TODO , PANIC ] ) ;
72
+ declare_lint_pass ! ( PanicUnimplemented => [ UNIMPLEMENTED , UNREACHABLE , TODO , PANIC ] ) ;
93
73
94
74
impl < ' tcx > LateLintPass < ' tcx > for PanicUnimplemented {
95
75
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
96
- if_chain ! {
97
- if let ExprKind :: Block ( ref block, _) = expr. kind;
98
- if let Some ( ref ex) = block. expr;
99
- if let Some ( params) = match_function_call( cx, ex, & paths:: BEGIN_PANIC )
100
- . or_else( || match_function_call( cx, ex, & paths:: BEGIN_PANIC_FMT ) ) ;
101
- then {
102
- let span = get_outer_span( expr) ;
103
- if is_expn_of( expr. span, "unimplemented" ) . is_some( ) {
104
- span_lint( cx, UNIMPLEMENTED , span,
105
- "`unimplemented` should not be present in production code" ) ;
106
- } else if is_expn_of( expr. span, "todo" ) . is_some( ) {
107
- span_lint( cx, TODO , span,
108
- "`todo` should not be present in production code" ) ;
109
- } else if is_expn_of( expr. span, "unreachable" ) . is_some( ) {
110
- span_lint( cx, UNREACHABLE , span,
111
- "`unreachable` should not be present in production code" ) ;
112
- } else if is_expn_of( expr. span, "panic" ) . is_some( ) {
113
- span_lint( cx, PANIC , span,
114
- "`panic` should not be present in production code" ) ;
115
- match_panic( params, expr, cx) ;
116
- }
76
+ if let Some ( _) = match_panic_call ( cx, expr) {
77
+ let span = get_outer_span ( expr) ;
78
+ if is_expn_of ( expr. span , "unimplemented" ) . is_some ( ) {
79
+ span_lint (
80
+ cx,
81
+ UNIMPLEMENTED ,
82
+ span,
83
+ "`unimplemented` should not be present in production code" ,
84
+ ) ;
85
+ } else if is_expn_of ( expr. span , "todo" ) . is_some ( ) {
86
+ span_lint ( cx, TODO , span, "`todo` should not be present in production code" ) ;
87
+ } else if is_expn_of ( expr. span , "unreachable" ) . is_some ( ) {
88
+ span_lint (
89
+ cx,
90
+ UNREACHABLE ,
91
+ span,
92
+ "`unreachable` should not be present in production code" ,
93
+ ) ;
94
+ } else if is_expn_of ( expr. span , "panic" ) . is_some ( ) {
95
+ span_lint ( cx, PANIC , span, "`panic` should not be present in production code" ) ;
117
96
}
118
97
}
119
98
}
@@ -132,20 +111,3 @@ fn get_outer_span(expr: &Expr<'_>) -> Span {
132
111
}
133
112
}
134
113
}
135
-
136
- fn match_panic ( params : & [ Expr < ' _ > ] , expr : & Expr < ' _ > , cx : & LateContext < ' _ > ) {
137
- if_chain ! {
138
- if let ExprKind :: Lit ( ref lit) = params[ 0 ] . kind;
139
- if is_direct_expn_of( expr. span, "panic" ) . is_some( ) ;
140
- if let LitKind :: Str ( ref string, _) = lit. node;
141
- let string = string. as_str( ) . replace( "{{" , "" ) . replace( "}}" , "" ) ;
142
- if let Some ( par) = string. find( '{' ) ;
143
- if string[ par..] . contains( '}' ) ;
144
- if params[ 0 ] . span. source_callee( ) . is_none( ) ;
145
- if params[ 0 ] . span. lo( ) != params[ 0 ] . span. hi( ) ;
146
- then {
147
- span_lint( cx, PANIC_PARAMS , params[ 0 ] . span,
148
- "you probably are missing some parameter in your format string" ) ;
149
- }
150
- }
151
- }
0 commit comments