-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathRedundantRecover.qhelp
More file actions
54 lines (48 loc) · 1.42 KB
/
RedundantRecover.qhelp
File metadata and controls
54 lines (48 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
The built-in <code>recover</code> function is only useful inside deferred
functions. Calling it in a function that is never deferred means that it will
always return <code>nil</code> and it will never regain control of a panicking
goroutine. The same is true of calling <code>recover</code> directly in a defer
statement.
</p>
</overview>
<recommendation>
<p>
Carefully inspect the code to determine whether it is a mistake that should be
fixed.
</p>
</recommendation>
<example>
<p>
In the example below, the function <code>fun1</code> is intended to recover
from the panic. However, the function that is deferred calls another function,
which then calls <code>recover</code>:
</p>
<sample src="RedundantRecover1.go" />
<p>
This problem can be fixed by deferring the call to the function which calls
<code>recover</code>:
</p>
<sample src="RedundantRecover1Good.go" />
<p>
In the following example, <code>recover</code> is called directly in a defer
statement, which has no effect, so the panic is not caught.
</p>
<sample src="RedundantRecover2.go" />
<p>
We can fix this by instead deferring an anonymous function which calls
<code>recover</code>.
</p>
<sample src="RedundantRecover2Good.go" />
</example>
<references>
<li>
<a href="https://blog.golang.org/defer-panic-and-recover">Defer, Panic, and Recover - The Go Blog</a>.
</li>
</references>
</qhelp>