Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider: Remove extraneous nil checks before d.Set() #15893

Closed
bflad opened this issue Oct 28, 2020 · 6 comments · Fixed by #16682 or #28902
Closed

provider: Remove extraneous nil checks before d.Set() #15893

bflad opened this issue Oct 28, 2020 · 6 comments · Fixed by #16682 or #28902
Labels
linter Pertains to changes to or issues with the various linters. stale Old or inactive issues managed by automation, if no further action taken these will get closed. technical-debt Addresses areas of the codebase that need refactoring or redesign. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Milestone

Comments

@bflad
Copy link
Contributor

bflad commented Oct 28, 2020

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

The (*schema.ResourceData).Set() receiver method automatically handles pointer types and nil values, so checking for nil beforehand is extraneous.

Example semgrep Rule

rules:
  - id: helper-schema-ResourceData-Set-extraneous-nil-check
    languages: [go]
    message: Nil value check before `d.Set()` is extraneous
    paths:
      include:
        - aws/
    patterns:
      - pattern-either:
        - pattern: |
            if $APIOBJECT != nil {
              d.Set($ATTRIBUTE, $APIOBJECT)
            }
        - pattern: |
            if $APIOBJECT != nil {
              if $ERROR := d.Set($ATTRIBUTE, $APIOBJECT); err != nil { $BODY }
            }
        - pattern: |
            if $APIOBJECT != nil {
              ...
              $ERROR := d.Set($ATTRIBUTE, $APIOBJECT)
              ...
            }
        - pattern: |
            if $APIOBJECT != nil {
              ...
              $ERROR = d.Set($ATTRIBUTE, $APIOBJECT)
              ...
            }
    severity: WARNING

Example Reports

aws/cloudfront_distribution_configuration_structure.go
severity:warning rule:helper-schema-ResourceData-Set-extraneous-nil-check: Nil value check before `d.Set()` is extraneous
98:	if distributionConfig.CallerReference != nil {
99:		d.Set("caller_reference", distributionConfig.CallerReference)
100:	}
106:	if distributionConfig.DefaultRootObject != nil {
107:		d.Set("default_root_object", distributionConfig.DefaultRootObject)
108:	}
109:	if distributionConfig.HttpVersion != nil {
110:		d.Set("http_version", distributionConfig.HttpVersion)
111:	}
112:	if distributionConfig.WebACLId != nil {
113:		d.Set("web_acl_id", distributionConfig.WebACLId)
114:	}

aws/data_source_aws_ami.go
severity:warning rule:helper-schema-ResourceData-Set-extraneous-nil-check: Nil value check before `d.Set()` is extraneous
258:	if image.Description != nil {
259:		d.Set("description", image.Description)
260:	}
264:	if image.ImageOwnerAlias != nil {
265:		d.Set("image_owner_alias", image.ImageOwnerAlias)
266:	}
268:	if image.KernelId != nil {
269:		d.Set("kernel_id", image.KernelId)
270:	}
273:	if image.Platform != nil {
274:		d.Set("platform", image.Platform)
275:	}
277:	if image.RamdiskId != nil {
278:		d.Set("ramdisk_id", image.RamdiskId)
279:	}
280:	if image.RootDeviceName != nil {
281:		d.Set("root_device_name", image.RootDeviceName)
282:	}
285:	if image.SriovNetSupport != nil {
286:		d.Set("sriov_net_support", image.SriovNetSupport)
287:	}

aws/data_source_aws_elasticache_cluster.go
severity:warning rule:helper-schema-ResourceData-Set-extraneous-nil-check: Nil value check before `d.Set()` is extraneous
192:	if cluster.ReplicationGroupId != nil {
193:		d.Set("replication_group_id", cluster.ReplicationGroupId)
194:	}

Affected Resources

TBD

Definition of Done

  • Rule added to .semgrep.yml with no reports on main branch and CI passes
@bflad bflad added tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. technical-debt Addresses areas of the codebase that need refactoring or redesign. linter Pertains to changes to or issues with the various linters. labels Oct 28, 2020
ewbankkit added a commit to ewbankkit/terraform-provider-aws that referenced this issue Dec 2, 2020
…icorp#15893).

Acceptance test output:

$ make testacc TEST=./aws TESTARGS='-run=TestAccAWSEBSVolume_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSEBSVolume_ -timeout 120m
=== RUN   TestAccAWSEBSVolume_basic
=== PAUSE TestAccAWSEBSVolume_basic
=== RUN   TestAccAWSEBSVolume_updateAttachedEbsVolume
=== PAUSE TestAccAWSEBSVolume_updateAttachedEbsVolume
=== RUN   TestAccAWSEBSVolume_updateSize
=== PAUSE TestAccAWSEBSVolume_updateSize
=== RUN   TestAccAWSEBSVolume_updateType
=== PAUSE TestAccAWSEBSVolume_updateType
=== RUN   TestAccAWSEBSVolume_updateIops_Io1
=== PAUSE TestAccAWSEBSVolume_updateIops_Io1
=== RUN   TestAccAWSEBSVolume_updateIops_Io2
=== PAUSE TestAccAWSEBSVolume_updateIops_Io2
=== RUN   TestAccAWSEBSVolume_kmsKey
=== PAUSE TestAccAWSEBSVolume_kmsKey
=== RUN   TestAccAWSEBSVolume_NoIops
=== PAUSE TestAccAWSEBSVolume_NoIops
=== RUN   TestAccAWSEBSVolume_InvalidIopsForType
=== PAUSE TestAccAWSEBSVolume_InvalidIopsForType
=== RUN   TestAccAWSEBSVolume_withTags
=== PAUSE TestAccAWSEBSVolume_withTags
=== RUN   TestAccAWSEBSVolume_multiAttach
=== PAUSE TestAccAWSEBSVolume_multiAttach
=== RUN   TestAccAWSEBSVolume_outpost
=== PAUSE TestAccAWSEBSVolume_outpost
=== RUN   TestAccAWSEBSVolume_gp3_basic
=== PAUSE TestAccAWSEBSVolume_gp3_basic
=== RUN   TestAccAWSEBSVolume_gp3_iops
=== PAUSE TestAccAWSEBSVolume_gp3_iops
=== RUN   TestAccAWSEBSVolume_gp3_throughput
=== PAUSE TestAccAWSEBSVolume_gp3_throughput
=== RUN   TestAccAWSEBSVolume_disappears
=== PAUSE TestAccAWSEBSVolume_disappears
=== CONT  TestAccAWSEBSVolume_basic
=== CONT  TestAccAWSEBSVolume_withTags
=== CONT  TestAccAWSEBSVolume_disappears
=== CONT  TestAccAWSEBSVolume_gp3_throughput
=== CONT  TestAccAWSEBSVolume_gp3_iops
=== CONT  TestAccAWSEBSVolume_gp3_basic
=== CONT  TestAccAWSEBSVolume_outpost
=== CONT  TestAccAWSEBSVolume_multiAttach
=== CONT  TestAccAWSEBSVolume_updateIops_Io2
=== CONT  TestAccAWSEBSVolume_InvalidIopsForType
=== CONT  TestAccAWSEBSVolume_NoIops
=== CONT  TestAccAWSEBSVolume_kmsKey
=== CONT  TestAccAWSEBSVolume_updateType
=== CONT  TestAccAWSEBSVolume_updateIops_Io1
=== CONT  TestAccAWSEBSVolume_updateSize
=== CONT  TestAccAWSEBSVolume_updateAttachedEbsVolume
=== CONT  TestAccAWSEBSVolume_outpost
    data_source_aws_outposts_outposts_test.go:66: skipping since no Outposts found
