Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaf60e9f

Browse files
build(deps): bump github.com/sonatard/noctx from 0.1.0 to 0.3.3 (#5771)
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
1 parentfd9a0f2 commitaf60e9f

File tree

5 files changed

+188
-87
lines changed

5 files changed

+188
-87
lines changed

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ require (
100100
github.com/shirou/gopsutil/v4v4.25.4
101101
github.com/sirupsen/logrusv1.9.3
102102
github.com/sivchari/containedctxv1.0.3
103-
github.com/sonatard/noctxv0.1.0
103+
github.com/sonatard/noctxv0.3.3
104104
github.com/sourcegraph/go-diffv0.7.0
105105
github.com/spf13/cobrav1.9.1
106106
github.com/spf13/pflagv1.0.6

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎pkg/golinters/noctx/noctx.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func New() *goanalysis.Linter {
1212

1313
returngoanalysis.NewLinter(
1414
a.Name,
15-
"Finds sending http request without context.Context",
15+
"Detects function and method with missing usage of context.Context",
1616
[]*analysis.Analyzer{a},
1717
nil,
1818
).WithLoadMode(goanalysis.LoadModeTypesInfo)

‎pkg/golinters/noctx/testdata/noctx.go‎

Lines changed: 92 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,96 +3,81 @@ package testdata
33

44
import (
55
"context"
6+
"database/sql"
67
"net/http"
78
)
89

910
varnewRequestPkg=http.NewRequest
1011

11-
funcNoctx() {
12-
consturl="http://example.com"
12+
func_() {
13+
consturl="https://example.com"
14+
1315
cli:=&http.Client{}
1416

1517
ctx:=context.Background()
16-
http.Get(url)// want `net/http\.Get must not be called`
17-
_=http.Get// OK
18-
f:=http.Get// OK
19-
f(url)// want `net/http\.Get must not be called`
20-
21-
http.Head(url)// want `net/http\.Head must not be called`
22-
http.Post(url,"",nil)// want `net/http\.Post must not be called`
23-
http.PostForm(url,nil)// want `net/http\.PostForm must not be called`
24-
25-
cli.Get(url)// want `\(\*net/http\.Client\)\.Get must not be called`
26-
_=cli.Get// OK
27-
m:=cli.Get// OK
28-
m(url)// want `\(\*net/http\.Client\)\.Get must not be called`
29-
30-
cli.Head(url)// want `\(\*net/http\.Client\)\.Head must not be called`
31-
cli.Post(url,"",nil)// want `\(\*net/http\.Client\)\.Post must not be called`
32-
cli.PostForm(url,nil)// want `\(\*net/http\.Client\)\.PostForm must not be called`
3318

34-
req,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
19+
req,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
3520
cli.Do(req)
3621

3722
req2,_:=http.NewRequestWithContext(ctx,http.MethodPost,url,nil)// OK
3823
cli.Do(req2)
3924

40-
req3,_:=http.NewRequest(http.MethodPost,url,nil)//OK
25+
req3,_:=http.NewRequest(http.MethodPost,url,nil)//want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
4126
req3=req3.WithContext(ctx)
4227
cli.Do(req3)
4328

4429
f2:=func(req*http.Request,ctx context.Context)*http.Request {
4530
returnreq
4631
}
47-
req4,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
32+
req4,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
4833
req4=f2(req4,ctx)
4934

50-
req41,_:=http.NewRequest(http.MethodPost,url,nil)//OK
35+
req41,_:=http.NewRequest(http.MethodPost,url,nil)//want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
5136
req41=req41.WithContext(ctx)
5237
req41=f2(req41,ctx)
5338

5439
newRequest:=http.NewRequest
55-
req5,_:=newRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
40+
req5,_:=newRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
5641
cli.Do(req5)
5742

58-
req51,_:=newRequest(http.MethodPost,url,nil)//OK
43+
req51,_:=newRequest(http.MethodPost,url,nil)//want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
5944
req51=req51.WithContext(ctx)
6045
cli.Do(req51)
6146

62-
req52,_:=newRequestPkg(http.MethodPost,url,nil)//want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
47+
req52,_:=newRequestPkg(http.MethodPost,url,nil)//TODO: false negative `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
6348
cli.Do(req52)
6449

6550
typeMyRequest= http.Request
6651
f3:=func(req*MyRequest,ctx context.Context)*MyRequest {
6752
returnreq
6853
}
69-
req6,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
54+
req6,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
7055
req6=f3(req6,ctx)
7156

72-
req61,_:=http.NewRequest(http.MethodPost,url,nil)//OK
57+
req61,_:=http.NewRequest(http.MethodPost,url,nil)//want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
7358
req61=req61.WithContext(ctx)
7459
req61=f3(req61,ctx)
7560

7661
typeMyRequest2 http.Request
7762
f4:=func(req*MyRequest2,ctx context.Context)*MyRequest2 {
7863
returnreq
7964
}
80-
req7,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
65+
req7,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
8166
req71:=MyRequest2(*req7)
8267
f4(&req71,ctx)
8368

84-
req72,_:=http.NewRequest(http.MethodPost,url,nil)//OK
69+
req72,_:=http.NewRequest(http.MethodPost,url,nil)//want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
8570
req72=req72.WithContext(ctx)
8671
req73:=MyRequest2(*req7)
8772
f4(&req73,ctx)
8873

8974
req8,_:=func() (*http.Request,error) {
90-
returnhttp.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
75+
returnhttp.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
9176
}()
9277
cli.Do(req8)
9378

9479
req82,_:=func() (*http.Request,error) {
95-
req82,_:=http.NewRequest(http.MethodPost,url,nil)//OK
80+
req82,_:=http.NewRequest(http.MethodPost,url,nil)//want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
9681
req82=req82.WithContext(ctx)
9782
returnreq82,nil
9883
}()
@@ -101,34 +86,99 @@ func Noctx() {
10186
f5:=func(req,req2*http.Request,ctx context.Context) (*http.Request,*http.Request) {
10287
returnreq,req2
10388
}
104-
req9,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
89+
req9,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
10590
req9,_=f5(req9,req9,ctx)
10691

107-
req91,_:=http.NewRequest(http.MethodPost,url,nil)//OK
92+
req91,_:=http.NewRequest(http.MethodPost,url,nil)//want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
10893
req91=req91.WithContext(ctx)
10994
req9,_=f5(req91,req91,ctx)
11095

111-
req10,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
112-
req11,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
96+
req10,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
97+
req11,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
11398
req10,req11=f5(req10,req11,ctx)
11499

115-
req101,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
116-
req111,_:=http.NewRequest(http.MethodPost,url,nil)//OK
100+
req101,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
101+
req111,_:=http.NewRequest(http.MethodPost,url,nil)//want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
117102
req111=req111.WithContext(ctx)
118103
req101,req111=f5(req101,req111,ctx)
119104

120105
func() (*http.Request,*http.Request) {
121-
req12,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
122-
req13,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
106+
req12,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
107+
req13,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
123108
returnreq12,req13
124109
}()
125110

126111
func() (*http.Request,*http.Request) {
127-
req14,_:=http.NewRequest(http.MethodPost,url,nil)// want `should rewritehttp.NewRequestWithContext or add \(\*Request\).WithContext`
128-
req15,_:=http.NewRequest(http.MethodPost,url,nil)//OK
112+
req14,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
113+
req15,_:=http.NewRequest(http.MethodPost,url,nil)//want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
129114
req15=req15.WithContext(ctx)
130115

131116
returnreq14,req15
132117
}()
118+
119+
req121,_:=http.NewRequest(http.MethodPost,url,nil)// want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
120+
req121.AddCookie(&http.Cookie{Name:"k",Value:"v"})
121+
req121=req121.WithContext(context.WithValue(req121.Context(),struct{}{},0))
122+
cli.Do(req121)
123+
}
124+
125+
func_() {
126+
consturl="http://example.com"
127+
cli:=&http.Client{}
128+
129+
http.Get(url)// want `net/http\.Get must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
130+
_=http.Get// OK
131+
f:=http.Get// OK
132+
f(url)// want `net/http\.Get must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
133+
134+
http.Head(url)// want `net/http\.Head must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
135+
http.Post(url,"",nil)// want `net/http\.Post must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
136+
http.PostForm(url,nil)// want `net/http\.PostForm must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
137+
138+
cli.Get(url)// want `\(\*net/http\.Client\)\.Get must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
139+
_=cli.Get// OK
140+
m:=cli.Get// OK
141+
m(url)// want `\(\*net/http\.Client\)\.Get must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
142+
143+
cli.Head(url)// want `\(\*net/http\.Client\)\.Head must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
144+
cli.Post(url,"",nil)// want `\(\*net/http\.Client\)\.Post must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
145+
cli.PostForm(url,nil)// want `\(\*net/http\.Client\)\.PostForm must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
133146
}
134147

148+
func_() {
149+
ctx:=context.Background()
150+
151+
db,_:=sql.Open("noctx","noctx://")
152+
153+
db.Exec("select * from testdata")// want `\(\*database/sql\.DB\)\.Exec must not be called. use \(\*database/sql\.DB\)\.ExecContext`
154+
db.ExecContext(ctx,"select * from testdata")
155+
156+
db.Ping()// want `\(\*database/sql\.DB\)\.Ping must not be called. use \(\*database/sql\.DB\)\.PingContext`
157+
db.PingContext(ctx)
158+
159+
db.Prepare("select * from testdata")// want `\(\*database/sql\.DB\)\.Prepare must not be called. use \(\*database/sql\.DB\)\.PrepareContext`
160+
db.PrepareContext(ctx,"select * from testdata")
161+
162+
db.Query("select * from testdata")// want `\(\*database/sql\.DB\)\.Query must not be called. use \(\*database/sql\.DB\)\.QueryContext`
163+
db.QueryContext(ctx,"select * from testdata")
164+
165+
db.QueryRow("select * from testdata")// want `\(\*database/sql\.DB\)\.QueryRow must not be called. use \(\*database/sql\.DB\)\.QueryRowContext`
166+
db.QueryRowContext(ctx,"select * from testdata")
167+
168+
// transactions
169+
170+
tx,_:=db.Begin()
171+
tx.Exec("select * from testdata")// want `\(\*database/sql\.Tx\)\.Exec must not be called. use \(\*database/sql\.Tx\)\.ExecContext`
172+
tx.ExecContext(ctx,"select * from testdata")
173+
174+
tx.Prepare("select * from testdata")// want `\(\*database/sql\.Tx\)\.Prepare must not be called. use \(\*database/sql\.Tx\)\.PrepareContext`
175+
tx.PrepareContext(ctx,"select * from testdata")
176+
177+
tx.Query("select * from testdata")// want `\(\*database/sql\.Tx\)\.Query must not be called. use \(\*database/sql\.Tx\)\.QueryContext`
178+
tx.QueryContext(ctx,"select * from testdata")
179+
180+
tx.QueryRow("select * from testdata")// want `\(\*database/sql\.Tx\)\.QueryRow must not be called. use \(\*database/sql\.Tx\)\.QueryRowContext`
181+
tx.QueryRowContext(ctx,"select * from testdata")
182+
183+
_=tx.Commit()
184+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp