Skip to content

Commit c207b27

Browse files
authored
lint: Improve extraneousnew linter to catch unnecessary use of value var (#4249)
1 parent 6ac8fe4 commit c207b27

11 files changed

Lines changed: 242 additions & 28 deletions

File tree

github/admin_users.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ func (s *AdminService) CreateUser(ctx context.Context, userReq CreateUserRequest
3131
return nil, nil, err
3232
}
3333

34-
var user User
34+
var user *User
3535
resp, err := s.client.Do(req, &user)
3636
if err != nil {
3737
return nil, resp, err
3838
}
3939

40-
return &user, resp, nil
40+
return user, resp, nil
4141
}
4242

4343
// DeleteUser deletes a user in GitHub Enterprise.

github/git_commits_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,14 @@ Commit Message.`
315315
Parents: []*Commit{{SHA: Ptr("p")}},
316316
Author: &author,
317317
}
318-
wantBody := createCommit{
318+
wantBody := &createCommit{
319319
Message: input.Message,
320320
Tree: Ptr("t"),
321321
Parents: []string{"p"},
322322
Author: &author,
323323
Signature: &signature,
324324
}
325-
var gotBody createCommit
325+
var gotBody *createCommit
326326
mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) {
327327
assertNilError(t, json.NewDecoder(r.Body).Decode(&gotBody))
328328
testMethod(t, r, "POST")

github/issue_import.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,20 @@ func (s *IssueImportService) Create(ctx context.Context, owner, repo string, iss
8383

8484
req.Header.Set("Accept", mediaTypeIssueImportAPI)
8585

86-
var i IssueImportResponse
86+
var i *IssueImportResponse
8787
resp, err := s.client.Do(req, &i)
8888
if err != nil {
8989
var aerr *AcceptedError
9090
if errors.As(err, &aerr) {
9191
if err := json.Unmarshal(aerr.Raw, &i); err != nil {
92-
return &i, resp, err
92+
return i, resp, err
9393
}
94-
return &i, resp, err
94+
return i, resp, err
9595
}
9696
return nil, resp, err
9797
}
9898

99-
return &i, resp, nil
99+
return i, resp, nil
100100
}
101101

102102
// CheckStatus checks the status of an imported issue.

github/private_registries.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ func (s *PrivateRegistriesService) ListOrganizationPrivateRegistries(ctx context
249249
return nil, nil, err
250250
}
251251

252-
var privateRegistries PrivateRegistries
252+
var privateRegistries *PrivateRegistries
253253
resp, err := s.client.Do(req, &privateRegistries)
254254
if err != nil {
255255
return nil, resp, err
256256
}
257-
return &privateRegistries, resp, nil
257+
return privateRegistries, resp, nil
258258
}
259259

260260
// CreateOrganizationPrivateRegistry creates a private registry configuration with an encrypted value for an organization.
@@ -270,12 +270,12 @@ func (s *PrivateRegistriesService) CreateOrganizationPrivateRegistry(ctx context
270270
return nil, nil, err
271271
}
272272

273-
var result PrivateRegistry
273+
var result *PrivateRegistry
274274
resp, err := s.client.Do(req, &result)
275275
if err != nil {
276276
return nil, resp, err
277277
}
278-
return &result, resp, nil
278+
return result, resp, nil
279279
}
280280

281281
// GetOrganizationPrivateRegistriesPublicKey retrieves the public key for encrypting secrets for an organization's private registries.
@@ -291,12 +291,12 @@ func (s *PrivateRegistriesService) GetOrganizationPrivateRegistriesPublicKey(ctx
291291
return nil, nil, err
292292
}
293293

294-
var publicKey PublicKey
294+
var publicKey *PublicKey
295295
resp, err := s.client.Do(req, &publicKey)
296296
if err != nil {
297297
return nil, resp, err
298298
}
299-
return &publicKey, resp, nil
299+
return publicKey, resp, nil
300300
}
301301

302302
// GetOrganizationPrivateRegistry gets a specific private registry for an organization.
@@ -313,13 +313,13 @@ func (s *PrivateRegistriesService) GetOrganizationPrivateRegistry(ctx context.Co
313313
return nil, nil, err
314314
}
315315

316-
var privateRegistry PrivateRegistry
316+
var privateRegistry *PrivateRegistry
317317
resp, err := s.client.Do(req, &privateRegistry)
318318
if err != nil {
319319
return nil, resp, err
320320
}
321321

322-
return &privateRegistry, resp, nil
322+
return privateRegistry, resp, nil
323323
}
324324

325325
// UpdateOrganizationPrivateRegistry updates a specific private registry for an organization.

github/repos_forks.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,20 @@ func (s *RepositoriesService) CreateFork(ctx context.Context, owner, repo string
7979
return nil, nil, err
8080
}
8181

82-
var fork Repository
82+
var fork *Repository
8383
resp, err := s.client.Do(req, &fork)
8484
if err != nil {
8585
// Persist AcceptedError's metadata to the Repository object.
8686
var aerr *AcceptedError
8787
if errors.As(err, &aerr) {
8888
if err := json.Unmarshal(aerr.Raw, &fork); err != nil {
89-
return &fork, resp, err
89+
return fork, resp, err
9090
}
9191

92-
return &fork, resp, err
92+
return fork, resp, err
9393
}
9494
return nil, resp, err
9595
}
9696

97-
return &fork, resp, nil
97+
return fork, resp, nil
9898
}

github/repos_forks_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ func TestRepositoriesService_CreateFork_deferred(t *testing.T) {
131131
}
132132
}
133133

134+
func TestRepositoriesService_CreateFork_deferred_badBody(t *testing.T) {
135+
t.Parallel()
136+
client, mux, _ := setup(t)
137+
138+
opt := &RepositoryCreateForkOptions{Organization: "o", Name: "n", DefaultBranchOnly: true}
139+
140+
mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) {
141+
testMethod(t, r, "POST")
142+
testJSONBody(t, r, opt)
143+
w.WriteHeader(http.StatusAccepted)
144+
fmt.Fprint(w, `{invalid json`)
145+
})
146+
147+
ctx := t.Context()
148+
repo, _, err := client.Repositories.CreateFork(ctx, "o", "r", opt)
149+
if err == nil {
150+
t.Fatal("Repositories.CreateFork returned nil error")
151+
}
152+
if repo != nil {
153+
t.Errorf("Repositories.CreateFork returned non-nil repo: %+v", repo)
154+
}
155+
}
156+
134157
func TestRepositoriesService_CreateFork_invalidOwner(t *testing.T) {
135158
t.Parallel()
136159
client, _, _ := setup(t)

github/security_advisories.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,21 @@ func (s *SecurityAdvisoriesService) CreateTemporaryPrivateFork(ctx context.Conte
168168
return nil, nil, err
169169
}
170170

171-
var fork Repository
171+
var fork *Repository
172172
resp, err := s.client.Do(req, &fork)
173173
if err != nil {
174174
var aerr *AcceptedError
175175
if errors.As(err, &aerr) {
176176
if err := json.Unmarshal(aerr.Raw, &fork); err != nil {
177-
return &fork, resp, err
177+
return fork, resp, err
178178
}
179179

180-
return &fork, resp, err
180+
return fork, resp, err
181181
}
182182
return nil, resp, err
183183
}
184184

185-
return &fork, resp, nil
185+
return fork, resp, nil
186186
}
187187

188188
// ListRepositorySecurityAdvisoriesForOrg lists the repository security advisories for an organization.

github/security_advisories_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,26 @@ func TestSecurityAdvisoriesService_CreateTemporaryPrivateFork_deferred(t *testin
513513
}
514514
}
515515

516+
func TestSecurityAdvisoriesService_CreateTemporaryPrivateFork_deferred_badBody(t *testing.T) {
517+
t.Parallel()
518+
client, mux, _ := setup(t)
519+
520+
mux.HandleFunc("/repos/o/r/security-advisories/ghsa_id/forks", func(w http.ResponseWriter, r *http.Request) {
521+
testMethod(t, r, "POST")
522+
w.WriteHeader(http.StatusAccepted)
523+
fmt.Fprint(w, `{invalid json`)
524+
})
525+
526+
ctx := t.Context()
527+
fork, _, err := client.SecurityAdvisories.CreateTemporaryPrivateFork(ctx, "o", "r", "ghsa_id")
528+
if err == nil {
529+
t.Fatal("SecurityAdvisories.CreateTemporaryPrivateFork returned nil error")
530+
}
531+
if fork != nil {
532+
t.Errorf("SecurityAdvisories.CreateTemporaryPrivateFork returned non-nil fork: %+v", fork)
533+
}
534+
}
535+
516536
func TestSecurityAdvisoriesService_CreateTemporaryPrivateFork_invalidOwner(t *testing.T) {
517537
t.Parallel()
518538
client, _, _ := setup(t)

0 commit comments

Comments
 (0)