--- SKIP: TestAccAWSEBSVolume_outpost (2.13s)
--- PASS: TestAccAWSEBSVolume_InvalidIopsForType (17.40s)
--- PASS: TestAccAWSEBSVolume_disappears (52.68s)
--- PASS: TestAccAWSEBSVolume_NoIops (55.37s)
--- PASS: TestAccAWSEBSVolume_gp3_basic (64.85s)
--- PASS: TestAccAWSEBSVolume_basic (65.87s)
--- PASS: TestAccAWSEBSVolume_multiAttach (66.08s)
--- PASS: TestAccAWSEBSVolume_withTags (66.99s)
--- PASS: TestAccAWSEBSVolume_kmsKey (67.13s)
--- PASS: TestAccAWSEBSVolume_updateIops_Io1 (95.66s)
--- PASS: TestAccAWSEBSVolume_gp3_iops (96.68s)
--- PASS: TestAccAWSEBSVolume_updateType (98.48s)
--- PASS: TestAccAWSEBSVolume_updateIops_Io2 (98.71s)
--- PASS: TestAccAWSEBSVolume_gp3_throughput (99.21s)
--- PASS: TestAccAWSEBSVolume_updateSize (99.32s)
--- PASS: TestAccAWSEBSVolume_updateAttachedEbsVolume (181.29s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	181.371s
@ewbankkit
Copy link
Contributor

Will this also handle extraneous calls to aws.xxxValue() functions?

d.Set("foo", aws.StringValue(thing.Foo))

vs.

d.Set("foo", thing.Foo)

@bflad
Copy link
Contributor Author

bflad commented Dec 2, 2020

Not as written, but could be handled in a separate rule. 👍

@bflad
Copy link
Contributor Author

bflad commented Dec 2, 2020

Created a separate issue for tracking: #16554 😄

ewbankkit added a commit to ewbankkit/terraform-provider-aws that referenced this issue Dec 22, 2020
…icorp#15893).

Acceptance test output:

$ make testacc TEST=./aws TESTARGS='-run=TestAccAWSEBSVolume_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSEBSVolume_ -timeout 120m
=== RUN   TestAccAWSEBSVolume_basic
=== PAUSE TestAccAWSEBSVolume_basic
=== RUN   TestAccAWSEBSVolume_updateAttachedEbsVolume
=== PAUSE TestAccAWSEBSVolume_updateAttachedEbsVolume
=== RUN   TestAccAWSEBSVolume_updateSize
=== PAUSE TestAccAWSEBSVolume_updateSize
=== RUN   TestAccAWSEBSVolume_updateType
=== PAUSE TestAccAWSEBSVolume_updateType
=== RUN   TestAccAWSEBSVolume_updateIops_Io1
=== PAUSE TestAccAWSEBSVolume_updateIops_Io1
=== RUN   TestAccAWSEBSVolume_updateIops_Io2
=== PAUSE TestAccAWSEBSVolume_updateIops_Io2
=== RUN   TestAccAWSEBSVolume_kmsKey
=== PAUSE TestAccAWSEBSVolume_kmsKey
=== RUN   TestAccAWSEBSVolume_NoIops
=== PAUSE TestAccAWSEBSVolume_NoIops
=== RUN   TestAccAWSEBSVolume_InvalidIopsForType
=== PAUSE TestAccAWSEBSVolume_InvalidIopsForType
=== RUN   TestAccAWSEBSVolume_withTags
=== PAUSE TestAccAWSEBSVolume_withTags
=== RUN   TestAccAWSEBSVolume_multiAttach
=== PAUSE TestAccAWSEBSVolume_multiAttach
=== RUN   TestAccAWSEBSVolume_outpost
=== PAUSE TestAccAWSEBSVolume_outpost
=== RUN   TestAccAWSEBSVolume_gp3_basic
=== PAUSE TestAccAWSEBSVolume_gp3_basic
=== RUN   TestAccAWSEBSVolume_gp3_iops
=== PAUSE TestAccAWSEBSVolume_gp3_iops
=== RUN   TestAccAWSEBSVolume_gp3_throughput
=== PAUSE TestAccAWSEBSVolume_gp3_throughput
=== RUN   TestAccAWSEBSVolume_disappears
=== PAUSE TestAccAWSEBSVolume_disappears
=== CONT  TestAccAWSEBSVolume_basic
=== CONT  TestAccAWSEBSVolume_withTags
=== CONT  TestAccAWSEBSVolume_disappears
=== CONT  TestAccAWSEBSVolume_gp3_throughput
=== CONT  TestAccAWSEBSVolume_gp3_iops
=== CONT  TestAccAWSEBSVolume_gp3_basic
=== CONT  TestAccAWSEBSVolume_outpost
=== CONT  TestAccAWSEBSVolume_multiAttach
=== CONT  TestAccAWSEBSVolume_updateIops_Io2
=== CONT  TestAccAWSEBSVolume_InvalidIopsForType
=== CONT  TestAccAWSEBSVolume_NoIops
=== CONT  TestAccAWSEBSVolume_kmsKey
=== CONT  TestAccAWSEBSVolume_updateType
=== CONT  TestAccAWSEBSVolume_updateIops_Io1
=== CONT  TestAccAWSEBSVolume_updateSize
=== CONT  TestAccAWSEBSVolume_updateAttachedEbsVolume
=== CONT  TestAccAWSEBSVolume_outpost
    data_source_aws_outposts_outposts_test.go:66: skipping since no Outposts found
--- SKIP: TestAccAWSEBSVolume_outpost (2.13s)
--- PASS: TestAccAWSEBSVolume_InvalidIopsForType (17.40s)
--- PASS: TestAccAWSEBSVolume_disappears (52.68s)
--- PASS: TestAccAWSEBSVolume_NoIops (55.37s)
--- PASS: TestAccAWSEBSVolume_gp3_basic (64.85s)
--- PASS: TestAccAWSEBSVolume_basic (65.87s)
--- PASS: TestAccAWSEBSVolume_multiAttach (66.08s)
--- PASS: TestAccAWSEBSVolume_withTags (66.99s)
--- PASS: TestAccAWSEBSVolume_kmsKey (67.13s)
--- PASS: TestAccAWSEBSVolume_updateIops_Io1 (95.66s)
--- PASS: TestAccAWSEBSVolume_gp3_iops (96.68s)
--- PASS: TestAccAWSEBSVolume_updateType (98.48s)
--- PASS: TestAccAWSEBSVolume_updateIops_Io2 (98.71s)
--- PASS: TestAccAWSEBSVolume_gp3_throughput (99.21s)
--- PASS: TestAccAWSEBSVolume_updateSize (99.32s)
--- PASS: TestAccAWSEBSVolume_updateAttachedEbsVolume (181.29s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	181.371s
@github-actions
Copy link

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

@github-actions github-actions bot added the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Nov 23, 2022
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 24, 2022
@github-actions github-actions bot added this to the v4.51.0 milestone Jan 13, 2023
@github-actions
Copy link

This functionality has been released in v4.51.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
linter Pertains to changes to or issues with the various linters. stale Old or inactive issues managed by automation, if no further action taken these will get closed. technical-debt Addresses areas of the codebase that need refactoring or redesign. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
2 participants